1

我在 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,长请求挂起并最终超时通常是一个问题,但我不确定它如何应用于我的项目,因为我只是尝试加载站点。

不知道如何调试这个问题,所以任何帮助表示赞赏!

4

1 回答 1

0

这可能会帮助您:

我所做的是我编辑/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 文件中做任何更改

于 2016-11-15T07:22:35.350 回答