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

Ubuntu系统下Nginx虚拟主机example.com配置失效问题求助

问题:Ubuntu 22.04配置Nginx虚拟主机后访问仍显示原站点

在Ubuntu 22.04系统上为example.com配置了Nginx虚拟主机,但访问该域名时仍显示原站点内容,请求排查解决。


系统与版本信息

  • Ubuntu版本:
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:    22.04
Codename:   jammy
  • Nginx版本:nginx/1.18.0 (Ubuntu)

相关配置文件

虚拟主机配置文件 /etc/nginx/sites-enabled/example.com

server {
    listen 80;
    listen [::]:80;

    server_name example.com;

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

    location / {
        try_files $uri $uri/ =404;
    }
}

Nginx主配置文件 /etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

排查与解决步骤

  1. 检查配置文件语法
    执行命令验证Nginx配置是否存在语法错误:

    sudo nginx -t
    

    若提示错误则修正配置;显示test is successful则继续下一步。

  2. 重启Nginx服务
    配置修改后必须重启Nginx才能生效:

    sudo systemctl restart nginx
    
  3. 确认域名解析
    确保example.com已正确解析到服务器IP,本地执行以下命令验证:

    ping example.com
    

    或使用nslookup/dig工具确认解析结果为服务器公网IP。

  4. 禁用默认站点
    Ubuntu默认Nginx站点/etc/nginx/sites-enabled/default可能仍在承接请求,禁用它:

    sudo unlink /etc/nginx/sites-enabled/default
    

    执行后重启Nginx。

  5. 修复文件权限
    确保Nginx运行用户www-data能读取站点目录及文件:

    sudo chown -R www-data:www-data /var/www/example.com
    sudo chmod -R 755 /var/www/example.com
    
  6. 验证请求匹配
    查看Nginx访问日志,确认请求是否被目标虚拟主机处理:

    sudo tail -f /var/log/nginx/access.log
    

    访问example.com后,观察日志中server_name字段是否为example.com,若不是则说明请求未匹配到目标配置。

  7. 清除浏览器缓存
    浏览器可能缓存旧页面,使用Ctrl+Shift+R强制刷新,或用无痕模式访问。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 17:50:44