nginx 的 proxy_pass使用详解

midoll 483 2022-07-05

proxy_pass使用详解:

假设访问 http://test.yeguxin.top/proxy/aaa/bbb.text

第一种: 绝对路径. 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/;
        }
    }

最基本proxy_pass 用法

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

# nginx