nginx proxy_redirect的作用及說(shuō)明
準(zhǔn)備環(huán)境
springboot /redirect controller
curl http://localhost:10080/redirect -vv * Trying ::1:10080... * Connected to localhost (::1) port 10080 (#0) > GET /redirect HTTP/1.1 > Host: localhost:10080 > User-Agent: curl/7.76.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 302 < X-Content-Type-Options: nosniff < X-XSS-Protection: 1; mode=block < Cache-Control: no-cache, no-store, max-age=0, must-revalidate < Pragma: no-cache < Expires: 0 < X-Frame-Options: DENY < Location: http://localhost:10080/ip < Content-Length: 0 < Date: Wed, 26 May 2021 05:55:45 GMT < * Connection #0 to host localhost left intact
1. 默認(rèn)配置
即proxy_redirect default;
location / {
proxy_pass http://localhost:10080/;
}效果
curl -vv http://localhost/redirect * Trying ::1:80... * connect to ::1 port 80 failed: 拒絕連接 * Trying 127.0.0.1:80... * Connected to localhost (127.0.0.1) port 80 (#0) > GET /redirect HTTP/1.1 > Host: localhost > User-Agent: curl/7.76.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 302 < Server: nginx/1.19.3 < Date: Wed, 26 May 2021 05:57:02 GMT < Content-Length: 0 < Location: http://localhost/ip < Connection: keep-alive < X-Content-Type-Options: nosniff < X-XSS-Protection: 1; mode=block < Cache-Control: no-cache, no-store, max-age=0, must-revalidate < Pragma: no-cache < Expires: 0 < X-Frame-Options: DENY < * Connection #0 to host localhost left intact
2. proxy_redirect default;
location / {
proxy_pass http://localhost:10080/;
proxy_redirect default;
}注意,proxy_redirect default必須在proxy_pass下方配置
效果
curl -vv http://localhost/redirect * Trying ::1:80... * connect to ::1 port 80 failed: 拒絕連接 * Trying 127.0.0.1:80... * Connected to localhost (127.0.0.1) port 80 (#0) > GET /redirect HTTP/1.1 > Host: localhost > User-Agent: curl/7.76.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 302 < Server: nginx/1.19.3 < Date: Wed, 26 May 2021 06:02:20 GMT < Content-Length: 0 < Location: http://localhost/ip < Connection: keep-alive < X-Content-Type-Options: nosniff < X-XSS-Protection: 1; mode=block < Cache-Control: no-cache, no-store, max-age=0, must-revalidate < Pragma: no-cache < Expires: 0 < X-Frame-Options: DENY < * Connection #0 to host localhost left intact
與方案1等同
3. proxy_redirect off;
location / {
proxy_pass http://localhost:10080/;
proxy_redirect off;
}效果
curl http://localhost:10080/redirect -vv * Trying ::1:10080... * Connected to localhost (::1) port 10080 (#0) > GET /redirect HTTP/1.1 > Host: localhost:10080 > User-Agent: curl/7.76.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 302 < X-Content-Type-Options: nosniff < X-XSS-Protection: 1; mode=block < Cache-Control: no-cache, no-store, max-age=0, must-revalidate < Pragma: no-cache < Expires: 0 < X-Frame-Options: DENY < Location: http://localhost:10080/ip < Content-Length: 0 < Date: Wed, 26 May 2021 06:03:34 GMT < * Connection #0 to host localhost left intact
可以看到,302響應(yīng)的是內(nèi)部地址。
總結(jié)
常規(guī)部署情況下,不需要特別配置proxy_redirect或者配置proxy_redirect default即可。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx 禁止訪問(wèn)某個(gè)目錄或文件的設(shè)置方法
如果基于WEB根目錄下,要禁止用戶訪問(wèn)/config目錄,或者要禁止用戶訪問(wèn)/config.ini(ZF常用INI,不過(guò)建議還是放到WEB目錄以外的地方),可以通過(guò)location進(jìn)行配置,返回403或者404等2010-09-09
Nginx配置http和https的實(shí)現(xiàn)步驟
Nginx是使用最多的代理服務(wù)器之一,本文主要介紹了Nginx配置http和https的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用
這篇文章主要介紹了詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用,包括ngx_http_headers_module與它的增強(qiáng)版ngx_headers_more的配置使用講解,需要的朋友可以參考下2016-01-01
Nginx反向代理中出現(xiàn)502錯(cuò)誤的解決步驟
反向代理是一種服務(wù)器代理的方式,它代理了客戶端的請(qǐng)求并將請(qǐng)求轉(zhuǎn)發(fā)給后端服務(wù)器,然后將后端服務(wù)器的響應(yīng)返回給客戶端,但經(jīng)常會(huì)遇到502錯(cuò)誤,所以本文給大家介紹了Nginx反向代理中出現(xiàn)502錯(cuò)誤的解決步驟,需要的朋友可以參考下2025-03-03
nginx打印請(qǐng)求頭日志方法(親測(cè)可用)
之前想用nginx打印收到的請(qǐng)求的請(qǐng)求頭,但是只找到打印請(qǐng)求體的,沒(méi)有打印請(qǐng)求頭的,本文就來(lái)介紹一下nginx打印請(qǐng)求頭日志方法,感興趣的可以了解一下2023-11-11
Nginx服務(wù)器中414錯(cuò)誤和504錯(cuò)誤的配置解決方法
這篇文章主要介紹了Nginx服務(wù)器中414錯(cuò)誤和504錯(cuò)誤的配置解決方法,分別對(duì)應(yīng)Request-URI Too Large和Gateway Time-out這樣的錯(cuò)誤提示,需要的朋友可以參考下2015-12-12
Nginx設(shè)置https和http同時(shí)使用同一個(gè)端口訪問(wèn)
本文主要介紹了Nginx通過(guò)8070端口同時(shí)支持HTTP和HTTPS的配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05

