nginx重定向问题解决(rewrite or internal redirection cycle)

柔光的暖阳◎ 2024-03-26 23:49 200阅读 0赞

文章目录

  • 错误日志和配置文件
  • 问题分析

错误日志和配置文件

访问日志文件

  1. 2023/10/15 07:13:48 [error] 30#30: *1 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 123.55.159.97, server: server_name, request: "GET / HTTP/1.1", host: "xxx.xxx.xxx.xxx"
  2. 123.55.159.97 - - [15/Oct/2023:07:13:48 +0000] "GET / HTTP/1.1" 500 579 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46"
  3. 123.55.159.97 - - [15/Oct/2023:07:13:48 +0000] "GET /favicon.ico HTTP/1.1" 500 579 "http:///" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46"
  4. 2023/10/15 07:13:48 [error] 30#30: *2 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 123.55.159.97, server: server_name, request: "GET /favicon.ico HTTP/1.1", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/"

nginx.conf配置文件内容

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include mime.types;
  6. default_type application/octet-stream;
  7. sendfile on;
  8. keepalive_timeout 65;
  9. client_max_body_size 50m;
  10. client_body_buffer_size 10m;
  11. client_header_timeout 1m;
  12. client_body_timeout 1m;
  13. gzip on;
  14. gzip_min_length 1k;
  15. gzip_buffers 4 16k;
  16. gzip_comp_level 4;
  17. gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  18. gzip_vary on;
  19. server {
  20. listen 80;
  21. server_name my_server_name;
  22. location / {
  23. root /usr/local/xxx_vue;
  24. index index.html index.htm;
  25. try_files $uri $uri/ /index.html;
  26. }
  27. location ^~ /api/ {
  28. proxy_pass http://xxx.xxx.xxx.xxx:8080/;
  29. proxy_set_header Host $host;
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. }
  33. }
  34. }

问题分析

  • 根据提供的访问日志和nginx.conf配置文件分析,是循环重定向的问题。
  • 配置中,location / 块使用了 try_files $uri $uri/ /index.html; 会导致导致在尝试访问根目录时发生重定向循环。
  • 修改:添加一个新的location=/index块,直接提供 /index.html 而不进行重定向。

    server {

    1. listen 80;
    2. server_name my_server_name;
    3. location / {
    4. root /usr/local/xxx_vue;
    5. index index.html index.htm;
    6. try_files $uri $uri/ /index.html;
    7. }
    8. location = /index.html {
    9. root /usr/local/xxx_vue;
    10. }
    11. location ^~ /api/ {
    12. proxy_pass http://xxx.xxx.xxx.xxx:8080/;
    13. proxy_set_header Host $host;
    14. proxy_set_header X-Real-IP $remote_addr;
    15. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    16. }

    }

  • 使用以下命令重新加载Nginx,即可生效

    1. sudo nginx -s reload

发表评论

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

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

相关阅读

    相关 定向302】Redirect

    介绍 URL 重定向,也称为 URL 转发,是一种当实际资源,如单个页面、表单或者整个 Web 应用被迁移到新的 URL 下的时候,保持(原有)链接可用的技术。HTTP