国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

詳解nginx rewrite和根據url參數location

2024-08-30 12:28:21
字體:
來源:轉載
供稿:網友

最近項目中涉及到舊老項目遷移,需要在nginx上做些配置,所以簡單學習了下,好記性不如爛筆頭,先記下來。

rewrite

首先查看下nginx是否支持rewrite:

./nginx -V

不支持說明安裝nginx時候缺少pcre,需要重新安裝nginx:

#安裝pcrewget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gztar -zxvf pcre-8.34.tar.gzcd pcre-8.34./configuremakemake install#安裝nginxcd nginx-1.0.12./configure --conf-path=/usr/local/nginx/conf/nginx.conf /--pid-path=/usr/local/nginx/nginx.pid /--with-http_ssl_module /--with-pcre=/usr/local/src/pcre-8.34 /makemake install#啟動nginx./nginx#重啟nginx./nginx –s reload

示例:

比如現有如下的nginx配置:

worker_processes 24;#worker_cpu_affinity 0000000000000001;worker_rlimit_nofile 65535;error_log logs/error.log crit;pid    logs/nginx.pid;events {  use  epoll;   worker_connections 2048000;}http {  include    mime.types;  default_type application/octet-stream;  charset utf-8;  sendfile    on;  tcp_nopush   on;  tcp_nodelay   on;  keepalive_timeout 60;  client_max_body_size    10m;   client_body_buffer_size   128k;   upstream log {    server 192.168.80.147:8338;  }  server {    listen    6061;    server_name 192.168.71.51;    location / {       proxy_pass         http://log;       proxy_redirect       off;       proxy_set_header      Host $host;       proxy_set_header      Remote_Addr $remote_addr;       proxy_set_header  X-REAL-IP $remote_addr;       proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_connect_timeout    90;       proxy_send_timeout     90;       proxy_read_timeout     90;       proxy_buffer_size      4k;       proxy_buffers        4 32k;       proxy_busy_buffers_size   64k;       proxy_temp_file_write_size 64k;    }     error_page  500 502 503 504 /50x.html;    location = /50x.html {      root  html;    }  log_format log '$remote_addr - $remote_user [$time_local] "$request" '           '$status $body_bytes_sent "$http_referer" '           '"$http_user_agent" "$http_x_forwarded_for"';    access_log logs/access_log.log log;        #設定查看Nginx狀態的地址      location /NginxStatus {     #stub_status on;      access_log on;      auth_basic "NginxStatus";      #auth_basic_user_file conf/htpasswd;      }  }}

現在需要作如下的重定向:

192.168.71.51/log.aspx –> 192.168.80.147:8338/log

192.168.71.51/do.aspx –> 192.168.80.147:8338/do

192.168.71.51/uplog.aspx –> 192.168.80.147:8338/log

可以如下配置:

server {    listen    6061;    server_name 192.168.71.51;  rewrite ^(.*)(?i)uplog.aspx(.*)$ $1log$2 break;  rewrite ^(.*)(?i)log.aspx(.*)$ $1log$2 break;  rewrite ^(.*)(?i)do.aspx(.*)$ $1do$2 break;      location / {       proxy_pass         http://log;       proxy_redirect       off;       proxy_set_header      Host $host;       proxy_set_header      Remote_Addr $remote_addr;       proxy_set_header  X-REAL-IP $remote_addr;       proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_connect_timeout    90;       proxy_send_timeout     90;       proxy_read_timeout     90;       proxy_buffer_size      4k;       proxy_buffers        4 32k;       proxy_busy_buffers_size   64k;       proxy_temp_file_write_size 64k;    }

關于這里的rewrite配置主要說明以下幾點:

  • rewrite用法: rewrite 正則 替換 標志位
  • 第一行配置和第二行配置順序不能顛倒,因為nginx會從上往下依次rewrite(break在這里不起作用);
  • (?!)表示忽略大小寫匹配(網上說的是~*,但好像不起作用,我的nginx版本是1.0.12);
  •  1,1,2表示前面正則表達式匹配到的部分;
  •  rewrite可以在server里也可以在location里,nginx會首先執行server里的rewrite,然后才會執行location,意味著location的是重寫后的url,之后還會執行location里的rewrite,最后nginx還會拿結果去執行剩下的location。

根據url參數location

實際開發中經常有根據請求參數來路由到不同請求處理者的情況,根據POST請求參數需要些nginx插件,這里主要簡單介紹下如何根據GET參數來路由。

還是上面的配置文件。比如我們希望訪問http://192.168.71.51:6061/do1.aspx?t=1212&c=uplog當url中的參數c為config或uplog的時候(忽略大小寫)我們路由到其他地方:

首先增加一個upstream,比如:

……upstream other {   server 192.168.71.41:2210;   }……

然后在location里增加如下的判斷即可:

……location / {     if ( $query_string ~* ^(.*)c=config/b|uplog/b(.*)$ ){     proxy_pass         http://other;     }……

關鍵是標紅的行,$query_string表示url參數,后面是標準的正則匹配,需要的注意的是nginx中if有很多限制,語法很苛刻,具體參看上面的文檔。

 很簡單卻很實用的配置,希望能幫到正在找這方面信息的同學。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汉川市| 阳曲县| 尚义县| 富锦市| 兴义市| 伊通| 大冶市| 晴隆县| 新晃| 县级市| 明光市| 墨江| 呈贡县| 神木县| 澄城县| 宜昌市| 寿宁县| 聊城市| 巴彦淖尔市| 宝鸡市| 通江县| 合作市| 兴山县| 紫阳县| 岳阳县| 伊金霍洛旗| 昌吉市| 富阳市| 固始县| 云梦县| 东乌珠穆沁旗| 江阴市| 张掖市| 基隆市| 商都县| 闽清县| 淮阳县| 双柏县| 昂仁县| 自贡市| 阜南县|