本篇內(nèi)容主要講解“nginx反向代理配置如何去除前綴”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“nginx反向代理配置如何去除前綴”吧!
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)拉孜,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
使用nginx做反向代理的時(shí)候,可以簡(jiǎn)單的直接把請(qǐng)求原封不動(dòng)的轉(zhuǎn)發(fā)給下一個(gè)服務(wù)。設(shè)置proxy_pass請(qǐng)求只會(huì)替換域名,如果要根據(jù)不同的url后綴來(lái)訪問(wèn)不同的服務(wù),則需要通過(guò)如下方法:
server { listen 8000; server_name abc.com; access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1g" main; location ^~/user/ { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-nginx-proxy true; proxy_pass http://user/; } location ^~/order/ { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-nginx-proxy true; proxy_pass http://order/; } }
^~/user/表示匹配前綴是user的請(qǐng)求,proxy_pass的結(jié)尾有/, 則會(huì)把/user/*后面的路徑直接拼接到后面,即移除user。
upstream user { server localhost:8089 weight=5; } upstream order { server localhost:8090 weight=5; } server { listen 80; server_name abc.com; access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1g" main; location ^~/user/ { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-nginx-proxy true; rewrite ^/user/(.*)$ /$1 break; proxy_pass http://user; } location ^~/order/ { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-nginx-proxy true; rewrite ^/order/(.*)$ /$1 break; proxy_pass http://order; } }
proxy_pass結(jié)尾沒(méi)有/, rewrite重寫了url。
到此,相信大家對(duì)“nginx反向代理配置如何去除前綴”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!