Pinax 与 Gunicorn 和 nginx 不提供静态资产

发布于 2024-12-29 08:34:44 字数 1165 浏览 3 评论 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 管理.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 文件夹都没有在正常站点上看到,但是对于管理部分,它们显示正常。

hi trying to get a nginx+gunicorn+django site up and running /it works well in development mode no errors or anything.configured the nginx for deployment with the following params

    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;
    }
}

my gunicorn is being called from within the django app with:
python manage.py run_gunicorn
i did this after collecting my static files into .../ASUT/site_media/static
only works in the dev mode tho .
i have tried substituting the location directive with

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

but my static assets are still not geting served all css/js/img folders arent getting seen for the normal site however for the admin section they show up ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2025-01-05 08:34:44

通过将 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;

}

sorted it by changing in settings.conf

STATIC_URL = "/static/"

and nginx.conf to

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文