教你nginx跳轉(zhuǎn)配置的四種方式
最近工作用到了nginx,但是路由配置特殊,業(yè)務(wù)場景復(fù)雜,因此整理了集中nginx跳轉(zhuǎn)的配置方式,如servername的正則,location的匹配順序,rewrite和proxy的示例,相信總有一種滿足你的需求。
一、配置server對應(yīng)的域名
server name 為虛擬服務(wù)器的識(shí)別路徑。因此不同的域名會(huì)通過請求頭中的HOST字段,匹配到特定的server塊,轉(zhuǎn)發(fā)到對應(yīng)的應(yīng)用服務(wù)器中去。server_name匹配規(guī)則:后面可以跟多個(gè)域名,第1個(gè)是主域名
1.1、精確匹配
如下nginx配置
????????listen???????8080; ????????server_name??test1.com; ????????location / { ????????????return 200 "I am test1!\n"; ????????} ????} ????server { ????????listen???????8080; ????????server_name??my.test.com; ????????location / { ????????????return 200 "I am mytest!\n"; ????????} ????}
請求結(jié)果
curl http://my.test.com:8080 返回:I am mytest!
curl http://test1.com:8080 返回:I am test1!
1.2、正則表達(dá)式
以*通配符開始的最長字符串,如下示例
server { listen 8080; server_name test1.*; location / { return 200 "I am test1!\n"; } }
以*通配符結(jié)束的最長字符串
listen 8080; server_name *.test.com; location / { return 200 "I am mytest!\n"; } }
通配符名字只可以在名字的起始處或結(jié)尾處包含一個(gè)星號,并且星號與其他字符之間用點(diǎn)分隔。所以,“my..com“都是非法的。
例如 :server_name my..com;
報(bào)以下錯(cuò)誤:
nginx: [emerg] invalid server name or wildcard "my.*.com" on 0.0.0.0:8080
匹配正則表達(dá)式
server { listen 8080; server_name ~^my(?<serno>.+).mydomain.com$; location / { return 200 $serno; } }
解釋說明
- ~: 表示大小寫敏感的正則;
- ^:匹配字符串的開始;
- {.+}:換行符以外的任意自讀重復(fù)一次活更多次;
- (): 分組與取值;
- :表示轉(zhuǎn)義;
- serno:設(shè)置提取的變量;
- $:匹配字符串的結(jié)束;
請求結(jié)果
curl http://my02.mydomain.com:8080 返回:02% curl http://my03.mydomain.com:8080 返回:03%
server_name的配置順序是怎樣的呢?
按照如下順序匹配:
- 匹配順序->
- ->精確匹配
- ->*在前的域名
- ->*在后的域名
- ->按文件中的順序匹配
- ->default server:第一個(gè),listen指定default
二、配置location
2.1、Location 匹配規(guī)則:僅匹配URI,忽略參數(shù)
location [=|~|~*|^~] /uri/ { … }
匹配的正則符號如下:
- = 嚴(yán)格匹配。如果請求匹配這個(gè)location,那么將停止搜索并立即處理此請求
- ~ 區(qū)分大小寫匹配(可用正則表達(dá)式)
- ~* 不區(qū)分大小寫匹配(可用正則表達(dá)式)
- !~ 區(qū)分大小寫不匹配
- !~* 不區(qū)分大小寫不匹配
- ^~ 如果把這個(gè)前綴用于一個(gè)常規(guī)字符串,那么告訴nginx 如果路徑匹配那么不測試正則表達(dá)式
2.2、舉例
1、匹配任意請求 location [=|~|~*|^~] /uri/ { … } 2、不區(qū)分大小寫匹配以js、php結(jié)尾的請求 location ~* .(js|php)$ { … } 3、區(qū)分大小寫匹配以.txt結(jié)尾的請求 location ~ ^.+\.txt$
2.3、匹配順序如下圖
按照上面的規(guī)則配置了如下location
location = /documents { return 200 'configuration A' } location /documents { return 200 'configuration B' } location /documents/txt1 { return 200 'configuration C' } location ^~ /documents/ { return 200 'configuration D' } location ~* /documents/(\w+)$ { return 200 'configuration E' } location ~ /documents/$ { return 200 'configuration F' }
- curl http://test1.com:8080/documents,精確匹配返回 configuration A
- curl http://test1.com:8080/documents/ ^~匹配上后不在匹配,返回 configuration D
- curl http://test1.com:8080/documents/txt1 走到了正則匹配,不會(huì)走到/documents/txt1(正則沒走完) 返回configuration E
- curl http://test1.com:8080/documents/txt1/,返回configuration C,因?yàn)檎齽t都不匹配
2.4、如何debug正則呢?
編譯的時(shí)候加上 --with-debug選項(xiàng),例如 ./configure --with-debug
conf文件加上要debug的host,debug_connection對應(yīng)要debug的連接。
events { worker_connections 1024; debug_connection 192.168.1.3; debug_connection 127.0.0.1; }
error.log查看debug日志,圖中test location就是正則匹配的過程
三、配置rewrite
語法如下:
指令語法:rewrite regex replacement[flag];
默認(rèn)值:none
應(yīng)用位置:server、location、if
rewrite是實(shí)現(xiàn)URL重定向的重要指令,他根據(jù)regex(正則表達(dá)式)來匹配內(nèi)容跳轉(zhuǎn)到replacement,結(jié)尾是flag標(biāo)記.
flag標(biāo)記 | 說明 |
---|---|
last | 本條規(guī)則匹配完成后繼續(xù)向下匹配新的location URI規(guī)則 |
break | 本條規(guī)則匹配完成后終止,不在匹配任務(wù)規(guī)則 |
redirect | 返回302臨時(shí)重定向 |
permanent | 返回301永久重定向 |
3.1、重定向
return三種code,code url和url。
返回狀態(tài)碼:444表示關(guān)閉連接 301表示http1。0中永久重定向,302表示臨時(shí)重定向,進(jìn)制緩存。http1.1后,303表示臨時(shí)重定向,允許改變方法,進(jìn)制緩存,307表示臨時(shí)重定向,不允許改變方法,禁止被緩存,308表示永久重定向,不允許改變方法。
返回code
location / { return 301 https://www.xxxx.com$request_uri; }
通過$request_uri變量匹配所有的URI。
rewrite ^ https://www.xxxx.com$request_uri? permanent;
通過正則匹配所有的URI后再去掉開頭第一個(gè)/(反斜線)。
rewrite ^/(.*)$ https://www.xxxx.com/$1;
與if指令結(jié)合
server { listen 80; server_name test1.net test2.net; if ($host != 'test1.net' ) { rewrite ^/(.*)$ http://www.baidu.net/$1 permanent; } }
3.2、如何查看rewrite日志
打開日志開關(guān)rewrite_log on;
可以配置到http,server,location和if上下文中
示例:curl http://test1.com:8080/first/2.txt
location /first { rewrite_log on; rewrite /first(.*) /second$1 last; }
效果圖如下
四、配置 proxy
對上游服務(wù)使用http/https協(xié)議進(jìn)行反向代理。proxy_pass后面跟url,可以仿造location,if in location和limit_except上下文中。 這個(gè)功能是默認(rèn)編譯到nginx中的。本文重點(diǎn)討論http proxy。
url參數(shù)規(guī)則
- url必須以http或者h(yuǎn)ttps開頭,接下來是域名、ip、unix socket或者upstream名字,都可以就端口。后面是可選的uri
http示例
proxy_pass http://localhost:8000/uri/;
UNIX域套接字路徑來定義示例
proxy_pass http://unix:/tmp/backend.socket:/uri/;
url中是否攜帶uri,結(jié)果也不一樣,如果在proxy_pass后面的url加/,相當(dāng)于是絕對根路徑,則nginx不會(huì)把location中匹配的路徑部分代理走;如果沒有/,則會(huì)把匹配的路徑部分給代理走。
目錄結(jié)構(gòu)如下
├── first
│ └── index.html
├── index.html
└── second
└── index.html
nginx配置如下
server { listen 8081; server_name my.test.com; } server { listen 8082; # 第一種情況 location /first { proxy_pass http://my.test.com:8081; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 第二種情況 location /first { proxy_pass http://my.test.com:8081/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
不帶/,然后 curl http://127.0.0.1:8082/first/index.html 返回index html
帶/,然后 curl http://127.0.0.1:8082/first/index.html 返回first index
- Url參數(shù)中可以攜帶變量
proxy_pass http://$host$uri;
- 可以配合rewrite break語句
location /nameb/ { rewrite /nameb/([^/]+) /test?nameb=$1 break; proxy_pass http://127.0.0.1:8801/; }
五、小結(jié)
配置nginx的路由,有多種方式,域名可以用server_name配置,uri可以用location配置,復(fù)雜的可以加rewrite配置修改請求。還有就是配置proxy代理,在代理中轉(zhuǎn)發(fā)id等。
到此這篇關(guān)于nginx跳轉(zhuǎn)配置的四種方式的文章就介紹到這了,更多相關(guān)nginx跳轉(zhuǎn)配置內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。