| 作者 | 時間 |
|---|---|
| 雨中星辰 | 2023-10-16 |
需求說明
需要用nginx代理一個應(yīng)用的請求,但是該沒有一個統(tǒng)一的url前綴。
就需要把:http://nginx ip/service-gateway/的請求轉(zhuǎn)發(fā)到http://10.35.13.191:9095/
例如:
http://nginx ip/service-gateway/index.html轉(zhuǎn)發(fā)后為http://10.35.13.191:9095/index.html
http://nginx ip/service-gateway/api/list轉(zhuǎn)發(fā)后為http://10.35.13.191:9095/api/list
nginx配置
location /service-gateway {
proxy_pass http://10.35.13.191:9095/;
}
上面的nginx配置中的location /service-gateway指定了一個URL路徑前綴為/service-gateway的位置塊。當(dāng)nginx收到請求時,如果請求的URL以/service-gateway開頭,就會將該請求轉(zhuǎn)發(fā)到http://10.35.13.191:9095/
具體解釋如下:
location /service-gateway: 這里指定了要匹配的URL路徑前綴為/service-gateway。也就是說,當(dāng)請求的URL以/service-gateway開始時,將會應(yīng)用下面的配置。
-
proxy_pass http://10.35.13.191:9095/: 這里使用了proxy_pass指令來將請求轉(zhuǎn)發(fā)到指定的后端服務(wù)器。http://10.35.13.191:9095/是代理目標(biāo)的地址。/符號表示將請求的URL保持不變,并且在轉(zhuǎn)發(fā)時不附加路徑前綴。
- 例如,當(dāng) nginx 收到請求 /service-gateway/hello 時,它會將請求轉(zhuǎn)發(fā)到 http://10.35.13.191:9095/hello。原始的 /service-gateway 路徑前綴將被忽略,只會將 /hello 部分轉(zhuǎn)發(fā)到后端服務(wù)器。
這樣配置后,當(dāng)請求http://nginx ip/service-gateway/hello時,nginx會將該請求轉(zhuǎn)發(fā)到http://10.35.13.191:9095/hello,并且在轉(zhuǎn)發(fā)時不再附加/service-gateway前綴。