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;
}
Related
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;
}
}
I'm trying to set up a simple blogging site that I wrote using the django framework. The website works except that it isn't serving static files. I imagine that's because nginx isn't running. However, when I configure it to run on any port other than 80 I get the following error:
nginx: [emerg] bind() to 172.17.0.1:9000 failed (99: Cannot assign requested address)
When I run it on a port that is already being used by gunicorn I get the following error:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
My nginx configuration file is as follows:
upstream django {
server 127.0.0.1:8080;
}
server {
listen 172.17.0.1:9000;
server_name my.broken.blog;
index index.html;
location = /assets/favicon.ico { access_log off; log_not_found off; }
location /assets {
autoindex on;
alias /var/www/html/mysite/assets;
}
location / {
autoindex on;
uwsgi_pass unix:///run/uwsgi/django/socket;
include /var/www/html/mysite/mysite/uwsgi_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
But if I run nginx without starting guincorn it runs properly but I get a 403 forbidden error.
Going with the suggested answer I don't get any errors, but the site is returning 403 forbidden and doesn't present the part of the website gunicorn is supposed to deliver.
Configure Nginx and Gunicorn in following way to make it work,
Use unix socket to comminicate between nginx and gunicron rather than running gunicorn in some port
Create a unit file for gunicorn in the following location
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/path-to-project-folder
ExecStart=/<path-to-env>/bin/gunicorn --workers 9 --bind unix:/path-to-sockfile/<blog>.sock app.wsgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.target
Then start and enable gunicorn service
It will generate a sock file in the specified path.
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
Note: Choose the suitable number of workers for gunicorn, can sepecify log files as follows
ExecStart=//bin/gunicorn --workers 9 --bind unix:/path-to-sockfile/.sock app.wsgi:application --access-logfile /var/log/gunicorn/access.log --error-logfile /var/log/error.log
create a new configuration file specific to the project in /etct/nginx rather than edit the default nginx.conf
nano /etc/nginx/blog.conf
and add the following lines ( can also add the file in /etc/nginx/default.d/)
server {
listen 80;
server_name 172.17.0.1; # your Ip
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path-to-static-folder;
}
location /media/ {
root /path-to-media-folder;
}
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:/path-to-sock-file/<blog-sock-file>.sock;
}
}
include the /etc/nginx/blog.conf to nignx.conf
---------
----------
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/blog.conf; # the new conf file added
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
------
-------
run sudo nginx -t // to check for errors in nginx configuration.
run sudo systemctl nginx restart
refer: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
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
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
I have been trying to deploy a demo app with this tutorial. I am doing this on a CentOS 7 in Virtualbox.
However, I am getting 502 Bad Gateway. How can I fix this?
server {} block in nginx.conf file
server {
listen 80;
server_name 172.16.16.215;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/michel/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:/home/michel/myproject/myproject.sock;
}
}
gunicorn.service file
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=michel
Group=nginx
WorkingDirectory=/home/michel/myproject
ExecStart=/home/michel/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/michel/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
var/logs/nginx/error.log
2016/08/28 18:55:14 [crit] 17557#0: *4 connect() to unix:/home/michel/myproject/myproject.sock failed (13: Permission denied) while connecting to upstream, client: 172.16.16.23, server: 172.16.16.217, request: "GET / HTTP/1.1", upstream: "http://unix:/home/michel/myproject/myproject.sock:/", host: "172.16.16.217"
Gunicorn does not have right to save .sock file, or nginx does not have right to read .sock file.
Move this file to /tmp/ folder:
nginx.conf
proxy_pass http://unix:/tmp/myproject.sock:/;
gunicorn.service
ExecStart=/home/michel/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/tmp/myproject.sock myproject.wsgi:application
sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
sudo semodule -i mynginx.pp
This was copied from this answer.
I ran into this issue as well. What solved the issue for me was running:
chmod 711 on the home directory
This allowed nginx to access the .sock file in the run folder. This permission error did not happen to me on Ubuntu, but was present on Amazon Linux 2, which is derived from Cent OS. These django deployment scripts may help others facing this issue.
There are many possibilities to get 502 badgateway , check with below cmds..
sudo systemctl status gunicorn
sudo systemctl status nginx
gunicorn --log-file=- projectname.wsgi:application
These above result should be in active mode
I disabled SELinux and the application worked.
sudo vi /etc/sysconfig/selinux
Set SELINUX=disabled.
Save and exit. RESTART.