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

文章目录

错误日志和配置文件

访问日志文件

bash 复制代码
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"
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"
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"
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配置文件内容

json 复制代码
events {
	worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

client_max_body_size     50m;
client_body_buffer_size  10m; 
client_header_timeout    1m;
client_body_timeout      1m;

gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_comp_level  4;
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;
gzip_vary on;
server {
listen       80;
server_name  my_server_name;

    location / {		
        root   /usr/local/xxx_vue;
        index  index.html index.htm; 
        try_files $uri $uri/ /index.html;	
    }
		
location ^~ /api/ {		
        proxy_pass http://xxx.xxx.xxx.xxx:8080/;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;						
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
	
}
}

问题分析

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

    location / {
        root /usr/local/xxx_vue;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location = /index.html {
        root /usr/local/xxx_vue;
    }

    location ^~ /api/ {
        proxy_pass http://xxx.xxx.xxx.xxx:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  • 使用以下命令重新加载Nginx,即可生效

    bash 复制代码
    sudo nginx -s reload
相关推荐
叽里咕噜怪3 小时前
Pod的详解与进阶
运维·容器·kubernetes
ONLYOFFICE3 小时前
入门指南:远程运行 ONLYOFFICE 协作空间 MCP 服务器
运维·服务器·github·onlyoffice
行初心3 小时前
uos基础 autostart 设置程序开机自启动
运维
Dovis(誓平步青云)3 小时前
《Linux 核心 IO 模型深析(中篇):探索Cmake与多路转接的高效实现poll》
linux·运维·服务器·数据库·csdn成长记录
韦东东3 小时前
行业资讯日报自动化:从采集到 LLM 生成的全链路拆解(以政务网站为例)
运维·人工智能·自动化·大模型·llm·政务·行业资讯
tianyuanwo3 小时前
TERM变量迷思:从Jenkins节点连接差异看终端仿真与构建系统的微妙关系
运维·ssh·jenkins·java web·term
一勺菠萝丶3 小时前
Jenkins 打包显示 SUCCESS 但产物不全?日志出现 Killed 的排查与解决(小白版)
运维·jenkins
tyatyatya3 小时前
Ansible自动化配置,从入门到实战
运维·自动化·ansible
Anakki3 小时前
企业级 Elastic Stack 集成架构:Spring Boot 3.x 与 Elasticsearch 8.x 深度实践指南
运维·jenkins·springboot·elastic search
DevOps-IT4 小时前
HTTP状态码(常见 HTTP Status Code 查询)
运维·服务器·网络·网络协议·http