繁体   English   中英

Django | Nginx提供的静态文件不被客户端使用

[英]Django | Static files served by nginx not used by Client

我有一个django应用,其中包含静态文件的以下设置:

STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_ROOT = '/opt/static/'

我正在使用follwoing gunicorn命令运行django: gunicorn evee.wsgi -b 0.0.0.0:8000

我已经配置nginx使用以下conf服务静态文件和ssl:

  server {
    keepalive_timeout 5;
    listen 443 ssl;
    server_name api.home.com;
    client_max_body_size 4G;
    error_page 500 502 503 504 /500.html;

    # path for static files
    root /opt;

    location / {
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Ssl off;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Port 80;
      proxy_set_header X-Forwarded-Proto $scheme;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://evee:8000;
    }
  }

有趣的是,我能够在客户端中看到CSS。 例如,对https://secapi.ril.com/static/admin/css/base.css的请求成功,并返回200响应。 我可以在提到的URL上查看所有静态文件,但是django似乎没有使用它们。 尝试更改客户端以及私有模式。

我做错什么了吗? 这是我上次检查的时间。

尝试在您的nginx配置文件中添加静态文件路径,如下所示:

location /static/ {
    alias   /opt/static/;
}

这里提到您的静态文件夹的完整路径。 我猜你的情况是/opt/static/

这是我解决此问题的方法。 必须编辑nginx.conf文件以配置上游,而不是将其直接重定向到http,并删除了一些正在设置的标头。 不知道它有什么不同或为什么起作用。 整个设置在Docker Swarm中运行。

#### SECAPI #####
  upstream app_server {
    # for a TCP configuration
    server evee:8000 fail_timeout=0;
  }

  server {
    keepalive_timeout 5;
    listen 443 ssl;
    server_name api.home.com;
    client_max_body_size 4G;
    error_page 500 502 503 504 /500.html;

    # path for static files
    root /opt;

    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 X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://app_server;
    }

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM