Nginx redirects to default page - django

I am setting up a domain for my Django/Gunicorn/Nginx server. It works fine with IP address instead of domain name in server_name but when I add domain name it redirects to default Ubuntu Nginx page. My Nginx file looks like this (please note that I replaced my domain with example.com):
Path : /etc/nginx/sites-available/projectname
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
root /path/to/static/dir;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unix:/path/to/gunicorn.sock;
}
}
I have run the command sudo nginx -t and sudo service nginx restart but no effect. Please let me know if I am doing anything wrong.

1- see main nginx.conf how include all config files. if it is including site-enabled path then go to path and see is a shortcut to config file of this site under site available?
or if all sites are enabled in nginx config file include directly available
include /etc/nginx/sites-available/*;
2-mix two server define code once and with rule forward non www to with www
3-if not work check dns config problem and see result from inside of server via putty not from outside of server with browser to see it is nginx problem or dns config problem.
note: changing dns name servers taken some hours to work and effect on clients.

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)

I have problem to deploy my django project with docker nginx and gunicorn

If I have running my containers on the server side and everything is okay,for accessing route on the browser should I put ip of nginx container with domain in etc/host or it have to work without that?
my nginx.config
server {
listen 80;
server_name my_domain;
root /code/;
error_log /var/log/nginx/new_error.log debug;
location / {
proxy_pass http://web:8000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /code/staticfiles/;
}
location /mediafiles/ {
alias /code/mediafiles/;
}
}
where web is my docker-container with gunicorn running
If your nginx container has the ports expose using the flag -p <host_port>:<container_port> you can access to nginx service adding the domain in your /etc/hosts file pointing to the ip of your localhost, however if you didn't use this flag you need pointing to the ip of your nginx container. What's the difference?...when you expose the ports you can use the service even outside of the host where the container was deployed.
I hope help you.

Django, nginx, + gunicorn: connection refused when serving media files

I have a Django app served by gunicorn with nginx handling incoming requests. Static files are served just fine, and media files are served in my local development environment- I have MEDIA_ROOT and MEDIA_URL set.
However, on my web host (running Ubuntu 14.04), media files aren't served. Even if I run the Django dev server instead of letting gunicorn serve the app, media files do not show up and the following errors are output to the console:
Since taking gunicorn out of the equation results in the same behavior, I believe the issue is with my nginx configuration. This is my sites-available for the app:
server {
listen 443 ssl;
server_name <server name here>;
<ssl stuff here>
access_log off;
location /static {
alias /opt/testenv/testenv/proj/static/;
}
location /media {
autoindex on;
alias /opt/testenv/testenv/proj/media/;
}
location ~ /.well-known {
allow all;
}
location / {
proxy_pass http://127.0.0.1:9900;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
server {
listen 80;
server_name <server name here>;
return 301 https://$host$request_uri;
}
I was initially missing the location /media block, which I thought was the issue. However, even after adding it, restarting nginx, and running collectstatic, I still get CONNECTION REFUSED errors when attepting to retrieve the files.
I turned on autoindex to verify that the files can be navigated to manually, and that does work.
The nginx error log doesn't contain anything related to these connection errors, which is curious to me- could the request be getting rejected elsewhere? I recently setup HTTPS but this issue was occurring even before that.
Looks like you are making requests directly to gunicorn instead of nginx,from you configuration you should visit:
https://127.0.0.1
instead of:
http://127.0.0.1:9900

Intermittent Bad Gateway nginx (with Django)

I'm using vagrant and virtualbox for my Django environment. The django environment uses nginx. Everything works fine except intermittently I'll see 502 bad gateway errors. When these errors happen, there is nothing in nginx access.log or error.log. Here are my configurations
Vagrant file private network
config.vm.network "private_network", ip: "192.168.33.10"
nginx.conf
server {
listen 80 default_server;
server_name _;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_set_header Host 192.168.33.10;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000;
}
}
I'm not sure how to debug or fix this issue. Any ideas?
You can try python manage.py runserver 192.168.33.10:8000 since Django defaults your ip to 127.0.0.1 and nginx might have problems with that.

how to serve static files through nginx and django in windows

I am a newbie. Just got setup the apache server in windows. But now I need nginx to serve my static files and I think I have searched almost everywhere how to configure nginx to serve static files, got many answers, but it was hard to understand. Can someone please explain where do I start from and how to configure nginx and django in windows in a noob's, perspective. Any help will be appreciated. Thank you!
edit the nginx.conf file located in the conf folder of the unzipped nginx for windows and add the following directive (You have to update the path to match the folder of your django project)
location /static/ {
alias 'C:/Users/user1/your_django_project/staticfiles/';
}
an example of your final nginx.conf could look like this if you are running a django webserver like hypercorn or other on port 8000 on localhost
http{
server {
server_name localhost;
location / {
proxy_pass http://backend;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP 127.0.0.1;
proxy_set_header X-Forwarded-For 127.0.0.1;
proxy_set_header X-Forwarded-Proto http;
}
location /static/ {
alias 'C:/Users/user1/your_django_project/staticfiles/';
}
}
upstream backend {
server 127.0.0.1:8000;
# There could be more than a backend here
}
}
events {
worker_connections 1024;
}
tested on nginx 1.20
Try something like this in your server section:
location /static/ {
root /the_directory_with_your_files/;
}