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 ...
随机推荐
- jQuery Address全站 AJAX 完整案例详解
本文详细介绍如何利用 jQuery 框架以及 jQuery Address 插件实现最基本的全站 AJAX 动态加载页面内容的功能的方法. 案例目标 以常见基本结构的网站为案例,实现全站链接 AJAX ...
- 【转载】Mysql load data infile用法(万级数据导入,在几秒之内)
https://blog.csdn.net/u014082714/article/details/53173975 http://blog.itpub.net/26506993/viewspace-2 ...
- Spark Scheduler内部原理剖析
文章正文 通过文章“Spark 核心概念RDD”我们知道,Spark的核心是根据RDD来实现的,Spark Scheduler则为Spark核心实现的重要一环,其作用就是任务调度.Spark的任务调度 ...
- JAVA 线程池架构浅析
经历了Java内存模型.JUC基础之AQS.CAS.Lock.并发工具类.并发容器.阻塞队列.atomic类后,我们开始JUC的最后一部分:线程池.在这个部分你将了解到下面几个部分: 线程池的基础架构 ...
- C#中怎么判断一个数组中是否存在某个数组值
(1) 第一种方法: ,,}; ); // 这里的1就是你要查找的值 ) // 不存在 else // 存在 (2) 第二种方法: string[] strArr = {"a",& ...
- win7(64bit)+python3.5+pyinstaller3.2安装和测试
最近因为做项目需要,需要在win7中安装pyinstaller用于将.py文件生成脱离python平台的可执行程序*.exe文件. 安装步骤 第一步:安装python3.5 [下载python3.5的 ...
- Scala学习笔记——函数式对象
用创建一个函数式对象(类Rational)的过程来说明 类Rational是一种表示有理数(Rational number)的类 package com.scala.first /** * Creat ...
- CentOS7 yum方式安装MariaDB 10.2.13-1
注:以下步骤都是以root身份运行. 一.建立mariadb.repo 1,编辑新文件,命令:vim /etc/yum.repos.d/mariadb.repo 2,输入如下内容,保存退出 [mar ...
- duilib进阶教程 -- 在MFC中使用duilib (1)
由于入门教程的反响还不错,因此Alberl就以直播的形式来写<进阶教程>啦,本教程的前提: 1.请先阅读<仿迅雷播放器教程> 2.要有一定的duilib基础,如果还没,请先阅读 ...
- java代码执行字符串中的逻辑运算方法
转载:https://www.jb51.net/article/143967.htm 方法一:Java调用js方法执行: /** * * @author: Longjun * @Description ...