nginx的proxy_pass路径转发规则浅析末尾/问题

一 location匹配路径末尾没有 /

此时proxy_pass后面的路径必须拼接location的路径:

 

1

2

3

4

5

6

7

8

location /sta

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/sta;

}

 

  • 外面访问:http://192.168.1.30/sta/sta1.html
  • 相当于访问:http://192.168.1.31/sta/sta1.html

注:这里也可以写成:“proxy_pass http://192.168.1.31/sta/;”。当然,不推荐使用上面这种写法

二 location匹配路径末尾有 /

此时proxy_pass后面的路径需要分为以下四种情况讨论:

(1)proxy_pass后面的路径只有域名且最后没有 /:

 

 

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31;

}

 

  • 外面访问:http://192.168.1.30/sta/sta1.html
  • 相当于访问:http://192.168.1.31/sta/sta1.html

(2)proxy_pass后面的路径只有域名同时最后有 /:

 

 

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/;

}

 

  • 外面访问:http://192.168.1.30/sta/sta1.html
  • 相当于访问:http://192.168.1.31/sta1.html

(3)proxy_pass后面的路径还有其他路径但是最后没有 /:

 

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc;

}

 

  • 外面访问:http://192.168.1.30/sta/sta1.html
  • 相当于访问:http://192.168.1.31/abcsta1.html

(4)proxy_pass后面的路径还有其他路径同时最后有 /:

 

 

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc/;

}

 

  • 外面访问:http://192.168.1.30/sta/sta1.html
  • 相当于访问:http://192.168.1.31/abc/sta1.html

附:在Nginx上面配置APK文件下载路径:

 

1

2

3

4

5

6

7

8

9

location ^~ /h5/appdownload/

{

      proxy_redirect off;

      proxy_set_header        Host $host;

      proxy_set_header        X-Real-IP $remote_addr;

      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://192.168.1.31/;

      proxy_set_header   Cookie $http_cookie;

}

 

  • 外面访问:http://test.com/h5/appdownload/Demo_1.0.0.apk
  • 相当于访问:http://192.168.1.31/Demo_1.0.0.apk

每次更新apk文件,只需要上传新的apk文件到192.168.1.31服务器,然后再更新对外的下载地址为http://test.com/h5/appdownload/newName.apk即可,并不需要更改Nginx的任何配置

原文地址:https://blog.csdn.net/thlzjfefe" target="_blank" rel="noopener" title="thlzjfefe">thlzjfefe</a> <img class="article-time-img article-heard-img" src="https://csdnimg.cn/release/blo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录tree -L 2 ./一、conf 目录conf 目录:存放nginx 配置文件的目录fastcgi.conf:存放fastcgi 相关的配置 fastcgi.conf.default:fastcgi.conf 的原始备份文件,用于还原 fastcgi_params:fastcgi 相关参数文件 fastcgi_params.default:fastcgi_params 的原始...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "imagename eq nginx.exe"
Nginx显示500错误原因和解决方法
linux系统下启停nginx的命令
nginx 的 default_server 指令可以定义默认的 server出处理一些没有成功匹配 server_name 的请求1.显示定义2.指定Server_name3.隐式定义这三种方式都可禁止 ip 直接访问且 1,3同时可以禁止未绑定域名的访问(比如泛解析了主域名)。如果没有显式定义,则会选取第一个定义的 server 作为 default_server。http {# 显示的定义一个 default serverserver {}}
Nginx是一款轻量级的 Web 服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。
一、 什么是负载均衡?什么是负载均衡?记得第一次接触 Nginx 是在实验室,那时候在服务器部署网站需要用 Nginx 。Nginx 是一个服务组件,用来反向代理、负载平衡和 HTTP 缓存等。那么这里的 负载均衡 是什么?负载均衡(LB,Load Balance),是一种技术解决方案。用来在多个资源(一般是服务器)中分配负载,达到最优化资源使用,避免过载。资源,相当于每个服务实例的执行操作单元,负载均衡就是将大量的数据处理操作分摊到多个操作单元进行执行,用来解决互联网分布式系统..
Nginx 的 HttpUpstreamModule 提供对后端(backend)服务器的简单负载均衡。一个最简单的 upstream 写法如下:upstream backend { server backend1.example.com; server backend2.example.com; server.backend3.example.com;}ser...
一、alias location   /view {       alias /opt/view;        index  index.html index.htm;       access_log on;       expires 30d;  }  alias 是目录别名的意思,指的是绝对路径。因此按照以上配置的话,如果请求路径是 /view/hello.html,...
现在假设有三台主机,他们的ip分别为:A: 192.168.1.167B: 192.168.1.168C: 192.168.1.169nginx作为代理服务器部署在主机 A 上面,B 和 C 作为两台应用服务器。现在想实现通过A访问B和C,有以下两种方式:一、通过不同的listen实现对B和C的访问,实现方式如下:在nginx.conf中添加两个serverserver {...
Nginx作为一个轻量级的HTTP服务器,相比Apache优势也是比较明显的,在性能上它占用资源少,能支持更高更多的并发连接,从而达到提高访问效率;在功能上它是一款非常优秀的代理服务器与负载均衡服务器;在安装配置上它安装,配置都比较简单。关于Nginx部署、配置的文章公众号已经发布过很多:深度总结|深入浅出NginxHTTP服务器Nginx服务介绍续Nginx优化配置详解1...
原文连接:https://www.cnblogs.com/coder-yoyo/p/6346595.html nginx配置location总结location匹配顺序&quot;=&quot;前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) 正则表达式指令匹配,按照配置文件里的顺序,成功就停止其他匹...
location语法:location [=|~|~*|^~] /uri/ { … } 默认:否上下文:server这个指令随URL不同而接受不同的结构。你可以配置使用常规字符串和正则表达式。如果使用正则表达式,你必须使用 ~* 前缀选择不区分大小写的匹配或者 ~ 选择区分大小写的匹配。确定 哪个location 指令匹配一个特定指令,常规字符串第一个测试。常规字符串匹配请求的开始...
Nginx中,set $para $1,$1表示路径中正则表达式匹配的第一个参数。以下是一个示例,用以实验$1,$2。如: location ~/abc/(.*)/(.*) { set $para1 $1 set $para2 $2 content_by_lua_block { ngx.say(ngx.var...
假如在域名b.com下,有一个html页面test.html,访问路径为:http://b.com/test.html;如果要防止别人在iframe下访问该页面,则可以通过nginx配置实现。举例如下:现有页面a.html,http://a.com/a.html,该页面有一个iframe,src=http://b.com/test.html&lt;!DOCTYPE html&gt;...
一 location匹配路径末尾没有 /此时proxy_pass后面的路径必须拼接location的路径: 1 2 3 4 5 6 7 8 location /sta { proxy_redirect off; proxy_set_headerHos...
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。 也许你听过以上关于Nginx的美妙的事情,您可能已经很喜欢它了,正在考虑如何提高Nginx服务...
在需要真实的流量做旁路测试的时候,我们就可以使用 nginx 来做流量的拷贝1 Nginx Mirror 模块nginx 自1.13.4 版本开始,自带一个ngx_http_mirror_module,使用起来非常简单,只要在需要 mirror 的 location 处添加 mirror 关键字就行Copy 123456789 location / { mirror /mirror; proxy_pass http://ba.
nginx作为静态服务器时,可以提高访问的速度,默认配置下,nginx响应头中会返回:Etag、Last-Modified等信息。这样客户端(比如浏览器)再次访问时,时常会携带请求头:If-Modified-Since,此值为上次响应头中Last-Modified对应的值,那么nginx在收到资源请求时,将会比较If-Modified-Since的值,是否与文件最后的修改时间相等,如果相等就返回304,否则返回200。 所以,如果让nginx每次否返回200,则需要将相应的响应头去掉,即...
原文连接:https://www.jianshu.com/p/0850db5af284$host变量的官方解释$host:in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name...