翼度科技»论坛 云主机 服务器技术 查看内容

nginx proxy_redirect的作用及说明

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
准备环境

springboot /redirect controller
  1. curl http://localhost:10080/redirect -vv
  2. *   Trying ::1:10080...
  3. * Connected to localhost (::1) port 10080 (#0)
  4. > GET /redirect HTTP/1.1
  5. > Host: localhost:10080
  6. > User-Agent: curl/7.76.1
  7. > Accept: */*
  8. >
  9. * Mark bundle as not supporting multiuse
  10. < HTTP/1.1 302
  11. < X-Content-Type-Options: nosniff
  12. < X-XSS-Protection: 1; mode=block
  13. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  14. < Pragma: no-cache
  15. < Expires: 0
  16. < X-Frame-Options: DENY
  17. < Location: http://localhost:10080/ip
  18. < Content-Length: 0
  19. < Date: Wed, 26 May 2021 05:55:45 GMT
  20. <
  21. * Connection #0 to host localhost left intact
复制代码
1. 默认配置

即proxy_redirect default;
  1. location / {
  2.                 proxy_pass http://localhost:10080/;
  3.         }
复制代码
效果
  1. curl -vv http://localhost/redirect
  2. *   Trying ::1:80...
  3. * connect to ::1 port 80 failed: 拒绝连接
  4. *   Trying 127.0.0.1:80...
  5. * Connected to localhost (127.0.0.1) port 80 (#0)
  6. > GET /redirect HTTP/1.1
  7. > Host: localhost
  8. > User-Agent: curl/7.76.1
  9. > Accept: */*
  10. >
  11. * Mark bundle as not supporting multiuse
  12. < HTTP/1.1 302
  13. < Server: nginx/1.19.3
  14. < Date: Wed, 26 May 2021 05:57:02 GMT
  15. < Content-Length: 0
  16. < Location: http://localhost/ip
  17. < Connection: keep-alive
  18. < X-Content-Type-Options: nosniff
  19. < X-XSS-Protection: 1; mode=block
  20. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  21. < Pragma: no-cache
  22. < Expires: 0
  23. < X-Frame-Options: DENY
  24. <
  25. * Connection #0 to host localhost left intact
复制代码
2. proxy_redirect default;
  1. location / {
  2.                 proxy_pass http://localhost:10080/;
  3.                 proxy_redirect default;
  4.         }
复制代码
注意,proxy_redirect default必须在proxy_pass下方配置
效果
  1. curl -vv http://localhost/redirect
  2. *   Trying ::1:80...
  3. * connect to ::1 port 80 failed: 拒绝连接
  4. *   Trying 127.0.0.1:80...
  5. * Connected to localhost (127.0.0.1) port 80 (#0)
  6. > GET /redirect HTTP/1.1
  7. > Host: localhost
  8. > User-Agent: curl/7.76.1
  9. > Accept: */*
  10. >
  11. * Mark bundle as not supporting multiuse
  12. < HTTP/1.1 302
  13. < Server: nginx/1.19.3
  14. < Date: Wed, 26 May 2021 06:02:20 GMT
  15. < Content-Length: 0
  16. < Location: http://localhost/ip
  17. < Connection: keep-alive
  18. < X-Content-Type-Options: nosniff
  19. < X-XSS-Protection: 1; mode=block
  20. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  21. < Pragma: no-cache
  22. < Expires: 0
  23. < X-Frame-Options: DENY
  24. <
  25. * Connection #0 to host localhost left intact
复制代码
与方案1等同

3. proxy_redirect off;
  1. location / {
  2.                 proxy_pass http://localhost:10080/;
  3.                 proxy_redirect off;
  4.         }
复制代码
效果
  1. curl http://localhost:10080/redirect -vv
  2. *   Trying ::1:10080...
  3. * Connected to localhost (::1) port 10080 (#0)
  4. > GET /redirect HTTP/1.1
  5. > Host: localhost:10080
  6. > User-Agent: curl/7.76.1
  7. > Accept: */*
  8. >
  9. * Mark bundle as not supporting multiuse
  10. < HTTP/1.1 302
  11. < X-Content-Type-Options: nosniff
  12. < X-XSS-Protection: 1; mode=block
  13. < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  14. < Pragma: no-cache
  15. < Expires: 0
  16. < X-Frame-Options: DENY
  17. < Location: http://localhost:10080/ip
  18. < Content-Length: 0
  19. < Date: Wed, 26 May 2021 06:03:34 GMT
  20. <
  21. * Connection #0 to host localhost left intact
复制代码
可以看到,302响应的是内部地址。

总结

常规部署情况下,不需要特别配置proxy_redirect或者配置proxy_redirect default即可。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

来源:https://www.jb51.net/server/322220u21.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具