nginx服务端代理配置

我们开发的时候时候i经常会遇到跨域问题,一般情况下都是后端接口配置对应的请求头,允许跨域请求配置,但是有时候也需要到nginx针对不同的接口,进行不同的地址请求,现在我们就自己配置下nginx的代理。

proxy_pass   proxy_pass指令中在nginx的两个模块都有


最基本proxy_pass 用法

server {
    listen      80;
    server_name chat.paas.scorpio.uat.newtank.cn;
    
     # 转发请求到 http://www.example.com
    location / {
        proxy_pass http://www.example.com;
    }
}

第一种: 绝对路径.    proxy_pass http://127.0.0.1:8080; 后面8080没有 “/”

  server {
        listen      80;
        server_name www.test.com;
        
        # 当访问 http://test.yeguxin.top/proxy/aaa/bbb.text时,nginx匹配到 /proxy/路径,把请求转发给127.0.0.1:8080服务.
        # 实际请求代理服务器路径为 " 127.0.0.1:8080/proxy/aaa/bbb.text "
        location /proxy/ {
             proxy_pass http://127.0.0.1:8080;
        }
    }

第二种:相对路径.    proxy_pass http://127.0.0.1:8080; 后面8080有 “/”

 server {
        listen      80;
        server_name www.test.com;
        
        # 当访问 http://test.yeguxin.top/proxy/aaa/bbb.text时,nginx匹配到 /proxy/路径,把请求转发给127.0.0.1:8080服务.
        # 实际请求代理服务器路径为 " 127.0.0.1:8080/aaa/bbb.text "
        location /proxy/ {
             proxy_pass http://127.0.0.1:8080/;
        }
    }

第三种    proxy_pass http://127.0.0.1:8080/static; 后面static没有 “/”

server {
        listen      80;
        server_name www.test.com;
        
        # 当访问 http://test.yeguxin.top/proxy/aaa/bbb.text时,nginx匹配到 /proxy/路径,把请求转发给127.0.0.1:8080服务.
        # 实际请求代理服务器路径为 " 127.0.0.1:8080/staticaaa/bbb.text "
        location /proxy/ {
             proxy_pass http://127.0.0.1:8080/static;
        }
    }

第四种    proxy_pass http://127.0.0.1:8080/static; 后面static有 “/”

server {
        listen      80;
        server_name www.test.com;
        
        # 当访问 http://test.yeguxin.top/proxy/aaa/bbb.text时,nginx匹配到 /proxy/路径,把请求转发给127.0.0.1:8080服务.
        # 实际请求代理服务器路径为 " 127.0.0.1:8080/static/aaa/bbb.text "
        location /proxy/ {
             proxy_pass http://127.0.0.1:8080/static/;
        }
    }

结果:

文章参考:nginx配置域名转发到其他域名的几种方法_nginx一个域名转发到另一个域名_叶孤心丶的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值