nginx proxy_redirect 作用

左手的ㄟ右手 2022-10-16 01:56 604阅读 0赞

准备环境 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即可。

发表评论

表情:
评论列表 (有 0 条评论,604人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx sendfile什么作用

    nginx sendfile什么作用 由于访问日志文件增长速度非常快,日志太大会严重影响服务器效率。同时,为了方便对日志进行分析计算,须要对日志文件进行定时切割。定时切割

    相关 Nginx try_files 作用

    Nginx的try\_files作用:按选项所指定的顺序去检查用户请求的文件是否存在,如果本地存在的话则返回该请求;不存在的话将该请求转发到指定的其他路径 location