教你nginx跳轉配置的四種方式
目錄
- 前言
- 一、配置server對應的域名
- 1.1、精確匹配
- 1.2、正則表達式
- 二、配置location
- 2.1、Location 匹配規則:僅匹配URI,忽略參數
- 2.2、舉例
- 2.3、匹配順序如下圖
- 2.4、如何debug正則呢?
- 三、配置rewrite
- 3.1、重定向
- 3.2、如何查看rewrite日志
- 四、配置 proxy
- 五、小結
前言
最近工作用到了nginx,但是路由配置特殊,業務場景復雜,因此整理了集中nginx跳轉的配置方式,如servername的正則,location的匹配順序,rewrite和proxy的示例,相信總有一種滿足你的需求。
一、配置server對應的域名
server name 為虛擬服務器的識別路徑。因此不同的域名會通過請求頭中的HOST字段,匹配到特定的server塊,轉發到對應的應用服務器中去。server_name匹配規則:后面可以跟多個域名,第1個是主域名
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";????????}????}
請求結果
curl http://my.test.com:8080 返回:I am mytest!
curl http://test1.com:8080 返回:I am test1!
1.2、正則表達式
以*通配符開始的最長字符串,如下示例
server {listen 8080;server_name test1.*;location / { return 200 "I am test1!\n";} }
以*通配符結束的最長字符串
listen 8080;server_name *.test.com;location / { return 200 "I am mytest!\n";} }
通配符名字只可以在名字的起始處或結尾處包含一個星號,并且星號與其他字符之間用點分隔。所以,“my..com“都是非法的。
例如 :server_name my..com;
報以下錯誤:
nginx: [emerg] invalid server name or wildcard "my.*.com" on 0.0.0.0:8080
匹配正則表達式
server {listen 8080;server_name ~^my(?<serno>.+).mydomain.com$;location / { return 200 $serno;} }
解釋說明
- ~: 表示大小寫敏感的正則;
- ^:匹配字符串的開始;
- {.+}:換行符以外的任意自讀重復一次活更多次;
- (): 分組與取值;
- :表示轉義;
- serno:設置提取的變量;
- $:匹配字符串的結束;
請求結果
curl http://my02.mydomain.com:8080 返回:02% curl http://my03.mydomain.com:8080 返回:03%
server_name的配置順序是怎樣的呢?
按照如下順序匹配:
- 匹配順序->
- ->精確匹配
- ->*在前的域名
- ->*在后的域名
- ->按文件中的順序匹配
- ->default server:第一個,listen指定default
二、配置location
2.1、Location 匹配規則:僅匹配URI,忽略參數
location [=|~|~*|^~] /uri/ { … }
匹配的正則符號如下:
- = 嚴格匹配。如果請求匹配這個location,那么將停止搜索并立即處理此請求
- ~ 區分大小寫匹配(可用正則表達式)
- ~* 不區分大小寫匹配(可用正則表達式)
- !~ 區分大小寫不匹配
- !~* 不區分大小寫不匹配
- ^~ 如果把這個前綴用于一個常規字符串,那么告訴nginx 如果路徑匹配那么不測試正則表達式
2.2、舉例
1、匹配任意請求location [=|~|~*|^~] /uri/ { … }2、不區分大小寫匹配以js、php結尾的請求location ~* .(js|php)$ { … }3、區分大小寫匹配以.txt結尾的請求location ~ ^.+\.txt$
2.3、匹配順序如下圖
按照上面的規則配置了如下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 走到了正則匹配,不會走到/documents/txt1(正則沒走完) 返回configuration E
- curl http://test1.com:8080/documents/txt1/,返回configuration C,因為正則都不匹配
2.4、如何debug正則呢?
編譯的時候加上 --with-debug選項,例如 ./configure --with-debug
conf文件加上要debug的host,debug_connection對應要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];
默認值:none
應用位置:server、location、if
rewrite是實現URL重定向的重要指令,他根據regex(正則表達式)來匹配內容跳轉到replacement,結尾是flag標記.
3.1、重定向
return三種code,code url和url。
返回狀態碼:444表示關閉連接 301表示http1。0中永久重定向,302表示臨時重定向,進制緩存。http1.1后,303表示臨時重定向,允許改變方法,進制緩存,307表示臨時重定向,不允許改變方法,禁止被緩存,308表示永久重定向,不允許改變方法。
返回code
location / { return 301 https://www.xxxx.com$request_uri;}
通過$request_uri變量匹配所有的URI。
rewrite ^ https://www.xxxx.com$request_uri? permanent;
通過正則匹配所有的URI后再去掉開頭第一個/(反斜線)。
rewrite ^/(.*)$ https://www.xxxx.com/$1;
與if指令結合
server {listen 80;server_name test1.net test2.net;if ($host != "test1.net" ) {rewrite ^/(.*)$ http://www.baidu.net/$1 permanent;}}
3.2、如何查看rewrite日志
打開日志開關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
對上游服務使用http/https協議進行反向代理。proxy_pass后面跟url,可以仿造location,if in location和limit_except上下文中。 這個功能是默認編譯到nginx中的。本文重點討論http proxy。
url參數規則
- url必須以http或者https開頭,接下來是域名、ip、unix socket或者upstream名字,都可以就端口。后面是可選的uri
http示例
proxy_pass http://localhost:8000/uri/;
UNIX域套接字路徑來定義示例
proxy_pass http://unix:/tmp/backend.socket:/uri/;
url中是否攜帶uri,結果也不一樣,如果在proxy_pass后面的url加/,相當于是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分給代理走。
目錄結構如下
├── 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參數中可以攜帶變量
proxy_pass http://$host$uri;
- 可以配合rewrite break語句
location /nameb/ { rewrite /nameb/([^/]+) /test?nameb=$1 break; proxy_pass http://127.0.0.1:8801/; }
五、小結
配置nginx的路由,有多種方式,域名可以用server_name配置,uri可以用location配置,復雜的可以加rewrite配置修改請求。還有就是配置proxy代理,在代理中轉發id等。
到此這篇關于nginx跳轉配置的四種方式的文章就介紹到這了,更多相關nginx跳轉配置內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!
