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

NGINX子域名跳转至主域名问题求助

NGINX多站点子域名跳转问题解决

问题诊断

你的子域名radio.example.com访问时跳转到主域名,核心问题出在配置的几个关键错误上:

错误点分析

  1. 子域名配置的强制跳转拦截:子域名server块最顶部的return 301 $scheme://radio.example.com$request_uri;会直接触发跳转,导致后续所有站点逻辑完全不执行。
  2. server_name_in_redirect 配置冲突:开启server_name_in_redirect on会让NGINX在跳转时强制用配置的server_name替换请求的Host头,容易导致子域名请求被替换为主域名。
  3. HTTP/HTTPS逻辑混杂:原配置将HTTP和HTTPS监听放在同一个server块中,用if判断跳转,逻辑冗余且容易引发匹配问题。
  4. 重复的fastcgi_params引入:两次include fastcgi_params可能导致参数覆盖,引发PHP解析异常。

修正后的配置文件

主站(example.com)配置

# 单独处理HTTP请求跳转至HTTPS
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name www.example.com example.com;
    return 301 https://$host$request_uri;
}

# HTTPS主站配置
server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    ssl_certificate /etc/ssl/certs/example.com.pem;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    server_name www.example.com example.com;
    server_name_in_redirect off;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    root /var/www/html/example.com;
    index index.php index.html index.htm;

    client_max_body_size 512M;
    client_body_timeout 300s;

    location ~ \.php$ {
        fastcgi_buffers 64 4K;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        try_files $fastcgi_script_name =404;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;

        fastcgi_index index.php;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

子域名(radio.example.com)配置

# 单独处理HTTP请求跳转至HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name radio.example.com;
    return 301 https://$host$request_uri;
}

# HTTPS子域名配置
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl_certificate /etc/ssl/certs/example.com.pem;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    server_name radio.example.com;
    server_name_in_redirect off;

    access_log /var/log/nginx/libretime.access.log;
    error_log /var/log/nginx/libretime.error.log;

    root /usr/share/libretime/legacy/public;
    index index.php index.html index.htm;

    client_max_body_size 512M;
    client_body_timeout 300s;

    location ~ \.php$ {
        fastcgi_buffers 64 4K;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        try_files $fastcgi_script_name =404;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;

        fastcgi_index index.php;
        fastcgi_pass unix:/run/libretime-legacy.sock;
    }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ ^/api/(v2|browser) {
        include proxy_params;
        proxy_redirect off;
        proxy_pass http://unix:/run/libretime-api.sock;
    }
}

操作步骤

  1. 备份原配置文件,避免丢失原有设置
  2. 将上述配置替换对应的文件
  3. 执行nginx -t验证配置语法正确性
  4. 执行systemctl reload nginx重载NGINX配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:45:29