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

Nginx反向代理多子域名配置失败求助(Debian环境)

搞定Nginx多子域名反向代理的配置问题

兄弟,你这重启失败的问题根本不用折腾MAC和IP,完全是配置文件里的一个低级拼写错误搞的鬼!

先揪出核心错误

你修改配置的时候,把Nginx的指令server_name写成了servername(少了中间的下划线),Nginx认不出这个错误指令,自然启动失败。

正确配置多子域名的两种方式

根据你的需求,给你两种靠谱的配置方案:

方案1:同一Server块搞定多个子域名(代理到同一后端)

如果两个子域名都要反向代理到同一个http://192.168.XX.YY,直接在server_name后面用空格把多个域名分开就行,不用重复写指令:

server {
    listen 80;
    # 空格分隔多个子域名,一次性搞定
    server_name firstsubdomain.domain.tld secondsubdomain.domain.tld;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_pass http://192.168.XX.YY;
    }
}

方案2:分开配置Server块(灵活适配不同需求)

要是以后两个子域名需要代理到不同后端,或者要设置不同的日志、路由规则,那就分开写两个独立的server块:

# 第一个子域名的配置
server {
    listen 80;
    server_name firstsubdomain.domain.tld;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_pass http://192.168.XX.YY;
    }
}

# 第二个子域名的配置
server {
    listen 80;
    server_name secondsubdomain.domain.tld;
    access_log /var/log/nginx/second_sub_access.log; # 单独日志方便排查
    error_log /var/log/nginx/second_sub_error.log;

    location / {
        proxy_pass http://192.168.AA.BB; # 可以指向不同后端
    }
}

必做的验证步骤!

每次改完Nginx配置,先别着急重启,先跑个命令检查语法是否正确:

nginx -t

如果输出nginx: configuration file /etc/nginx/nginx.conf test is successful,说明配置没问题,再重启服务:

service nginx restart

最后说一句,你之前换MAC和IP完全没必要,这个问题纯纯是配置语法错误,和网络硬件信息没关系~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:25:00