源地址 :

https://www.zifangsky.cn/917.html

一 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的任何配置

nginx的proxy_pass路径转发规则浅析(末尾/问题)的更多相关文章

  1. nginx的proxy_pass路径转发规则最后带/问题

    一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...

  2. Nginx配置proxy_pass转发的/路径问题

    Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...

  3. 用haproxy实现nginx的proxy_pass转发功能

    公司的网站有个需求,主站点上有两个URL,没有在本地nginx上配置,而是在另一台主机的nginx上配置的站点.如果使用nginx作为反向代理,可以使用proxy_pass指令转发对这两个URL的请求 ...

  4. Nginx location 和 proxy_pass路径配置详解

    目录 一.Nginx location 基本配置 1.1.Nginx 配置文件 1.2 .Python 脚本 二.测试 2.1.测试 location 末尾存在 / 和 proxy_pass末尾存在 ...

  5. Nginx配置location及rewrite规则

    Nginx配置location及rewrite规则 示例: location  = / {   # 精确匹配 / ,主机名后面不能带任何字符串   [ configuration A ] } loca ...

  6. Nginx配置proxy_pass

    nginx配置proxy_pass,需要注意转发的路径配置 1.location /test/ { proxy_pass http://t6:8300; } 2.location /test/ { p ...

  7. Nginx 常用全局变量 及Rewrite规则详解

    每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...

  8. nginx 之 proxy_pass详解

    在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径:如果没有/,表示相对路径,把匹配的路径部分也给代理走.     假设下面四种情况分别用 h ...

  9. nginx 之 proxy_pass

    nginx中有两个模块都有proxy_pass指令 ngx_http_proxy_module的proxy_pass 语法: proxy_pass URL; 场景: location, if in l ...

随机推荐

  1. 在 Redis 上实现的分布式锁

    由于近排很忙,忙各种事情,还有工作上的项目,已经超过一个月没写博客了,确实有点惭愧啊,没能每天或者至少每周坚持写一篇博客.这一个月里面接触到很多新知识,同时也遇到很多技术上的难点,在这我将对每一个有用 ...

  2. ES6,Array.find()和findIndex()函数的用法

    ES6为Array增加了find(),findIndex函数. find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined. findIndex()函数也是查找目标元素,找到就返 ...

  3. Series 入门(创建和增删改查)

    Series 是pandas两大数据结构中(DataFrame,Series)的一种.使用pandas 前需要将pandas 模块引入,因为Series和DataFrame用的次数非常多,所以将其引入 ...

  4. Mysql系列五:数据库分库分表中间件mycat的安装和mycat配置详解

    一.mycat的安装 环境准备:准备一台虚拟机192.168.152.128 1. 下载mycat cd /softwarewget http:-linux.tar.gz 2. 解压mycat tar ...

  5. Java如何检查文件是否在服务器上被修改了?

    在Java编程中,如何检查文件是否在服务器上被修改了? 以下示例显示如何检查文件是否在服务器上进行了修改. package com.yiibai; import java.net.URL; impor ...

  6. 开源分布式日志系统ExceptionLess部署杂乱笔记 加密

    前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wiki .NET 4.6.1 这个因为我装了VS2015,就没有 ...

  7. alibaba的FastJson(高性能JSON开发包) json转换

    http://www.oschina.net/code/snippet_228315_35122 class User{ private int id; private String name; pu ...

  8. 一台PC双网卡,一个外网一个内网

    问题:一台PC双网卡,一个连外网一个连内网.用户主要访问外网,内网只访问有限的几个ip.因为外网很大,一般人公司内网常访问的ip是有限的几个. 现在如何做到在上外网的同时也能访问内网的系统?明明两个网 ...

  9. [React] 07 - Flux: uni-flow for react

    相关资源 Ref: [Android Module] 03 - Software Design and Architecture Ref: Flux 架构入门教程 Ref: 详解React Flux架 ...

  10. js中的try/catch

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...