You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx中子域名chat.example.com访问时总是重定向至www.example.com的问题排查

问题原因及解决办法

核心问题:子域名配置语法错误

你的/etc/nginx/conf.d/chat.conf缺少server {开头标记,导致Nginx无法识别这个子域名的server块。访问chat.example.com时,Nginx找不到匹配的配置,就会使用端口80上的默认server(即nginx.conf里监听0.0.0.0:80的www.example.com相关配置),从而触发重定向。

修复步骤

  1. 修正chat.conf的语法错误
    编辑/etc/nginx/conf.d/chat.conf,在开头添加server {,修正后的完整配置如下:

    server {
        listen       80;
        server_name  chat.example.com;
        #rewrite ^(.*)$ https://${server_name}$1 permanent;
    
        location / {
                client_max_body_size 1m;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_read_timeout 3600;
                proxy_pass http://127.0.0.1:8001;
        }
    }
    
  2. 验证配置合法性
    执行命令测试Nginx配置是否正确:

    nginx -t
    

    若输出nginx: configuration file /etc/nginx/nginx.conf test is successful,说明配置无语法问题。

  3. 重启或重载Nginx服务
    使修改后的配置生效:

    # 重载配置(不中断现有服务,推荐)
    nginx -s reload
    # 或直接重启服务
    systemctl restart nginx
    
  4. 额外排查(可选)

    • 若之前开启过HTTPS重定向(注释的return 301规则),可通过curl -L chat.example.com/chat跟踪重定向路径,或清除客户端缓存避免旧规则影响。
    • 确认后端服务http://127.0.0.1:8001正常运行,排除后端服务自身导致的重定向问题。

内容的提问来源于stack exchange,提问作者Dolphin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:27:34