使用 Django (Nginx + Gunicorn) 提供静态文件时 HTTP 504 网关超时
HTTP 504 Gateway Time-out when serving static file with Django (Nginx + Gunicorn)
我在 Amazon EC2 上托管我的 Django 项目时遇到问题。
使用 Gunicorn 和 Nginx 托管站点,尝试在浏览器中加载我的页面时出现以下错误(摘自 Javascript 控制台):
Failed to load resource: the server responded with a status of 504 (Gateway Time-out): https://example.com/favicon.ico
我认为 Nginx 在查找我的静态文件时遇到了一些问题,但我不确定原因。这是我的 Nginx 配置:
server {
listen 443 default;
client_max_body_size 100M;
server_name www.example.com;
keepalive_timeout 5;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
# the domain name it will serve for
charset utf-8;
# path for static files
root /opt/app/staticfiles;
location /static {
root /opt/app/staticfiles;
}
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://app_server;
}
}
/var/log/nginx/access.log 和 cat /var/log/nginx/error.log 不显示任何内容。
对于 HTTP 代码 504,长请求挂起并最终超时通常是一个问题,但我不确定它如何应用于我的项目,因为我只是尝试加载网站。
不确定如何调试此问题,因此非常感谢您的帮助!
这可能对您有帮助:
我所做的是编辑 /etc/nginx/site-enabled/default
文件如下:
server {
#nginx server configuration
listen 80 default_server;
# listen [::] default_server ipv6only=on;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
# server_name localhost;
# provide whole path of static files you got after running `python manage.py collectstatics`
location /static{
alias /home/ubuntu/folder1/folder2/webpage/static;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://0.0.0.0:8134;#Provide your django server's link
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
注意:- 确保你已经完成 python manage.py collectstatics
。不要在 nginx.conf 文件
中做任何更改
我在 Amazon EC2 上托管我的 Django 项目时遇到问题。 使用 Gunicorn 和 Nginx 托管站点,尝试在浏览器中加载我的页面时出现以下错误(摘自 Javascript 控制台):
Failed to load resource: the server responded with a status of 504 (Gateway Time-out): https://example.com/favicon.ico
我认为 Nginx 在查找我的静态文件时遇到了一些问题,但我不确定原因。这是我的 Nginx 配置:
server {
listen 443 default;
client_max_body_size 100M;
server_name www.example.com;
keepalive_timeout 5;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
# the domain name it will serve for
charset utf-8;
# path for static files
root /opt/app/staticfiles;
location /static {
root /opt/app/staticfiles;
}
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://app_server;
}
}
/var/log/nginx/access.log 和 cat /var/log/nginx/error.log 不显示任何内容。
对于 HTTP 代码 504,长请求挂起并最终超时通常是一个问题,但我不确定它如何应用于我的项目,因为我只是尝试加载网站。
不确定如何调试此问题,因此非常感谢您的帮助!
这可能对您有帮助:
我所做的是编辑 /etc/nginx/site-enabled/default
文件如下:
server {
#nginx server configuration
listen 80 default_server;
# listen [::] default_server ipv6only=on;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
# server_name localhost;
# provide whole path of static files you got after running `python manage.py collectstatics`
location /static{
alias /home/ubuntu/folder1/folder2/webpage/static;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://0.0.0.0:8134;#Provide your django server's link
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
注意:- 确保你已经完成 python manage.py collectstatics
。不要在 nginx.conf 文件