gpt4 book ai didi

django - 使用Django提供静态文件时,HTTP 504网关超时(Nginx + Gunicorn)

转载 作者:行者123 更新时间:2023-12-02 20:18:48 26 4
gpt4 key购买 nike

我在将我的Django项目托管在Amazon EC2上时遇到了问题。
使用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文件中做任何更改

关于django - 使用Django提供静态文件时,HTTP 504网关超时(Nginx + Gunicorn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com