Django + Gunicorn + Nginx. 404 - django

I am trying to run django + gunicorn + nginx locally.
For some reason nginx is not getting data from static. Please tell me what am I doing wrong?
Gunicorn:
gunicorn config.wsgi:application --bind 127.0.0.1:8001
Nginx config:
# mysite_nginx.conf
upstream project_server {
server 127.0.0.1:8001;
}
# the upstream component nginx needs to connect to
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ferom/Programming/my_avatar_clock/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ferom/Programming/my_avatar_clock/my_avatar_clock/proj/static; # your Django project's static files - amend as required
}
location / {
proxy_pass http://127.0.0.1:8000;
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"';
}
}
Folders:
In settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
But when I open http://127.0.0.1/admin/
Nginx say me: 404.
cat /var/log/nginx/error.log - empty
What am I doing wrong?

Maybe you unicorn port was not the same on the Nginx file. I recommend you try set the same port, on the Gunicorn and Nginx proxy.
you say that Gunicorn command was on port 8001:
gunicorn config.wsgi:application --bind 127.0.0.1:8001
But you nginx file was proxy on port 8000:
proxy_pass http://127.0.0.1:8000;

Related

Setting up reverse proxy Nginx + Gunicorn/Django REST - 404 Not Found

I have a setup with Nginx as web server and Gunicorn as application server serving a Django REST API.
Nginx is listening at port 80 and gunicorn at port 8000 (I launch gunicorn using this command):
gunicorn --bind 0.0.0.0:8000 cdm_api.wsgi -t 200 --workers=3
and when I launch the petition to port 8000, I am able to access to the API running for instance:
curl -d "username=<user>&password=<pass>" -X POST http://127.0.0.1:8000/api/concept
However, when I try to make the petition though Nginx acting as reverse proxy, I get 404 Not Found Error:
curl -d "username=<user>&password=<pass>" -X POST http://127.0.0.1:80/api/concept
Here is my nginx conf file:
server {
listen 80;
listen [::]:80;
server_name 127.0.0.1;
location /api/ {
proxy_pass http://127.0.0.1:8000/api/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 360s;
proxy_read_timeout 360s;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
Let me know if more information is needed. Thanks in advance!
So
gunicorn will create a sock file and you will have to map your proxy_pass with the sock file created, for now you are mapping to your localhost:8000 for this you will have to run the django runserver command so that nginx can find a server running on port 8000.
something like -
location / {
include proxy_params;
#proxy_pass http://unix:/home/abc/backend/social.sock;
proxy_pass http://unix:/home/abc/backend/backend.sock;
}
gunicorn make it possible to serve django at production, you don't have to command runserver everytime. Make sure gunicorn restarts everytime when server reboots.

Nginx as reverse proxy django gunicorn returning 400 bad request

I'm trying to use nginx as a reverse proxy to receive incoming calls, then, depending on the server_name, redirect those calls to different computers (hosts), running nginx Django and Gunicorn. So far, I've tried different configurations for the conf file on the host, but none of them are working. Is there anything wrong with my conf files?
This is the nginx.conf in 192.168.0.13 that will function as a reverse proxy:
server {
listen 80;
server_name www.coding.test;
location / {
proxy_pass http://192.168.0.8:80;
proxy_redirect off;
# app1 reverse proxy follow
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
This is the nginx.conf in 192.168.0.8 that is intended to run the django app:
upstream django {
server unix:///home/pi/coding-in-dfw/mysocket.sock fail_timeout=0;
}
server {
listen 80 default_server;
server_name www.coding.test
client_max_body_size 4G;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /static/ {
alias /home/pi/coding-in-dfw/static/;
}
location /media/ {
alias /home/pi/coding-in-dfw/media/;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
location /.well-known {
alias /home/pi/coding-in-dfw/.well-known;
}
}
Finally this is the way I'm running gunicorn:
gunicorn --workers 5 --bind unix:///home/pi/coding-in-dfw/mysocket.sock codingindfw.wsgi:application && sudo service nginx restart
Any help is appreciated.

Django, Gunicorn Setup

I am trying to setup a django project on my server and cannot get it to run. I am using virtualenv, gunicorn and nginx for static files. I am not sure were I am going wrong. My current setup is as follows:
myenv
- project(my django project)
- bin(and all it contains)
- lib(and all it contains)
- include(and all it contains)
- gunicorn_config.py
gunicorn_config.py:
command = '/home/me/django/myenv/bin/gunicorn'
pythonpath = '/home/me/django/myenv/project'
bind = '127.0.0.1:80'
workers = 2
nginx project.conf:
upstream project_server {
server unix:/tmp/gunicorn_project.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
# set the correct host(s) for your site
server_name project.com www.project.com;
keepalive_timeout 5;
# path for static files
root /home/me/django/myenv/assets;
location / {
# checks for static file, if not found proxy to app
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://project_server;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/me/django/myenv/project/project/templates;
}
}
And I run the following to try start it all up:
/home/me/django/myenv/bin/gunicorn -c /home/me/django/myenv/gunicorn_config.py project/project/wsgi.py
But it just says "Can't connect to ('127.0.0.1', 80)"
You've configured gunicorn to bind on a TCP port, but gunicorn is binding on a unix socket. You should use the same thing; preferably the socket, so it doesn't conflict with the port nginx is actually listening on.
In gunicorn_config.py:
bind = 'unix:/tmp/gunicorn_project.sock'
Basically, I would guess nginx spins up before gunicorn. It takes port 80 (from your listen). gunicorn comes next, also wants port 80 (from your bind), and finds it occupied so it errors out. Run gunicorn on a different port and use proxy_pass to tell nginx about it.
Gunicorn
bind = '127.0.0.1:8000'
Nginx
proxy_pass http://127.0.0.1:8000/;

Setup two websites using Django apps and Nginx, but after enter site1.com it keeps showing content of site2.com

After I type in my browser www.site2.com, it open page of www.site1.com.
I think my configuration for both nginx and gunicorn are fine.
etc/nginx/site-available/site1 - my default website www.site1.com
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/site1/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/site1/static_root;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
etc/nginx/site-available/site2 - my new website www.site2.com
upstream app_server2 {
server 127.0.0.1:9500 fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name www.site2.com site2.com;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/test-django/site2/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/test-django/site2/static;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server2;
}
}
etc/init/site1.conf
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectedly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
exec gunicorn \
--name=site1\
--pythonpath=site1 \
--bind=0.0.0.0:9000 \
--config /etc/gunicorn.d/gunicorn.py \
site1.wsgi:application
etc/init/site2.conf
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectedly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django/test-django/
exec gunicorn \
--name=site2\
--pythonpath=site2 \
--bind=127.0.0.1:9500 \
--config /etc/gunicorn.d/gunicorn.py \
site2.wsgi:application
I didn't forget about service nginx restart and service site2 restart, also after nginx -t I don't have any erros.
In your wsgi.py files of your projects, are you using the same variable for the django.conf.ENVIRONMENT_VARIABLE? Usually the line is: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_site.settings")
Try to change the line for those:
django.conf.ENVIRONMENT_VARIABLE = "DJANGO_SITEA_SETTINGS_MODULE"
os.environ.setdefault("DJANGO_SITEA_SETTINGS_MODULE", "project_site.settings")
Change SITEA for whatever you want for each project and also project_site.settings for the name of the folder where the settings file is. Restart the server after this changes.
I don't know if this is the answer but it may help because it fixed for me the same problem you describe.

nginx : How to make nginx point to django

Iam new to nginx.
I have configured nginx,gunicorn and django.
when i start nginx, it gives an error as,
404 Not Found
nginx/1.1.19
as it is not pointing to django.
Now i need to point out nginx to django(using gunicorn as middleware) in conf file using location or root.
Can anyone tell me how to point out nginx to django.
Thank you
I've got many Apps running like this:
server {
# listen, statics, media, etc
location / {
proxy_pass http://127.0.0.1:8000; # Gunicorn Server
}
}
First, you need to actually have the gunicorn process running. Do this either manually, or ideally using a process management tool such as supervisord. Here's a sample supervisord script that runs a gunicorn process on a django project:
[program:example]
user=ubuntu
group=ubuntu
directory=/home/ubuntu/dev/example
command=python manage.py run_gunicorn -c gunicorn_config.py
autostart=true
autorestart=true
redirect_stderr=True
Then you need a proper nginx conf. Here's a minimal sample based off a site running in production:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
location / {
proxy_pass http://localhost:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# use this if you're serving static assets locally
location /static/ { # make sure this is your STATIC_ROOT
alias /home/ubuntu/dev/example/static/;
access_log off;
}
}