• Nginx proxy_pass 详解


    Nginx proxy_pass 详解

    原文地址:https://www.jianshu.com/p/b010c9302cd0

    nginx 中配置 proxy_pass 代理转发时,如果在 proxy_pass 后面的 url 加 /,表示绝对根路径;如果没有 /,表示相对路径,把匹配的路径部分也给代理走。

    1. 多种配置访问验证

    • 假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

    👉 第一种

    location /proxy/ {
    	proxy_pass http://127.0.0.1/;
    }
    # 代理到URL:http://127.0.0.1/test.html
    
    • 1
    • 2
    • 3
    • 4

    👉 第二种

    相对于第一种,最后少了一个 /

    location /proxy/ {
    	proxy_pass http://127.0.0.1;
    }
    # 代理到URL:http://127.0.0.1/proxy/test.html
    
    • 1
    • 2
    • 3
    • 4

    👉 第三种

    location /proxy/ {
    	proxy_pass http://127.0.0.1/aaa/;
    }
    # 代理到URL:http://127.0.0.1/aaa/test.html
    
    • 1
    • 2
    • 3
    • 4

    👉 第四种

    相对于第三种,最后少一个 /

    location /proxy/ {
    	proxy_pass http://192.168.137.103/aa;
    }
    # 看网上博客文章说的是代理到 URL:http://127.0.0.1/aaatest.html,实际测试结果有点差异,后期在看原因
    
    • 1
    • 2
    • 3
    • 4

    2. 两个 proxy_pass 关系和区别

    ngx_stream_proxy_module ngx_http_proxy_module 两个模块中都有 proxy_pass 参数,两个proxy_pass都是用来做后端代理的指令。

    • ngx_stream_proxy_module模块的proxy_pass指令只能在 server 段使用使用, 只需要提供域名或ip地址和端口。可以理解为端口转发,可以是 tcp 端口,也可以是 udp 端口。
    • ngx_http_proxy_module模块的proxy_pass指令需要在 location 段,location中的if段,limit_except段中使用,处理需要提供域名或ip地址和端口外,还需要提供协议,如"http"或"https",还有一个可选的uri可以配置。

    3. proxy_pass 具体用法

    ngx_stream_proxy_module模块的proxy_pass指令

    server {
        listen 127.0.0.1:12345;
        proxy_pass 127.0.0.1:8080;
    }
     
    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 1m;
        proxy_pass example.com:12345;
    }
     
    server {
        listen 53 udp;
        proxy_responses 1;
        proxy_timeout 20s;
        proxy_pass dns.example.com:53;
    }
     
    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ngx_http_proxy_module模块的proxy_pass指令

    server {
        listen      80;
        server_name www.test.com;
     
        # 正常代理,不修改后端url的
        location /some/path/ {
            proxy_pass http://127.0.0.1;
        }
     
        # 修改后端url地址的代理(本例后端地址中,最后带了一个斜线)
        location /testb {
            proxy_pass http://www.other.com:8801/;
        }
     
        # 使用 if in location
        location /google {
            if ( $geoip_country_code ~ (RU|CN) ) {
                proxy_pass http://www.google.hk;
            }
        }
     
        location /yongfu/ {
            # 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/
            proxy_pass http://unix:/tmp/backend.socket:/uri/;;
     
            # 匹配到请求方法为: PUT or DELETE, 代理到9080
            limit_except PUT DELETE {
                proxy_pass http://127.0.0.1:9080;
            }
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    4. proxy_pass后,后端服务器的url(request_uri)情况分析

    server {
        listen      80;
        server_name www.test.com;
     
        # 情形A
        # 访问 http://www.test.com/testa/aaaa
        # 后端的request_uri为: /testa/aaaa
        location ^~ /testa/ {
            proxy_pass http://127.0.0.1:8801;
        }
        
        # 情形B
        # 访问 http://www.test.com/testb/bbbb
        # 后端的request_uri为: /bbbb
        location ^~ /testb/ {
            proxy_pass http://127.0.0.1:8801/;
        }
     
        # 情形C
        # 下面这段location是正确的
        location ~ /testc {
            proxy_pass http://127.0.0.1:8801;
        }
     
        # 情形D
        # 下面这段location是错误的
        #
        # nginx -t 时,会报如下错误: 
        #
        # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular 
        # expression, or inside named location, or inside "if" statement, or inside 
        # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
        # 
        # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
        location ~ /testd {
            proxy_pass http://127.0.0.1:8801/;   # 记住,location为正则表达式时,不能这样写!!!
        }
     
        # 情形E
        # 访问 http://www.test.com/ccc/bbbb
        # 后端的request_uri为: /aaa/ccc/bbbb
        location /ccc/ {
            proxy_pass http://127.0.0.1:8801/aaa$request_uri;
        }
     
        # 情形F
        # 访问 http://www.test.com/namea/ddd
        # 后端的request_uri为: /yongfu?namea=ddd
        location /namea/ {
            rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
            proxy_pass http://127.0.0.1:8801;
        }
     
        # 情形G
        # 访问 http://www.test.com/nameb/eee
        # 后端的request_uri为: /yongfu?nameb=eee
        location /nameb/ {
            rewrite    /nameb/([^/]+) /yongfu?nameb=$1 break;
            proxy_pass http://127.0.0.1:8801/;
        }
     
        access_log /data/logs/www/www.test.com.log;
    }
     
    server {
        listen      8801;
        server_name www.test.com;
        
        root        /data/www/test;
        index       index.php index.html;
     
        rewrite ^(.*)$ /test.php?u=$1 last;
     
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
     
        access_log /data/logs/www/www.test.com.8801.log;
    }
     
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    文件: /data/www/test/test.php

    通过查看 $_SERVER['REQUEST_URI'] 的值,我们可以看到每次请求的后端的 request_uri 的值,进行验证

    
    echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];
    
    • 1
    • 2

    5. 总结

    情形A和情形B进行对比,可以知道proxy_pass后带一个URI,可以是斜杠(/)也可以是其他uri,对后端request_uri变量的影响。

    情形D说明,当location为正则表达式时,proxy_pass不能包含URI部分。

    情形E通过变量($request_uri, 也可以是其他变量),对后端的request_uri进行改写。

    情形F和情形G通过rewrite配合break标志,对url进行改写,并改写后端的request_uri。需要注意,proxy_pass地址的URI部分在情形G中无效,不管如何设置,都会被忽略。

     
     
     
     
     

  • 相关阅读:
    ChatGPT基本原理
    30.10.2 认证插件更新
    学校官网首页 2页网页设计(HTML+CSS+JavaScript)
    一体化预制泵站在农田灌溉中的优势
    【数据结构与算法】递归全流程详细剖析 | 详解图的深度优先遍历
    Codeforces Round #835 (Div. 4) C. Advantage
    localforage-本地存储的优化方案
    使用 HTTP Client 轻松进行 API 测试
    web3-引言之读取账户地址
    Milvus 2.3.功能全面升级,核心组件再升级,超低延迟、高准确度、MMap一触开启数据处理量翻倍、支持GPU使用!
  • 原文地址:https://blog.csdn.net/D1179869625/article/details/133222973