nginx如何通过proxy_pass设置反向代理,隐藏端口号

 更新时间:2024年01月25日 14:40:11   作者:Homilier  
这篇文章主要介绍了nginx如何通过proxy_pass设置反向代理,隐藏端口号方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

通过proxy_pass设置反向代理,隐藏端口号

nginx配置修改,通过 proxy_pass 设置反向代理,监听域名(IP)转发到指定端口。

server
    {
        listen 80;
        server_name www.xxx.com;
 
        server_name_in_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        
        location / {
            proxy_pass http://www.xxx.com:8978;
        }
    }

nginx proxy_pass的配置

Nginx的官网将proxy_pass分为两种类型:

  • 不带URI方式:只包含IP和端口号的,不带uri(单个/也算uri),比如proxy_pass http://localhost:8080
  • 带URI方式:在端口号之后有其他路径的,包含了只有单个/的如proxy_pass http://localhost:8080/,以及其他路径,比如proxy_pass http://localhost:8080/abc

URL末尾存在 uri 

处理逻辑:

代理请求时,会先将请求的uri中和location匹配的部分替换成 proxy_pass 指定的uri,再将最终的uri拼接到代理地址,才是最终访问的url

如:

location /proxy {
	proxy_pass http://127.0.0.1:8099/svr1;  # uri为'/svr1'
}

发送如下请求:http://localhost:8088/proxy/index.html

详细解析:

  • 请求的uri:/proxy/index.html
  • location匹配的部分:/proxy
  • proxy_pass 指定的uri:/svr1
  • 最终的uri:/svr1/index.html (将请求的uri中和location匹配的部分替换成 proxy_pass 指定的uri)
  • 代理地址:http://127.0.0.1:8099
  • 最终访问的url:http://127.0.0.1:8099/svr1/index.html
  • 即访问 http://localhost:8088/proxy/index.html,
  • 实际请求路径为 http://127.0.0.1:8099/svr1/index.html 

URL末尾不存在 uri

处理逻辑:

代理请求时,直接将请求的uri拼接到代理地址,就是最终访问的url

如:

location /proxy2 {
	proxy_pass http://127.0.0.1:8099;  # 无uri
}

发送如下请求:http://localhost:8088/proxy2/index.html  

详细解析:  

  • 请求的uri:/proxy2/index.html
  • 代理地址:http://127.0.0.1:8099
  • 最终访问的url:http://127.0.0.1:8099/proxy2/index.html
  • 即访问 http://localhost:8088/proxy2/index.html,
  • 实际请求路径为 http://127.0.0.1:8099/proxy2/index.html

下面的几个例子加深理解

server {
   listen       80;
   server_name  localhost;
 
   location /api1/ {
           proxy_pass http://localhost:8080;
        }
   # http://localhost/api1/xxx -> http://localhost:8080/api1/xxx
 
 
   location /api2/ {
           proxy_pass http://localhost:8080/;
        }
   # http://localhost/api2/xxx -> http://localhost:8080/xxx
 
 
   location /api3 {
           proxy_pass http://localhost:8080;
        }
   # http://localhost/api3/xxx -> http://localhost:8080/api3/xxx
 
 
   location /api4 {
           proxy_pass http://localhost:8080/;
        }
   # http://localhost/api4/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。
 
 
   location /api5/ {
           proxy_pass http://localhost:8080/haha;
        }
   # http://localhost/api5/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。
 
   location /api6/ {
           proxy_pass http://localhost:8080/haha/;
        }
   # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx
 
   location /api7 {
           proxy_pass http://localhost:8080/haha;
        }
   # http://localhost/api7/xxx -> http://localhost:8080/haha/xxx
 
   location /api8 {
           proxy_pass http://localhost:8080/haha/;
        }
  # http://localhost/api8/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Nginx监控模块(vts模块)详解

    Nginx监控模块(vts模块)详解

    国内用Nginx的比较多,下面这篇文章主要给大家介绍了关于Nginx监控模块(vts模块)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • 详解Nginx服务器之负载均衡策略(6种)

    详解Nginx服务器之负载均衡策略(6种)

    这篇文章主要介绍了详解Nginx服务器之负载均衡策略(6种),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • nginx目录路径重定向的方法

    nginx目录路径重定向的方法

    这篇文章主要介绍了nginx目录路径重定向的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • nginx rewrite 实现URL跳转的方法

    nginx rewrite 实现URL跳转的方法

    今天小编就为大家分享一篇nginx rewrite 实现URL跳转的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Nginx中部署Angular项目遇到的坑巨坑

    Nginx中部署Angular项目遇到的坑巨坑

    这篇文章主要介绍了Nginx中部署Angular项目遇到的坑巨坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 通过nginx反向代理来调试代码的实现

    通过nginx反向代理来调试代码的实现

    这篇文章主要介绍了通过nginx反向代理来调试代码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Nginx实现动态内容缓存的示例代码

    Nginx实现动态内容缓存的示例代码

    在Nginx中实现动态内容的缓存可以显著提高性能,减少后端服务器的负载,本文就来介绍一下Nginx动态内容缓存实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-11-11
  • nginx location中多个if里面proxy_pass的方法

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

    这篇文章主要介绍了nginx location中多个if里面proxy_pass的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Nginx处理请求时的匹配规则详析

    Nginx处理请求时的匹配规则详析

    这篇文章主要给大家介绍了关于Nginx处理请求时的匹配规则的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-11-11
  • 如何在centos上使用yum安装rabbitmq-server

    如何在centos上使用yum安装rabbitmq-server

    这篇文章主要介绍了如何在centos上使用yum安装rabbitmq-server,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09

最新评论