nginx working with ip but not domain name - django

I'm trying to set up a django app with gunicorn and ngix. I followed this tutorial. Everything seems to be working but when I edit the server_name in /etc/nginx/sites-available/project to anything other than the serevr ip address I get the default nginx index page instead of the django app. When this is the server config:
server {
listen 80;
server_name <myserverip>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
everything works as expected (nginx serves the app) when I put the ip address into my browser, but if the I add a domain name to replace the ip or in addition to the ip all I get is the nginx page in either location. My ALLOWED_HOSTS in settings.py includes the server ip and the domain name. I cannot see any issue in the nginx logs either. Not sure what the issue is at this point.

You should be able to change the server_name to include your domain:
server_name <myserverip> <mydomainname>;
Feel free to drop the ip address if you only want to be able to access the site using your domain name. You'll also want to add any subdomains (i.e. www) you want to serve the same site. For example:
server_name youdomainname.com www.yourdomainname.com;
Don't forget to restart nginx after updating the config file.

In server_name you can write an IP or URL, but at the same time in your settings.py you must give it permission.
[settings.py]
ALLOWED_HOSTS = ['IP', 'URL']
Here, I uploaded something to my github that can help you:
Github/Nginx

I solved the issue. The nginx default configuration /etc/nginx/sites-available/default was shadowing mine. I commented out the server portion of the default configuration and the domain is working as expected.

Related

Nginx shows only Welcome page after changing server_name from IP adress to domain

I use Nginx as Reverse Proxy for a Django project with Gunicorn.
After following this tutorial from Digital Ocean How To Set Up an ASGI Django App I was able to visit my project through the server IP adress in a browser with http.
In the next step I followed the How To Secure Nginx with Let's Encrypt tutorial from Digital Ocean. Now the site was available with http:// and https:// in front of the IP adress.
To redirect the user automatically to https I used code from this tutorial.5 Steps to deploy Django
The outcome is the following file in /etc/nginx/sites-available:
# Force http to https
server {
listen 80;
server_name EXAMPLE_IP_ADRESS;
return 301 https://EXAMPLE_IP_ADRESS$request_uri;
}
server {
listen 80; # manged by Certbot
server_name EXAMPLE_IP_ADRESS;
# serve static files
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/projectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
The redirect to https is working fine, so I assume the changes I made according to the last tutorial are okay.
After the tests with the EXAMPLE_IP_ADRESS as server_name went well I have changed the server_name to my domain in the form www.example.com
When I type the domain in the browser the only result is the Nginx Welcome page. So the connection to the server is successfull but Nginx is loading the wrong server block.
After searching for hours I came across this Question. Here the answer of ThorSummoner worked for me. The comment by mauris under this answer to unlink the default file in the sites-enabled was the command I needed.
unlink sites-enabled/default
(I posted this Q&A because I searched hours for the solution and hope this is reducing the search time for others having a Django project too with the same problem)

nginx throws bad request 400 when accessed through ddns domain

Working perfect when accessed through local web server IP but throws 400 error when accessed through NOIP hostname.
For context, router is configured to forward requests to the web server on that port.
This is the nginx config file:
server {
listen 80;
server_name 192.168.1.64;
#server_name example.ddns.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/projectdir;
}
location / {
include proxy_params;
# proxy_pass http://example.ddns.net;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
I tried adding the lines where the hashtags are but to no avail.
Assuming your server is properly configured, edit your setting.py in your Django project with the following:
ALLOWED_HOSTS = ["192.168.1.64"]
For future reference, in my case, although I had ALLOWED_HOSTS configured correctly, the problem was reloading gunicorn/nginx after making changes to the django settings file. The following line solved it:
sudo systemctl restart gunicorn
sudo systemctl restart nginx
Credit where it's due, comments from Iain Shelvington and Gaëtan GR were spot on, the underlying problem was ALLOWED_HOSTS setting (which in my case was not applied until a gunicorn/nginx restart was done.

nginx connection refused after config

I was setting up nginx on my aws ubuntu instance. At first every time went well, but after I config nginx and try to connect django, I can't even see the welcome page from either public ip nor the localhost(which was able to access from both sides). The nginx status command shows nginx is running.
Here's my nginx config:
/nginx/sites-available/mysite.com
server{
charset utf-8;
listen 80;
server_name my_aws_ip;
location /static{
alias my_django_static_path;
}
location / {
proxy_set_header Host $host;
proxy_pass http://unix:tmp/mysite.socket;
}
}
And I made a link to /nginx/sites-enabled/
It appears that every time I restarted nginx, I will be able to see the welcome page. However, after that, nginx refuses connections.
I didn't change anything in nginx.conf. Do I need to?
server_name should be your domain name, IP address should be specified as part of the listen directive
proxy_pass http://unix:tmp/mysite.socket;
Not sure where you are hoping this will end up, but you need to decide if you are sending it via http or to a socket. Not both. Having said that if it's for django then it's not proxy_pass you want at all
I'm guessing you mean:
uwsgi_pass unix:/tmp/mysite.socket;
You'll also need to include these somewhere in your config

Error while hitting DNS via Amazon Route53 to the given hosts

SETUP
R53--->ELB(Classic+SSL)--->Nginx(just one instance)
I have a application running on my Nginx server which is listening on port 80. The Nginx instance is working fine and i can hit it via its public IP.
I have now placed a ELB in front of my Nginx , ELB registers my instance as healthy and i can hit ELB(classic) public URL to reach my website. I am terminating SSL on ELB itself
Now when i place a CNAME entry on Route53 ,and point this to my ELB i reach
503 Service Unavailable : No server is available to handle this request.
I tried a lot of things but not sure why this happens.
My Nginx file looks like :
server {
listen 80 ;
listen [::]:80;
root /home/ubuntu/partner/public;
index index.html index.htm index.nginx-debian.html;
# Make site accessible from http://asw.xyz.community/
#server_name drone.xyz.community;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;
#return 301 $schema://www.drone.xyz.community$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
add the server_name directive to nginx config. Also see How nginx processes a request.
Server names are defined using the server_name directive and determine
which server block is used for a given request. See also “How nginx
processes a request”. They may be defined using exact names, wildcard
names, or regular expressions:
server_name <Your Route53 Domain Name>;

unable to access django website from URL but accessible from IP address

I have installed django on Amazon lightsail, I can access my website through the static server IP address. My domain is hosted on Godaddy. I change the Name Server in GoDaddy. But still when I try to access my website through domain name it shows me Nignx welcome page.
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
In my django settings.py I have given only the IP address
ALLOWED_HOSTS = ['YOUR_AMAZON_LIGHTSAIL_IP']
And in Nginx file, I also added only the IP address.
server {
listen 80;
server_name YOUR_AMAZON_LIGHTSAIL_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/django_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/django_project/django_project.sock;
}
}
I am using Gunicorn and Nginx on my hosting server.
You have to add the domain name into allowed hosts like this
ALLOWED_HOSTS = ['mydomain.name']
With this users can only access the site via your domain address. If you also want them to be able to access through the IP you do like this
ALLOWED_HOSTS = ['mydomain.name','ip.ip.ip.ip']
I did the following changes to allow the domain to work,
First create a static IP address on AWS light sail
Assign the static IP address to your VPS
Create a DNS zone, add the NameServer to your domain provider. In my case its Godaddy.
In the django settings.py add the domain name in allowed host setting,
ALLOWED_HOSTS = ['example.com', '0.0.0.0']
In the Nginx file add the domain name
server {
listen 80;
server_name example.com 0.0.0.0;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/django_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/django_project/django_project.sock;
}
}
Restart the Nginx
sudo service nginx restart
Restart the server
sudo reboot