可以仅使用 Nginx 和 Daphne 为 Django Channels 应用程序提供服务吗?
Possible to serve Django Channels app only using Nginx and Daphne?
我假设我可以 运行 仅使用 Daphne
(ASGI) 和 Nginx
作为我的 Django 应用程序开始的代理的 Django Channels
应用程序和。
申请将 运行 Daphne
127.0.0.1:8001
但是,我 运行 遇到 403 Forbidden
错误。
2019/03/06 17:45:40 [error] *1 directory index of "/home/user1/app/src/app/" is forbidden
当我发帖时,另一位用户提到
There is no directive to pass http request to django app in your
nginx config
并建议查看 fastcgi_pass
或 uwsgi_pass
或 Gunicorn
。
显然 ASGI
上的 Django Channels 运行s 并且我现在正在通过它传递所有请求(不是 uWSGI
然后根据请求传递到 ASGI
.)
我可以只使用 Nginx
和 Daphne
来提供我的 Django 应用程序吗? Django Channels docs 似乎是这么认为的,因为他们没有提到需要 Gunicorn 或类似的东西。
我的 nginx 配置
upstream socket {
ip_hash;
server 127.0.0.1:8001 fail_timeout=0;
}
server {
listen 80;
#listen [::]:80 ipv6only=on;
server_name your.server.com;
access_log /etc/nginx/access.log;
root /var/www/html/someroot;
location / {
#autoindex on;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri =404;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
#proxy_set_header X-NginX-Proxy true;
#proxy_pass http://socket;
#proxy_redirect off;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
#proxy_redirect off;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_cache one;
#proxy_cache_key sfs$request_uri$scheme;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
# managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem;
# managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
是的,这是可能的。试试这个配置:
upstream socket {
ip_hash;
server $DAPHNE_IP_ADDRESS$ fail_timeout=0;
}
server {
...
location / {
proxy_pass http://socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...
}
哪里 $DAPHNE_IP_ADDRESS$ - 你的 daphne IP 和端口没有模式(127.0.0.1:8001
)。
我假设我可以 运行 仅使用 Daphne
(ASGI) 和 Nginx
作为我的 Django 应用程序开始的代理的 Django Channels
应用程序和。
申请将 运行 Daphne
127.0.0.1:8001
但是,我 运行 遇到 403 Forbidden
错误。
2019/03/06 17:45:40 [error] *1 directory index of "/home/user1/app/src/app/" is forbidden
当我发帖时,另一位用户提到
There is no directive to pass http request to django app in your nginx config
并建议查看 fastcgi_pass
或 uwsgi_pass
或 Gunicorn
。
显然 ASGI
上的 Django Channels 运行s 并且我现在正在通过它传递所有请求(不是 uWSGI
然后根据请求传递到 ASGI
.)
我可以只使用 Nginx
和 Daphne
来提供我的 Django 应用程序吗? Django Channels docs 似乎是这么认为的,因为他们没有提到需要 Gunicorn 或类似的东西。
我的 nginx 配置
upstream socket {
ip_hash;
server 127.0.0.1:8001 fail_timeout=0;
}
server {
listen 80;
#listen [::]:80 ipv6only=on;
server_name your.server.com;
access_log /etc/nginx/access.log;
root /var/www/html/someroot;
location / {
#autoindex on;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri =404;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
#proxy_set_header X-NginX-Proxy true;
#proxy_pass http://socket;
#proxy_redirect off;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
#proxy_redirect off;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_cache one;
#proxy_cache_key sfs$request_uri$scheme;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
# managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem;
# managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
是的,这是可能的。试试这个配置:
upstream socket {
ip_hash;
server $DAPHNE_IP_ADDRESS$ fail_timeout=0;
}
server {
...
location / {
proxy_pass http://socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...
}
哪里 $DAPHNE_IP_ADDRESS$ - 你的 daphne IP 和端口没有模式(127.0.0.1:8001
)。