Ubuntu 17.10下Nginx虚拟主机配置失效问题求助
Hey there, let's walk through fixing this issue—virtual host misconfigurations can be tricky but usually boil down to a few common checks. Here's what to verify step by step:
1. Confirm Your hosts File Changes Are Active
First, let's make sure your hosts entry is actually being used. Sometimes DNS caches can hold onto old records, or formatting issues in the file can break resolution:
- Run
ping test.appin your terminal. You should see output showing it's resolving to127.0.0.1. If not:- Flush the system DNS cache with:
sudo systemd-resolve --flush-caches - Or restart the resolver service entirely:
sudo systemctl restart systemd-resolved
- Flush the system DNS cache with:
- Double-check your
/etc/hostsfile formatting—your current entry mergeslocalhostandtest.appon one line, which can cause inconsistencies. Split them into separate lines like this:127.0.0.1 localhost 127.0.0.1 test.app 127.0.1.1 Flashy-PC # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
2. Verify Nginx Virtual Host Configuration
Next, let's ensure your Nginx config for test.app is properly set up:
- Check if you have a config file in
/etc/nginx/sites-available/fortest.app. If not, create one with:server { listen 80; server_name test.app; # Replace this with your actual website root directory root /var/www/test.app; index index.html index.htm; location / { try_files $uri $uri/ =404; } } - Enable the site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/test.app /etc/nginx/sites-enabled/ - Critical: Test for Nginx syntax errors with:
sudo nginx -t. If you get any errors (like missing semicolons or invalid paths), fix them before proceeding.
3. Restart Nginx & Check Service Status
After fixing any config errors, restart Nginx to apply changes:
sudo systemctl restart nginx- Verify the service is running:
sudo systemctl status nginx—it should show "active (running)" in green.
4. Check Firewall Settings
Ubuntu 17.10 uses ufw by default. Make sure port 80 (HTTP) is open to allow traffic:
- Run
sudo ufw status. Look for a line like80/tcp ALLOW Anywhere - If port 80 isn't allowed, open it with:
sudo ufw allow 80/tcp
5. Clear Browser Cache
Sometimes your browser caches old DNS data or failed requests. Try accessing test.app in incognito/private mode, or clear your browser's cache and cookies to rule this out.
6. Dig Deeper with Nginx Logs
If all the above steps don't work, check Nginx's error logs to pinpoint the exact issue:
- Run
sudo tail -f /var/log/nginx/error.logand then try accessingtest.appin your browser. The log will show specific errors (like permission issues, missing files, or config mistakes) that you can address directly.
Bonus: Fix Directory Permissions
Ensure the Nginx user (www-data) has access to your website root directory:
sudo chown -R www-data:www-data /var/www/test.appsudo chmod -R 755 /var/www/test.app
内容的提问来源于stack exchange,提问作者flashydev

