`
dxm1986
  • 浏览: 429893 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

nginx proxy_pass location 为正则表达式的问题

阅读更多

 


http://blog.helosa.org/2010/02/10/nginx-proxy_pass.html 
在nginx中配置proxy_pass时,如果是按照^~匹配路径时

要注意proxy_pass后的url最后的/

当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走

如果没有/,则会把匹配的路径部分也给代理走

01.location ^~ /static_js/ 
02.{ 
03.proxy_cache js_cache; 
04.proxy_set_header Host js.test.com; 
05.proxy_pass http://js.test.com/
06.}

如上面的配置,如果请求的url是http://servername/static_js/test.html 
会被代理成http://js.test.com/test.html 
而如果这么配置:

01.location ^~ /static_js/ 
02.{ 
03.proxy_cache js_cache; 
04.proxy_set_header Host js.test.com; 
05.proxy_pass  http://js.test.com
06.} 

则会被代理到http://js.test.com/static_js/test.htm 
当然,我们可以用如下的rewrite来实现/的功能 

location ^~ /static_js/ 

proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
rewrite /static_js/(.+)$ /$1 break; 
proxy_pass http://js.test.com; 

见配置,摘自nginx.conf 里的server 段:

server {
listen 80;
server_name abc.163.com ;
location / {
proxy_pass http://ent.163.com/ ;
}
location /star/ {
proxy_pass http://ent.163.com ;
}
}


里面有两个location,我先说第一个,/ 。其实这里有两种写法,分别是:



location / {
proxy_pass http://ent.163.com/ ;
}


location / {
proxy_pass http://ent.163.com ;
}


出来的效果都一样的。



第二个location,/star/。同样两种写法都有,都出来的结果,就不一样了。



location /star/ {
proxy_pass http://ent.163.com ;
}


当访问 http://abc.163.com/star/ 的时候,nginx 会代理访问到 http://ent.163.com/star/ ,并返回给我们。



location /star/ {
proxy_pass http://ent.163.com/ ;
}


当访问 http://abc.163.com/star/ 的时候,nginx 会代理访问到 http://ent.163.com/ ,并返回给我们。



这两段配置,分别在于, proxy_pass http://ent.163.com/ ; 这个”/”,令到出来的结果完全不同。



前者,相当于告诉nginx,我这个location,是代理访问到http://ent.163.com 这个server的,我的location是什么,nginx 就把location 加在proxy_pass 的 server 后面,这里是/star/,所以就相当于 http://ent.163.com/star/。如果是location /blog/ ,就是代理访问到 http://ent.163.com/blog/。



后者,相当于告诉nginx,我这个location,是代理访问到http://ent.163.com/的,http://abc.163.com/star/ == http://ent.163.com/ ,可以这样理解。改变location,并不能改变返回的内容,返回的内容始终是http://ent.163.com/ 。 如果是location /blog/ ,那就是 http://abc.163.com/blog/ == http://ent.163.com/ 。



这样,也可以解释了上面那个location / 的例子,/ 嘛,加在server 的后面,仍然是 / ,所以,两种写法出来的结果是一样的。



PS: 如果是 location ~* ^/start/(.*)\.html 这种正则的location,是不能写”/”上去的,nginx -t 也会报错的了。因为,路径都需要正则匹配了嘛,并不是一个相对固定的locatin了,必然要代理到一个server。

 

分享到:
评论

相关推荐

    详解Nginx proxy_pass的一个/斜杠引发的血案

    一个nginx的server模块下需要proxy到两个server,所以就通过location的不同路径来区分转发到不同的服务器上。 一开始是这么写的 location / { proxy_pass http://server1/; } location /index { proxy_pass ...

    Nginx之proxy_redirect使用详解

    今天在做nginx反向代理apache的时候出了一点点问题,原来后端apache用的端口是8080通过反向代理后,使用wireshark抓包发现location头域数值为http://192.168.1.154:8080/wuman/  如果把这个返回给客户端肯定是不...

    Nginx Location 正则_NginxLocation正则.md_

    nginx正则表达式. : 匹配除换行符以外的任意字符? : 重复0次或1次+ : 重复1次或更多次* : 重复0次或更多次\d :匹配数字^ : 匹配字符串的开始$ : 匹配字符串的介绍{n} : 重复n次{n} : 重复n次或更多次[c] :...

    45 - Nginx中的正则表达式.mp4

    正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。 许多程序设计语言都支持...

    nginx_tcp_proxy_module-master.zip

    nginx_tcp_proxy_module-master.zip

    nginx 反向代理之 proxy_pass的实现

    格式很简单: proxy_pass URL; 其中URL包含:传输协议(http://, https://等)、主机名(域名或者IP:PORT)、uri。 示例如下: proxy_pass http://www.xxx.com/; proxy_pass http://192.168.200.101:8080/uri; ...

    记一次nginx中proxy_pass的使用问题

    最近排查一个web服务的问题,webserver使用的nginx,最终发现是踩了nginx中proxy_pass的一个坑,这里记录下来。 踩坑经过 一个线上的http服务,示例nginx关键配置如下: server { listen 80; server_name ligang....

    nginx location中多个if里面proxy_pass的方法

    1、首先我们回顾一下nginx中location的相关知识 1)location的匹配指令: ~ #波浪线表示执行一个正则匹配,区分大小写 ~* #表示执行一个正则匹配,不区分大小写 ^~ #^~表示普通字符匹配,不是正则匹配。如果该...

    nginx proxy_pass反向代理配置中url后加不加/的区别介绍

    而在日常的web网站部署中,经常会用到nginx的proxy_pass反向代理,有一个配置需要弄清楚:配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,...

    nginx配置、proxy_passfast 和 cgi_pass区别,反向代理,负载均衡、nginx+fastcgi+php的并发阻塞问题

    2、反向代理proxy_pass 2.1、反向代理 2.2、反向代理-使用upstream 2.3、反向代理-负载均衡-轮循模式 2.4、反向代理-负载均衡-权重模式 2.4、反向代理-负载均衡-ip_hash方式 3、网关代理fastcgi_pass(php+nginx配合...

    nginx location 配置 正则表达式实例详解

    1.location 介绍 •location 是在 server 块中配置,用来通过匹配接收的uri来实现分类处理不同的请求,如反向代理,取静态文件等 •location 在 server 块中可以有多个,且是有顺序的,会被第一个匹配的 location ...

    nginx配置proxy_pass中url末尾带/与不带/的区别详解

    注意:当location为正则表达式匹配模式时,proxy_pass中的url末尾是不允许有”/”的,因此正则表达式匹配模式不在讨论范围内。  proxy_pass配置中url末尾带/时,nginx转发时,会将原uri去除location匹配表达式后的...

    nginx proxy_pass指令’/’使用注意事项

    1. proxy_pass配置说明 不带/ 代码如下: location /test/ { proxy_pass http://t6:8300;  } 带/ 代码如下: location /test/  {   proxy_pass http://t6:8300/;   } 上面两种配置,区别只在于proxy_pass...

    yangpeng14#DevOps#详解Nginx-proxy_pass使用1

    前言日常不管是研发还是运维,都多少会使用Nginx服务,很多情况Nginx用于反向代理,那就离不开使用proxy_pass,有些同学会对 proxy_pass

    nginx_tcp_proxy_module-master

    nginx_tcp_proxy_module-master 是nginx的TCP模块

    ngx_http_proxy_connect_module.zip

    备用nginx代理模块

Global site tag (gtag.js) - Google Analytics