nginx的proxy_pass路径转发规则浅析(末尾/问题)
源地址 :
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路径转发规则浅析(末尾/问题)的更多相关文章
- nginx的proxy_pass路径转发规则最后带/问题
一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...
- Nginx配置proxy_pass转发的/路径问题
Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...
- 用haproxy实现nginx的proxy_pass转发功能
公司的网站有个需求,主站点上有两个URL,没有在本地nginx上配置,而是在另一台主机的nginx上配置的站点.如果使用nginx作为反向代理,可以使用proxy_pass指令转发对这两个URL的请求 ...
- Nginx location 和 proxy_pass路径配置详解
目录 一.Nginx location 基本配置 1.1.Nginx 配置文件 1.2 .Python 脚本 二.测试 2.1.测试 location 末尾存在 / 和 proxy_pass末尾存在 ...
- Nginx配置location及rewrite规则
Nginx配置location及rewrite规则 示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } loca ...
- Nginx配置proxy_pass
nginx配置proxy_pass,需要注意转发的路径配置 1.location /test/ { proxy_pass http://t6:8300; } 2.location /test/ { p ...
- Nginx 常用全局变量 及Rewrite规则详解
每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...
- nginx 之 proxy_pass详解
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径:如果没有/,表示相对路径,把匹配的路径部分也给代理走. 假设下面四种情况分别用 h ...
- nginx 之 proxy_pass
nginx中有两个模块都有proxy_pass指令 ngx_http_proxy_module的proxy_pass 语法: proxy_pass URL; 场景: location, if in l ...
随机推荐
- CountDownLatch、CyclicBarrier及Semaphore的用法示例
一.参考blog https://www.cnblogs.com/dolphin0520/p/3920397.html 二.CountDownLatch 个人把它类比于一个持有计数的闸门,每到达这个闸 ...
- 服务端怎样暴露IBinder接口对象
服务端怎样暴露IBinder接口对象: package com.example.mydownload; import android.app.Service; import android.conte ...
- mysql 存储引擎对索引的支持
一.首先给出mysql官方文档给出的不同存储引擎对索引的支持 从上面的图中可以得知,mysql 是支持hash索引的,但支持和不支持又和具体的存储引擎有关系.从图中看到InnoDB是支持Btree索引 ...
- Flink source task 源码分析
http://vinoyang.com/2016/05/05/flink-stream-source/ http://vinoyang.com/2016/12/28/flink-runtime-com ...
- 【iCore4 双核心板_FPGA】例程十五:基于单口RAM的ARM+FPGA数据存取实验
实验现象: 写RAM命令格式:write:地址(0-255),数据(0-65535)\cr\lf 读RAM命令格式:read:地址(0-255)\cr\lf 核心代码: int main(void) ...
- URLEncoder 和URLDecoder
通常在字符串的编码转换上,可以使用这两个类: public static void main(String[] args) { String str = "你好吗?我很好!"; t ...
- 安装 Xshell 5/6 时出现.dll以及0xc000007错误的解决
安装 Xshell 5/6 时出现.dll以及0xc000007错误的解决 问题:缺少 mfc110.dll或者是其他.dll文件以及应用程序运行错误,如下所示. 方法: 一种是网上直接下载.(缺少. ...
- 关于C#中的动态数组ArrayList
在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组. 一.ArrayList类与Array类的区别 ArrayList类实 ...
- Apache重写规则由浅入深剖析.htaccess
1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体 地怎样接待它,就是此文件 ...
- [React] 01 - Intro: javaScript library for building user interfaces
教学视频: http://www.php.cn/code/8217.html React 教程: http://www.runoob.com/react/react-tutorial.html 本篇是 ...