Implementation of Nginx Routing Forwarding and Reverse Proxy location Configuration
- 2021-11-29 16:52:57
- OfStack
Three Ways of Nginx Configuration
The first type directly replaces the location matching part
The target address of the second type of proxy_pass, without/by default, means that only the proxy domain name, url and parameter parts will not change (splice the requested path to the proxy URL after the proxy_pass target domain name)
If/is added after the target address of the third type of proxy_pass, it means that the part of path that matches location successfully is cut out and then spliced to the target address of proxy_pass
location Configuration
location [ = | ~ | ~* | ^~ ] uri {...}
The square brackets in front of uri are optional, explained as follows:
"=": Before the standard uri, the request string is required to match the uri strictly, and once the match is successful, it will stop '~': Used before regular uri and case sensitive '~ *': Used before regular uri but not case sensitive "^ ~": Before using standard uri, Nginx is required to use this location to process the request immediately after finding the location that identifies uri and the request string, instead of using the regular uri in the location block to match the request string
符号 | 含义 |
= | 精确匹配 |
^~ | 非正则匹配 |
~ | 正则匹配(区分大小写) |
~* | 正则匹配(不区分大小写) |
!~ | 正则不匹配(区分大小写) |
!~* | 正则不匹配(不区分大小写) |
普通匹配(这里没有符号的时候) |
Example
For example, the following configuration demonstrates the third configuration scenario when we visit http://44.179. 118.54:80/shop/xxx
When accessing, Nginx will intercept /shop/and splice the following path onto proxy_pass
So what we actually visited is: http://44.179. 118.54: 8007/xxx
# shop-service
# Reverse proxy shop-service Services
location ^~ /shop/ {
#proxy_redirect off;
#proxy_connect_timeout 60;
#proxy_read_timeout 60;
#proxy_send_timeout 60;
#proxy_buffer_size 4k;
#proxy_buffers 4 32k;
#proxy_busy_buffers_size 64k;
#proxy_temp_file_write_size 64k;
#proxy_max_temp_file_size 128m;
proxy_pass http://44.179.118.54:8007/;
#proxy_set_header X-Real-IP $remote_addr;
#root /var/www/test/user/
#index index.html
#proxy_pass https://www.baidu.com;
}