Ubuntu 18虚拟机中Nginx无法在your_domain.com提供服务的问题
问题排查:Ubuntu 18虚拟机Nginx域名访问失败
现象
Chrome提示「无法访问此网站,your_domain.com的服务器IP地址无法找到」,执行traceroute your_domain.com返回traceroute: unknown host your_domain.com,相同配置在macOS可正常运行。
1. 修复/etc/hosts格式错误
你的/etc/hosts存在格式问题:同一域名重复配置、注释写法不符合规范,会导致域名解析失败。修改为如下格式:
127.0.0.1 localhost # 本地测试:将域名指向虚拟机自身 127.0.0.1 your_domain.com www.your_domain.com # 若需指向外部IP,仅保留一行即可(注释掉其他重复项) # 35.188.213.229 your_domain.com www.your_domain.com # 10.128.0.48 your_domain.com www.your_domain.com
注意:hosts每行仅能包含一组IP+域名,注释需以
#开头单独标注。
2. 验证Nginx配置有效性
检查Nginx配置是否存在语法错误:
sudo nginx -t
若输出test is successful,重启Nginx生效:
sudo systemctl restart nginx
若报错,根据提示修正配置文件。
3. 排查域名解析问题
- 测试本地解析是否生效:
正常应返回你配置的IP(如127.0.0.1)。ping your_domain.com - 若ping失败,刷新DNS缓存:
sudo systemctl restart systemd-resolved - 验证虚拟机网络连通性:执行
ping google.com,确认网络本身无问题。
4. 检查防火墙规则
Ubuntu 18默认使用ufw,确认80端口已开放:
sudo ufw status
若80端口未开放,执行以下命令开放:
sudo ufw allow 80/tcp sudo ufw reload
5. 验证Nginx服务状态
若直接访问虚拟机IP(如http://10.128.0.48)也无法打开页面,检查Nginx运行状态:
sudo systemctl status nginx
若服务未启动,执行sudo systemctl start nginx启动,查看报错日志定位问题:
cat /var/log/nginx/error.log
内容的提问来源于stack exchange,提问作者sinwon
相关产品推荐
相关产品推荐

