溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nginx中的兩個(gè)模塊的proxy_pass的區(qū)別是什么

發(fā)布時(shí)間:2021-11-29 13:35:22 來(lái)源:億速云 閱讀:214 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“nginx中的兩個(gè)模塊的proxy_pass的區(qū)別是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

1.ngx_stream_proxy_module模塊的proxy_pass指令

只能在server段使用使用, 只需要提供域名或ip地址和端口。

可以理解為端口轉(zhuǎn)發(fā),可以是tcp端口,也可以是udp端口。

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;

2.ngx_http_proxy_module模塊的proxy_pass指令

  • 需要在location段,location中的if段,limit_except段中使用,

  • 處理需要提供域名或ip地址和端口外,還需要提供協(xié)議,如"http"或"https",

  • 還有一個(gè)可選的uri可以配置。

3.proxy_pass后,后端服務(wù)器的url(request_uri)情況分析

server {
    listen      80;
    server_name www.test.com;
 
    # 情形A
    # 訪問(wèn) http://www.test.com/testa/aaaa
    # 后端的request_uri為: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    
    # 情形B
    # 訪問(wèn) 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是錯(cuò)誤的
    #
    # nginx -t 時(shí),會(huì)報(bào)如下錯(cuò)誤: 
    #
    # 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
    # 
    # 當(dāng)location為正則表達(dá)式時(shí),proxy_pass 不能包含URI部分。本例中包含了"/"
    location ~ /testd {
        proxy_pass http://127.0.0.1:8801/;   # 記住,location為正則表達(dá)式時(shí),不能這樣寫?。?!
    }
 
    # 情形E
    # 訪問(wèn) 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
    # 訪問(wèn) 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
    # 訪問(wèn) 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;
}

“nginx中的兩個(gè)模塊的proxy_pass的區(qū)別是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI