参考一 :proxy_pass

1.location和proxy_pass都带/,则真实地址不带location匹配目录

location /api/ {
	proxy_pass http://127.0.0.1:8080/;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/upload

2.location不带/,proxy_pass带/,则真实地址会带/

location /api {
	proxy_pass http://127.0.0.1:8080/;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080//upload

3.location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/

location /api/ {
	proxy_pass http://127.0.0.1:8080;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/api/upload

4.location和proxy_pass都不带/,则真实地址会带location匹配目录/api/

location /api {
	proxy_pass http://127.0.0.1:8080;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/api/upload

5.同1,但 proxy_pass带地址

location /api/ {
	proxy_pass http://127.0.0.1:8080/server/;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/server/upload

6.同2,但 proxy_pass带地址,则真实地址会多个/

location /api {
	proxy_pass http://127.0.0.1:8080/server/;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/server//upload

7.同3,但 proxy_pass带地址,则真实地址会直接连起来

location /api/ {
	proxy_pass http://127.0.0.1:8080/server;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/serverupload

8.同4,但 proxy_pass带地址,则真实地址匹配地址会替换location匹配目录

location /api {
	proxy_pass http://127.0.0.1:8080/server;
}
  • 访问地址: www.test.com/api/upload >> http://127.0.0.1:8080/server/upload

总结

  1. proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录
  2. proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部

参考二:其他方式

一 Nginx的location语法

location [ = | ~ | ~* | ^~ |@ ] uri{...}

符号 解释
= 严格匹配。如果请求匹配这个location,那么将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
~* 不区分大小写匹配(可用正则表达式)
!~ 区分大小写不匹配
!~* 不区分大小写不匹配
^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式
  • 示例1:location / { }

    匹配任意请求

  • 示例2: location ~* .(gif|jpg|jpeg) {rewrite.(gif|jpg|jpeg)/logo.png;}

    区分大小写匹配任何以gif、jpg、jpeg结尾的请求,并将该请求重定向到 /logo.png请求

  • 示例3:location ~ ^.+.txt$ { root /usr/local/nginx/html/;}

    区分大小写匹配以.txt结尾的请求,并设置此location的路径是/usr/local/nginx/html/。也就是以.txt结尾的请求将访问/usr/local/nginx/html/ 路径下的txt文件

二 alias与root的区别

  • root 实际访问文件路径会拼接URL中的路径
  • alias 实际访问文件路径不会拼接URL中的路径
location ^~ /sta/ { 
	alias /usr/local/nginx/html/static/;
}
  • 访问地址: http://test.com/sta/sta1.html >> /usr/local/nginx/html/static/sta1.html
location ^~ /tea/ {
	root /usr/local/nginx/html/;
}
  • 访问地址: http://test.com/tea/tea1.html >> /usr/local/nginx/html/tea/tea1.html

三 last 和 break关键字的区别

  1. last 和 break 当出现在location 之外时,两者的作用是一致的没有任何差异
  2. ast 和 break 当出现在location 内部时:
  • last 使用了last 指令,rewrite 后会跳出location 作用域,重新开始再走一次刚才的行为
  • break 使用了break 指令,rewrite后不会跳出location 作用域,它的生命也在这个location中终结

四 permanent 和 redirect关键字的区别

  • rewrite … permanent 永久性重定向,请求日志中的状态码为301
  • rewrite … redirect 临时重定向,请求日志中的状态码为302

五 综合实例

将符合某个正则表达式的URL重定向到一个固定页面

比如:我们需要将符合“/test/(\d+)/[\w-.]+” 这个正则表达式的URL重定向到一个固定的页面。符合这个正则表达式的页面可能是:http://test.com/test/12345/abc122.htmlhttp://test.com/test/456/11111cccc.js

从上面的介绍可以看出,这里可以使用rewrite重定向或者alias关键字来达到我们的目的。因此,这里可以这样做:

  1. 使用rewrite关键字:
location ~ ^.+.txt  { 
	rewrite ^/test/(\d+)/[\w-.]+$ /testpage.txt last;
}

这里将所有符合条件的URL(PS:不区分大小写)都重定向到/testpage.txt请求,也就是 /usr/local/nginx/html/testpage.txt 文件

  1. 使用alias关键字:
location ~* ^/test/(\d+)/[\w-.]+$ { 
	alias /usr/local/nginx/html/static/sta1.html;
}

这里将所有符合条件的URL(PS:不区分大小写)都重定向到/usr/local/nginx/html/static/sta1.html 文件