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

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

Related

how to deploy react as frontend and drf (django rest framework) on same server( Digitalocean or aws )

I have Django react app. I am using drf for apis and react as front-end. i want to deploy them on digital ocean separately on same server. any suggestions?
First of all, I must say that my main stack is Django-uWSGI-NGNIX. There is a great instruction, which you can use with DigitalOcean:
How To Serve Django Applications with uWSGI and Nginx
And this one for Gunicorn:
How To Set Up Django with Postgres, Nginx, and Gunicorn
I recommend trying to deploy your Django App on the server first, using these instructions. Maybe it will be just a simple one-page project.
After that you can modify your ngnix configs. In my case it will be:
upstream my_backend_server_name_or_whatever {
# server unix:///path/to/your/mysite/mysite.sock; -- if you want to use sockets
server 127.0.0.1:5000;
}
server {
listen 80;
server_name yourdomain.com/api; #or /back, /backend, /what_you_like
charset utf-8;
client_max_body_size 75M;
location /media #your locantions configs
....
location / {
uwsgi_pass my_backend_server_name_or_whatever;
include /path/to/file/uwsgi_params;
}
}
Also, you need to run your CGI server on port 5000. After that, you can access to your Django app through yourdomain.com/api and it will upstream to localhost:5000.
Try this step with your current Django app. After that, you can configure your DRF to work with these links.
If it will work, the next step. Run your NodeJS server on the other port, like 5100. You can find the same instructions for Webpack or raw NodeJS. After that use the same technics, but for the NodeJS server. In my case:
upstream my_frontend_server_or_whatever {
server localhost:5100;
}
server {
listen 80;
server_name yourdomain.com;
access_log /var/log/nginx/ide.access.log;
error_log /var/log/nginx/ide.error.log;
client_max_body_size 75M;
location / {
proxy_pass http://my_frontend_server_or_whatever;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
}
Now you can save both files as backend.conf and frontend.conf, run ngnix and check all configs like in instructions above. After that, you can use yourdomain.com/api links in your React App.
In my opinion, this is the simplest way to try React+DRF. But these configs only for development!! Not for production.
Hope it's helpful.

deploying a django app with nginx, gunicorn, daphne (for channels 2) and supervisor

As it follows from the title I'm trying to deploy a django application with the above-mentioned stack of technologies. We're using django for the serverside and angular 6 for the frontend. I'm doing this for the first time in my life and there are some things that are not quite clear to me, so correct me if I say something weird.
I'm using Nginx to serve static. The config looks as follows:
serverside_nginx_static.conf
server {
listen [::]:80 ;
root /home/user/serverside_deployment/static/;
index index.html index.htm index.nginx-debian.html;
client_max_body_size 4G;
access_log /home/user/serverside_deployment/logs/nginx-access.log;
error_log /home/user/serverside_deployment/logs/nginx-error.log;
location / {
try_files $uri $uri/ /index.html;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/user/serverside_deployment/static/;
}
1.1 I'm also using Nginx to communicate with gunicorn which in turn communicates with the django app itself. The appropriate config looks like this:
serverside_nginx.conf
upstream mtt_server {
server unix:/home/user/serverside_deployment/serverside_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 8000;
server_name 192.168.10.98;
client_max_body_size 4G;
access_log /home/user/serverside_deployment/logs/nginx-access.log;
error_log /home/user/serverside_deployment/logs/nginx-error.log;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://mtt_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/user/serverside_deployment/static/;
}
}
I'm using supervisor to start the gunicorn bash script.
So this configuration does work or to be precise the REST does but the websockets do not. I'm getting 404 Not Found saying that the defined channels cannot be found. Unless I'm mistaken in order to solve this issue I need to deploy django channels with Daphne as the interface server.
This manual seems to be a tad obsolete but give an idea as to what the process of deployment should look like. However my case is more complicated and I can't make out how I should connect the daphne part to what I already have. I just need to make the websockets work and I don't think I fully understand why it cannot find the channels open.
It's an interesting task but unfortunately there's not too much time left to solve it so I would appreciate any help.
P.S.
The static is somewhat in the wrong location at the moment, but it will be moved to the right place later.
EDIT:
What is weird is that everything works fine when ran on the dev server.
EDIT 2:
Any ideas?

Nginx redirects to default page

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.

nginx: [emerg] host not found in "localhost:8004" of the "listen" directive

I am developing a django powered application using nginx and gunicorn.
Everything was fine, until today which i tried to restart nginx which failed. so I tested it with sudo nginx -t command and I got this error.
nginx: [emerg] host not found in "localhost:8004" of the "listen" directive in
/etc/nginx/sites-enabled/808:2
nginx: configuration file /etc/nginx/nginx.conf test failed
this is 808 config file contents:
server {
listen localhost:8004;
error_log /var/www/html/burger808/error.log;
location / {
proxy_pass http://127.0.0.1:8005;
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"';
}
location /static/ {
autoindex on;
alias /var/www/html/burger808/burger808/static/;
}
location /media/ {
autoindex on;
alias /var/www/html/burger808/burger808/media/;
}
}
I thought it might be some conflicting over 8004 port. So i checked the other congfig files and there were no conflictings.
I wonder what could've caused this error?
No need of localhost:8004.Only need is
listen 8004;
My problem was that I was trying to add custom host "my-website.test" to the nginx config.
What I had to do first is to modify my /etc/hosts and add "my-website.test" there.
After that my nginx ran successfully.
this smart answer was my solution
I assume that you're running a Linux, and you're using gEdit to edit
your files. In the /etc/nginx/sites-enabled, it may have left a temp
file e.g. default~ (watch the ~). Delete this file, and it will
solve your problem.

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/;
}