詳解nginx rewrite和根據(jù)url參數(shù)location
最近項(xiàng)目中涉及到舊老項(xiàng)目遷移,需要在nginx上做些配置,所以簡(jiǎn)單學(xué)習(xí)了下,好記性不如爛筆頭,先記下來(lái)。
rewrite
首先查看下nginx是否支持rewrite:
./nginx -V
不支持說(shuō)明安裝nginx時(shí)候缺少pcre,需要重新安裝nginx:
#安裝pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-8.34.tar.gz cd pcre-8.34 ./configure make make install #安裝nginx cd 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 \ make make install #啟動(dòng)nginx ./nginx #重啟nginx ./nginx –s reload
示例:
比如現(xiàn)有如下的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;
#設(shè)定查看Nginx狀態(tài)的地址
location /NginxStatus {
#stub_status on;
access_log on;
auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
}
}
}
現(xiàn)在需要作如下的重定向:
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;
}
關(guān)于這里的rewrite配置主要說(shuō)明以下幾點(diǎn):
- rewrite用法: rewrite 正則 替換 標(biāo)志位
- 第一行配置和第二行配置順序不能顛倒,因?yàn)閚ginx會(huì)從上往下依次rewrite(break在這里不起作用);
- (?!)表示忽略大小寫(xiě)匹配(網(wǎng)上說(shuō)的是~*,但好像不起作用,我的nginx版本是1.0.12);
- 1,1,2表示前面正則表達(dá)式匹配到的部分;
- rewrite可以在server里也可以在location里,nginx會(huì)首先執(zhí)行server里的rewrite,然后才會(huì)執(zhí)行l(wèi)ocation,意味著location的是重寫(xiě)后的url,之后還會(huì)執(zhí)行l(wèi)ocation里的rewrite,最后nginx還會(huì)拿結(jié)果去執(zhí)行剩下的location。
根據(jù)url參數(shù)location
實(shí)際開(kāi)發(fā)中經(jīng)常有根據(jù)請(qǐng)求參數(shù)來(lái)路由到不同請(qǐng)求處理者的情況,根據(jù)POST請(qǐng)求參數(shù)需要些nginx插件,這里主要簡(jiǎn)單介紹下如何根據(jù)GET參數(shù)來(lái)路由。
還是上面的配置文件。比如我們希望訪問(wèn)http://192.168.71.51:6061/do1.aspx?t=1212&c=uplog當(dāng)url中的參數(shù)c為config或uplog的時(shí)候(忽略大小寫(xiě))我們路由到其他地方:
首先增加一個(gè)upstream,比如:
……
upstream other {
server 192.168.71.41:2210;
}
……
然后在location里增加如下的判斷即可:
……
location / {
if ( $query_string ~* ^(.*)c=config\b|uplog\b(.*)$ ){
proxy_pass http://other;
}
……
關(guān)鍵是標(biāo)紅的行,$query_string表示url參數(shù),后面是標(biāo)準(zhǔn)的正則匹配,需要的注意的是nginx中if有很多限制,語(yǔ)法很苛刻,具體參看上面的文檔。
很簡(jiǎn)單卻很實(shí)用的配置,希望能幫到正在找這方面信息的同學(xué)。
相關(guān)文章
Nginx服務(wù)器限制IP訪問(wèn)的各種情況全解析
這篇文章主要介紹了Nginx服務(wù)器限制IP訪問(wèn)的各種情況全解析,包括限制同一IP在一段時(shí)間內(nèi)的訪問(wèn)次數(shù)和全局限IP訪問(wèn)以及限制IP訪問(wèn)指定目錄等情況,需要的朋友可以參考下2015-08-08
Nginx搭建負(fù)載均衡集群的實(shí)現(xiàn)
這篇文章主要介紹了Nginx搭建負(fù)載均衡集群的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Nginx中IF語(yǔ)句實(shí)現(xiàn)數(shù)學(xué)比較功能
這篇文章主要介紹了Nginx中IF語(yǔ)句實(shí)現(xiàn)數(shù)學(xué)比較功能,即在Nginx中用if判斷數(shù)字大小,類(lèi)似編程語(yǔ)言中的邏輯比較,需要的朋友可以參考下2015-02-02
nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解
這篇文章主要介紹了nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Mac中使用Nginx實(shí)現(xiàn)80端口轉(zhuǎn)發(fā)8080端口
端口轉(zhuǎn)發(fā)(Port forwarding),有時(shí)被叫做隧道,是安全殼(SSH) 為網(wǎng)絡(luò)安全通信使用的一種方法。端口轉(zhuǎn)發(fā)是轉(zhuǎn)發(fā)一個(gè)網(wǎng)絡(luò)端口從一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)到另一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)的行為,其使一個(gè)外部用戶從外部經(jīng)過(guò)一個(gè)被激活的NAT路由器到達(dá)一個(gè)在私有內(nèi)部IP地址(局域網(wǎng)內(nèi)部)上的一個(gè)端口2017-09-09

