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服务端代理配置_第1张图片

文章参考:https://blog.csdn.net/yeguxin/article/details/94020476

你可能感兴趣的:(Linux)