赞
踩
指令 | 说明 |
---|---|
= | 用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。 |
~ | 用于表示 uri 包含正则表达式,并且区分大小写。 |
~* | 用于表示 uri 包含正则表达式,并且不区分大小写。 |
^~ | 用于不含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。 |
!~ | 区分大小写不匹配。 |
!~* | 不区分大小写不匹配 |
/a | 普通前缀匹配,优先级低于带参数前缀匹配。 |
/ | 任何请求都会匹配 |
# 如果请求的是:http://localhost:80/api1,
# 转发形成的就会是:http://localhost:8001/
location /api1/ {
proxy_pass http://localhost:8001/;
}
# 如果请求的是:http://localhost:80/api1,会被匹配到
# 转发形成的就会是:http://localhost:8001/
# 如果请求的是:http://localhost:80/api1/test,不会被匹配到,因为是精确匹配
location = /api1/ {
proxy_pass http://localhost:8001/;
}
# rewrite重写了请求路径,break不可省略,$1为正则匹配的内容
# ^/api1/(.*)$,在这个正则表达式中,$1为(.*)中的内容
# proxy_set_header的使用并不会失效
# 如果请求的是:http://localhost:80/api1,
# 转发形成的就会是:http://localhost:8001/
location ~ /api1 {
rewrite ^/api1/(.*)$ /$1 break;
proxy_set_header test001 $host:$server_port;
proxy_set_header test002 $remote_addr;
proxy_pass http://localhost:8001;
}
#server只能是ip+端口,不然启动报错
upstream api{
server localhost:9001;
server localhost:9002;
server localhost:9003;
}
#proxy_pass里面的api对应的是upstream后面的api
location /api/ {
proxy_pass http://api/;
}
#本机host配置
127.0.0.1 www.test001.com
127.0.0.1 www.test002.com
127.0.0.1 www.test003.com
server {
#监听端口
listen 80;
#服务名
server_name www.test001.com;
location / {
root html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:9001/;
}
#500类型错误处理
error_page 500 502 503 504 /50x.html;
#映射文件50x.html
location = /50x.html {
#相对路径
root html;
}
}
server {
#监听端口
listen 80;
#服务名
server_name www.test002.com;
location / {
root html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:9002/;
}
#500类型错误处理
error_page 500 502 503 504 /50x.html;
#映射文件50x.html
location = /50x.html {
#相对路径
root html;
}
}
if($request_method !~ ^(GET|POST)$ ){
return 403;
}
if( $request_uri ~* \.\.[\\\\/] ){
return 404;
}
#当写127.0.0.1的时候,使用localhost会报500,只能使用127.0.0.1
if ( $host !~ ^127.0.0.1 ){
return 500;
}
# 如果请求的是:http://localhost:80/api1,
# 转发形成的就会是:http://localhost:8001/api1
location /api1 {
proxy_pass http://localhost:8001;
}
# 如果请求的是:http://localhost:80/api1,
# 转发形成的就会是:http://localhost:8001/
location /api1/ {
proxy_pass http://localhost:8001/;
}
# 如果请求的是:http://localhost:80/api1,
# 转发形成的就会是:http://localhost:8001//
location /api1 {
proxy_pass http://localhost:8001/;
}
#proxy_pass最多只能写到端口
#比如http://localhost:9001/,多个/报错
#比如http://localhost:9001/test,多个/test报错
#所以正则表达式的转发经常配合rewrite使用
location ~ /api1 {
proxy_pass http://localhost:9001;
}
# $host为127.0.0.1
# $remote_addr为127.0.0.1
http://127.0.0.1/api/test
# $host为www.test001.com
# $remote_addr为127.0.0.1
http://www.test001.com/api/test
# $host为localhost
# $remote_addr为127.0.0.1
http://localhost/api/test
# 假设我本机ip为192.168.1.27
# $host为www.baidu.com
# $remote_addr为192.168.1.27
https://www.baidu.com/s?wd=北京
rewrite < regex > < replacement > [flag]
regex:正则表达式
replacement :跳转后的内容
flag:rewrite支持的flag标记
flag标记说明 | |
---|---|
标记 | 说明 |
last | 相当于Apache的【L】标记,表示完成rewrite |
break | 本条规则匹配完成即终止,不在匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址,爬虫不会更新url |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url |
ast和break比较 | ||
---|---|---|
last | break | |
使用场景 | 一般写在server和if中 | 一般使用在location中 |
URL匹配 | 不重质重写后的url匹配 | 终止重写后的url匹配 |
#$1为(.*)
rewrite ^/api1/(.*)$ /$1 break;
#$1为前面的(.*),$2为后面的(.*)
rewrite ^/(.*)/(.*)$ /$1 break;
#当正则表达式和请求不匹配的时候,后面的/$1将不被执行,请求不会被rewrite替换
rewrite ^/apitest/(.*)$ /$1 break;
logging:
level:
org.springframework: debug
logging:
level:
org:
springframework:
web:
servlet:
mvc:
method:
annotation:
RequestMappingHandlerMapping: trace
HttpServletRequest request;
Enumeration<String> enumeration= request.getHeaderNames();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。