502 Bad Gateway with NGINX, Gunicorn, Django - django

I tried setting up a project with NGINX and Gunicorn using this guide. However, I keep getting a 502 Bad Gateway error.
Here is my server conf:
server {
listen 80;
server_name nexus-staging.chop.edu;
access_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.access.log main;
error_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.error.log warn;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /webapps/nexus/static_cdn;
}
location / {
proxy_set_header Host $http_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 $scheme;
proxy_pass http://unix:/webapps/nexus/nexus.sock;
}
}
Here is my gunicorn service file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
EnvironmentFile=/etc/sysconfig/nexus
User=svc_dgdnexus
Group=dgd_svc
WorkingDirectory=/webapps/nexus
ExecStart=/webapps/nexus/pyvenv/bin/gunicorn --workers 4 --bind unix:/webapps/nexus/nexus.sock django_config.wsgi:application --name nexus --access-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.access.log --error-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.error.log
[Install]
WantedBy=multi-user.target
This is the nginx error:
2017/02/17 22:55:23 [error] 36168#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10.249.27.89, server: nexus-staging.chop.edu, request: "GET / HTTP/1.1", upstream: "http://unix:/webapps/nexus/nexus.sock:/", host: "nexus-staging.chop.edu"
This is the gunicorn error:
[2017-02-17 22:53:59 -0500] [36143] [INFO] Booting worker with pid: 36143
[2017-02-17 22:53:59 -0500] [36145] [INFO] Booting worker with pid: 36145
[2017-02-17 22:55:23 -0500] [36137] [CRITICAL] WORKER TIMEOUT (pid:36140)
[2017-02-18 03:55:23 +0000] [36140] [INFO] Worker exiting (pid: 36140)
[2017-02-17 22:55:23 -0500] [36202] [INFO] Booting worker with pid: 36202

I found a solution:
Here is my server conf:
server {
listen 80;
server_name nexus-staging.chop.edu;
access_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.access.log main;
error_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.error.log warn;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /webapps/nexus/static_cdn/;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
}
Here is the gunicorn service file:
[Unit]
Description=gunicorn daemon
After=network.target
After=syslog.target
[Service]
EnvironmentFile=/etc/sysconfig/nexus
User=svc_dgdnexus
Group=dgd_svc
WorkingDirectory=/webapps/nexus
ExecStart=/webapps/nexus/pyvenv/bin/gunicorn django_config.wsgi --workers 4 --access-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.access.log --error-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.error.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
Make sure your ALLOWED_HOSTS in django settings is something like: .nexus-staging.chop.edu. The . is critical for it work.

Related

502 bad gateway: Nginx, gunicorn, Django

Here is my nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 0.0.0.0;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /myproject;
}
location / {
proxy_set_header Host $http_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 $scheme;
proxy_pass http://unix:/root/myproject/myproject.sock;
}
}
}
And my project have this structure:
This is my gunicorn.service file
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/root/myproject/myproject
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
error.log
2022/04/13 12:45:30 [crit] 20474#20474: *8 connect() to unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.81.234.10, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/", host: "10.10.89.8"
My gunicorn service is working properly and nginx.conf is also working properly with no errors.
All permissions have been granted to myproject directory
Still I am getting a 502 Bad Gateway error.
Please let me know if any additional information needed
The issue is you are creating the Socket file in different location and pointing the nginx to different location. Your socket file is created at
/run/gunicorn.sock as you can see from this command
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
and referring to it in different location(/root/myproject/myproject.sock) in nginx configuration as you can
see here
proxy_pass http://unix:/root/myproject/myproject.sock;
You can solve this in 2 ways
by changing gunicorn conf from
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
to
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application
By changing nginx conf from
proxy_pass http://unix:/root/myproject/myproject.sock;
to
proxy_pass http://unix:/run/gunicorn.sock;
I recommend using the first approach
Restart both gunicorn and nginx after these changes

DJango + nginx + gunicorn: how to start caching and which way is more standard?

I deployed my Django project using Gunicorn, but now can't use Nginx caching.
How to start caching on a project that use Gunicorn and which caching method is standard for Django.
I try to use Ngnix caching but it won't work.
Here is an example of my Ngnix conf and Caching Conf
Ngnix Conf
/etc/nginx/sites-available/my-project-config
upstream daphne_server {
server unix:/var/www/my-project-config/env/run/daphne.sock fail_timeout=0;
}
upstream gunicorn_server {
server unix:/var/www/my-project-config/env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 8000;
server_name my_server_ip_address or domain name;
client_max_body_size 100M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/my_project_dir/public_html;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/ {
proxy_pass http://localhost:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
Cache Config
/etc/nginx/sites-available/my-project-cache-config
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=60m;
upstream origin_server {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name _;
location / {
include proxy_params;
proxy_pass http://origin_server;
proxy_cache custom_cache;
proxy_cache_valid any 10m;
add_header X-Proxy-Cache $upstream_cache_status;
}
}
Gunicorn Socket
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Gunicorn Service
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project_dir
ExecStart=/home/ubuntu/my_project_dir/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
my_project.wsgi:application
[Install]
WantedBy=multi-user.target
I am also using gunicorn to host a django webserver on Nginx, and I did not run into this problem. I don't believe it should matter that you are hosting with gunicorn. As long as you configure caching for the directory that contains all of your static files it should work.
Here's an example where the static files are cached for a year:
location /static {
root [location of /static folder];
expires 1y;
access_log off;
add_header Cache-Control "public";
}
If this doesn't solve the problem please add your cache settings for Nginx to your question
If you are trying to cache django views or templates, look into django-cahceops or a similar package

No .sock file is being created by my gunicorn service

I have the following problem while hosting my website. I configured all the things .conf files here:
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=user
Group=nginx
WorkingDirectory=/root/myproject
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --workers 3 --bind
unix:/root/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
and my nginx.conf
server {
listen 80;
server_name agros.uz www.agros.uz;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/myproject/;
}
location / {
proxy_set_header Host $http_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 $scheme;
proxy_pass http://unix:/root/myproject/myproject.sock;
}
}
However I am having the following error
unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 37.9.113.208, server: agros.uz, request: "GET /uz/insurance?type=physical&page=2&page=7&page=6&page=8&page=4 HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/uz/insurance?type=physical&page=2&page=7&page=6&page=8&page=4", host: "agros.uz"
When I access the domain this error happens 502 Bad Gateway error
and the .sock file is not being created in /root/myproject/myproject.sock as it mentioned in my gunicorn.service file.

I'm keep getting 502 gateway error (django)

Would you please check my codes and tell me where does this error come form?
gunicorn.sevice file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/root/parsiproject
ExecStart=/root/parsiproject/parsiEnv/bin/gunicorn --workers 3 --bind
unix:/root/parsiproject/myproject.sock myproject.wsgi:applicat$
[Install]
WantedBy=multi-user.target
nginx.conf file:
...
server {
listen 80;
server_name 64.34.135.108;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/parsiproject;
}
location / {
proxy_set_header Host $http_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 $scheme;
proxy_pass http://unix:/root/parsiproject/myproject.sock;
}
...
nginx error.log:
2018/08/12 11:57:37 [crit] 3792#0: *35 connect() to
unix:/home/user/myproject/myproject.sock failed (2: No such file or
directory) w$
I've already inserted my server ip in setting.py allowed host.
I guess your socket path is not proper and also the gunicorn service file is having issue. Also myproject.wsgi:applicat$ should be myproject.wsgi:application I guess. Follow the below tutorial for creating the socket and all the path.
Django setup tutorial
Following is an example of the service file:
[Unit]
Description=xyz django daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/services/xyz_backend/xyz_backend
ExecStart=/var/www/services/xyz_backend/xyz_python_env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/services/xyz_backend/xyz_backend/xyz.sock xyz_backend.wsgi:application

Django gunicorn nginx "connection reset by peer while reading response header"

trying to set up django with gunicorn and nginx, getting some weird errors from nginx:
2015/10/07 06:17:22 [error] 28817#0: *12 connect() to
unix:/home/david/StockSearch/stocksearch/stocksearch.sock failed (111: Connection refused) while connecting to upstream,
client:79.176.114.181, server: 128.199.36.78, request: "GET / HTTP/1.1",
upstream: "http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock:/",
host: "128.199.36.78" 2015/10/07 06:17:24 [error] 28817#0: *12 recv()
failed (104: Connection reset by peer) while reading response header from upstream,
client: 79.176.114.181, server: 128.199.36.78, request: "GET / HTTP/1.1",
upstream: "http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock:/",
host: "128.199.36.78"
nginx config:
server {
listen 80;
server_name 128.199.36.78;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/david/StockSearch/stocksearch;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
}
}
gunicorn config:
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid david
setgid www-data
chdir /home/david/StockSearch/stocksearch
exec /home/david/Env/ssenv/bin/gunicorn --workers 3 --bind unix:/home/david/StockSearch/stocksearch/stocksearch.sock stocksearch.wsgi:application
I'm very new to nginx and gunicorn, but i think you can't do that:
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
this is achived with fastCGI not with proxypass:
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
astcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
I can tell how i configure my nginx and gunicorn in my server, i hope my configuration helps:
server {
server_name XX.XX.XX.XX;
access_log /webapps/Project/log/acces.log;
error_log /webapps/Project/log/error.log warn;
location /static/ {
root /webapps/Project;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM N$
}
}
then i start my gunicorn server in my local machine like:
gunicorn Project.wsgi:application --bind 127.0.0.1:8001