【问题标题】:Pinax with gunicorn and nginx not serving static assets带有 gunicorn 和 nginx 的 Pinax 不提供静态资产
【发布时间】:2012-02-20 01:08:29
【问题描述】:

您好,尝试启动并运行 nginx+gunicorn+django 站点/它在开发模式下运行良好,没有错误或任何东西。使用以下参数配置 nginx 以进行部署

    upstream my-backend {
    server localhost:8000 fail_timeout=0;
}

server {
    listen 80;

    root /home/wakwanza/Cod/NP/ASUT;

    keepalive_timeout 5;

    location /site_media/ {
    autoindex on;
        access_log off;
    }

    location /static/  {
    autoindex on;
        access_log off;
    }

    location / {
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   REMOTE_HOST      $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-FORWARDED-PROTOCOL $scheme;

    proxy_redirect off;

        proxy_pass http://my-backend;
    }
}

我的 gunicorn 在 django 应用程序中被调用: python manage.py run_gunicorn 我在将我的静态文件收集到 .../ASUT/site_media/static 之后这样做了 仅适用于开发模式。 我尝试用

替换位置指令
    location /static/  {
    autoindex on;
        access_log off;
alias /home/wakwanza/Cod/NP/ASUT/site_media/;
    }

但是我的静态资产仍然没有得到服务,所有 css/js/img 文件夹在普通站点上都没有被看到,但是对于管理部分它们显示正常。

【问题讨论】:

    标签: django static nginx pinax gunicorn


    【解决方案1】:

    通过更改 settings.conf 对其进行排序

    STATIC_URL = "/static/"
    

    和 nginx.conf 到

    upstream app_server {
        server localhost:8000 fail_timeout=0;
        # For a TCP configuration:
        # server 192.168.0.7:8000 fail_timeout=0;
    }
    
    server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;
    
        keepalive_timeout 5;
    
        # path for static files
        #root /home/wakwanza/Cod/NP/ASUT/site_media/static;
    
        location /static/ {    
        autoindex on;    
        alias   /home/wakwanza/Cod/NP/ASUT/site_media/static/;    
        }
    
        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }
    
        location @proxy_to_app {
            proxy_pass_header Server;
            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;
        }
    
        error_page 500 502 503 504 /500.html;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 2017-05-31
      • 2015-11-30
      • 2014-01-13
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多