Question

nginx works with ip but gives 'Welcome to nginx' error with domain name

I have a Django application on an Ubuntu 22.04 droplet with Gunicorn and Nginx. I added DNS for my domain (ifbt.farm), which is resolving to my ip address (24.199.74.74). If I configure nginx with

server_name 24.199.74.74; and sudo systemctl restart nginx

It proxies the request to Gunicorn correctly so I see my application when I visit https://24.199.74.74

but if I configure it with

server_name ifbt.farm www.ifbt.farm; and sudo systemctl restart nginx

It gives the ‘Welcome to nginx’ error page when I visit https://ifbt.farm. My Django settings has

ALLOWED_HOSTS = [‘ifbt.farm’, ‘www.ifbt.farm’, ‘24.199.74.74’]

This link on stack overflow notes a similar problem, and the OP solved it by commenting out the server_name line in sites-available/default, which he said was shadowing it. I tried that, but it didn’t change anything in my case.

Thanks for any thoughts on this!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

This must have been a DNS resolution issue. About two hours after I posted this question (two days ago), it started working correctly without me making any further changes!

Thanks, Bobby for following up!

Bobby Iliev
Site Moderator
Site Moderator badge
May 20, 2023

Hi there,

It looks like that you’ve managed to get it all working! Happy to see that. If you have a chance feel free to share your solution here with the community.

In general, this issue could be due to a number of reasons. Here are some debugging steps:

  • Check Your Nginx Configurations: Make sure you have the correct settings in your Nginx configurations file for the Django application. This file is typically located in /etc/nginx/sites-available/ directory. Here is an example of what your configuration should look like:
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/your/gunicorn.sock;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Please replace /path/to/your/gunicorn.sock with the path to your Gunicorn socket file.

  • Check Your DNS Settings: Double-check your DNS settings and make sure that your domain name is properly pointing to your droplet’s IP address. There could be a delay between setting the DNS record and it taking effect due to DNS propagation.

  • Check Server Block Symbolic Link: When you create a server block file in the /etc/nginx/sites-available/ directory, you must create a symbolic link to it in the /etc/nginx/sites-enabled/ directory for Nginx to serve it. To create a symbolic link, you can use the ln command:

sudo ln -s /etc/nginx/sites-available/yourconfig /etc/nginx/sites-enabled/

Replace yourconfig with the name of your Nginx configuration file.

  • Check Nginx Errors: Look for errors in the Nginx error log. The log file is typically located at /var/log/nginx/error.log. You can tail the file by typing:
sudo tail -f /var/log/nginx/error.log

This command will display the last few entries in the file and then display new entries as they are added.

  1. Restart Nginx: After making changes, remember to restart or reload Nginx:
sudo systemctl restart nginx

Here is also a nice tutorial on how to troubleshoot common Nginx problems:

https://www.digitalocean.com/community/tutorials/how-to-troubleshoot-common-nginx-errors

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel