I'm keep getting 502 gateway error (django) - 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

Related

Nginx returns standard pages and ignores Django urls

I've tried everything, and I don't understand what the problem is.
OS: Ubuntu 20.04.5 LTS
Nginx config:
/etc/nginx/sites-available/default
server {
listen 80;
server_name ***.**.***.***;
charset utf-8;
client_max_body_size 10M;
location /static {
alias /var/django-backend/static;
}
location /media {
alias /var/django-backend/media;
}
location / {
proxy_set_header Host $http_host;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
P.S. Then I run the command sudo ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled
Gunicorn service config:
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
WorkingDirectory=/var/django-backend
ExecStart=/var/django-backend/venv/bin/gunicorn \
--access-logfile - \
-k uvicorn.workers.UvicornWorker \
--workers 3 \
--bind unix:/run/gunicorn.sock \
backend.asgi:application
[Install]
WantedBy=multi-user.target
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
When I go to any endpoint, nginx returns a 404 page
Seeing you can still access the Static files it's probably something with the Gunicorn Settings. Double Check that it's running and not throwing up any errors..
Looking at the Gunicorn Docs, and my own uWsgi.Nginx Settings, it looks like it's normally set up like (snippet) so try giving that a shot
Note: That it's seperated into it's own upstream section that is outside of the server section
upstream django {
# full path to socket, (what I use)
server unix:///run/gunicorn.sock;
# `cd /run/gunicorn.sock` would be the location
# the doc example is:
# server unix:/tmp/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name ***.**.***.***;
charset utf-8;
client_max_body_size 10M;
location /static {
alias /var/django-backend/static;
}
location /media {
alias /var/django-backend/media;
}
location / {
proxy_set_header Host $http_host;
proxy_pass http://django;
# The Docs also has all this extra Junk, idk if it's important
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
}
}

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

Django channels and nginx - error during handshake

I'm trying to deploy a Django/Django Channels application to a VPS. The django part of the project works, i can visit any url and the templates are loading, but the Django Channels part of it doesn't work. Whenever i try to reach the websocket i either get connection refused or WebSocket connection to 'ws://54.39.20.155/receiver' failed: Error during WebSocket handshake: Unexpected response code: 404
Can someone help me find what i'm doing wrong and tell me what do i need to do in order to run Django Channels?
Here is my setup:
Environment:
virtualenv
django
django-channels
gunicorn
nginx
systemd
/etc/nginx/sites-available/myproject
server {
listen 80;
server_name 54.39.20.155;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /WaitingRoomVenv/WaitingRoom/WaitingRoom/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
}
}
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/WaitingRoomVenv/WaitingRoom
ExecStart=/WaitingRoomVenv/WaitingRoomEnv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/WaitingRoomVenv/WaitingRoomEnv.sock WR.wsgi:application
[Install]
WantedBy=multi-user.target
To start gunicorn: sudo systemctl start gunicorn
To start nginx: sudo systemctl restart nginx
Add to your nginx.conf
location /receiver {
proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}

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.

502 Bad Gateway with NGINX, Gunicorn, 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.