0

嗨,尝试让 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 指令代替

    location /static/  {
    autoindex on;
        access_log off;
alias /home/wakwanza/Cod/NP/ASUT/site_media/;
    }

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

4

1 回答 1

2

通过更改 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;

}
于 2012-01-27T23:54:03.003 回答