【问题标题】:nginx+gunicorn+django/mezzanine not serving static filesnginx+gunicorn+django/mezzanine 不提供静态文件
【发布时间】:2017-05-31 18:44:26
【问题描述】:

我有一个夹层应用。我使用 gunicorn 作为网络服务器,使用 nginx 作为反向代理。但是,不会提供静态文件(但是,它们是通过内置的 django 开发服务器提供的)。我已经运行了 python manage.py collectstatic。

这是我的 nginx.conf 文件:

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Configuration containing list of application servers
    upstream app_servers {

        server 127.0.0.1:8080;
        # server 127.0.0.1:8081;
        # ..
        # .

    }

    # Configuration for Nginx
    server {

        # Running port
        listen 80;

        # Settings to serve static files 
        location ^~ /static/  {

            # Example:
            # root /full/path/to/application/static/file/dir;
            root /root/myapp/myapp/static/;

        }

        # Serve a static file (ex. favico)
        # outside /static directory
        location = /favico.ico  {

            root /app/favico.ico;

        }

        # Proxy connections to the application servers
        # app_servers
        location / {

            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $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-Host $server_name;

        }
    }
}

我可以做些什么来提供静态文件?

【问题讨论】:

    标签: python django nginx gunicorn


    【解决方案1】:

    这看起来像是在 nginx 中对 rootalias 的错误使用。假设在你的 settings.py 文件中你有:

    STATIC_URL = '/static/'
    

    那么你想要一个看起来像这样的位置:

        location /static/  {
            # Example:
            # root /full/path/to/application/static/file/dir;
            root /root/myapp/myapp/;
            # - or -
            # alias /root/myapp/myapp/static/;
        }
    

    对于alias

    定义指定位置的替换。比如下面的配置

       location /i/ {
           alias /data/w3/images/;
       }
    

    根据“/i/top.gif”的请求,将发送文件/data/w3/images/top.gif。 ... 当位置与指令值的最后一部分匹配时:

     location /images/ {
       alias /data/w3/images/;
     }
    

    最好使用 root 指令:

     location /images/ {
       root /data/w3;
     }
    

    【讨论】:

    • 对不起,我不关注。我应该用别名试试吗?看来您建议我应该改用 root,但我从未使用过别名,所以我有点困惑。
    • 看看我的根指令和你的区别。您可以使用 root 或别名,您只需确保指向正确的目录。
    • 好的,我都尝试了(删除带有根指令的“静态”目录,并使用“别名”保留它)。都没有奏效。静态资源返回 403。
    • 您收到的是 403?访问日志文件有什么错误?
    • "客户 IP" [18/Jan/2017:22:08:38 +0000] "GET /static/css/bootstrap.css HTTP/1.1" 403 178 "站点 IP" "用户代理"
    猜你喜欢
    • 2015-11-30
    • 2021-07-18
    • 2013-06-27
    • 2023-03-05
    • 2016-01-02
    • 1970-01-01
    • 2019-02-12
    • 2021-05-27
    • 2021-12-02
    相关资源
    最近更新 更多