nginx 之 proxy_pass


nginx中有兩個模塊都有proxy_pass指令

ngx_http_proxy_moduleproxy_pass

語法: proxy_pass URL;
場景: location, if in location, limit_except
說明: 設置后端代理服務器的協議(protocol)和地址(address),以及location中可以匹配的一個可選的URI。協議可以是"http""https"。地址可以是一個域名或ip地址和端口,或者一個 unix-domain socket 路徑。  
詳見官方文檔: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

 ngx_stream_proxy_moduleproxy_pass

語法: proxy_pass address;
場景: server
說明: 設置后端代理服務器的地址。這個地址(address)可以是一個域名或ip地址和端口,或者一個 unix-domain socket路徑。  
詳見官方文檔: http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass

兩個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可以配置。

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; #設置源站地址
}

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;
        }
    }

}

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;
}

小結

情形A和情形B進行對比,可以知道proxy_pass后帶一個URI,可以是斜杠(/)也可以是其他uri,對后端request_uri變量的影響。
情形D說明,當location為正則表達式時,proxy_pass不能包含URI部分(proxy_pass在以下情況下,指令中不能有URI,正則表達式位置、命名的地點、if 塊),
情形E通過變量($request_uri, 也可以是其他變量),對后端的request_uri進行改寫。
情形F和情形G通過rewrite配合break標志,對url進行改寫,並改寫后端的request_uri。需要注意,proxy_pass地址的URI部分在情形G中無效,不管如何設置,都會被忽略。

 相關文檔:ngx_stream_proxy_module

     nginx中文文檔-ngx_stream_proxy_module

     NGINX反向代理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM