繁体   English   中英

尝试了所有内容,但仍无法通过nginx + gunicorn提供Django项目的静态文件

[英]Tried everything but still cannot serve static files of Django project via nginx+gunicorn

我必须通过nginx + gunicorn部署Django项目(nginx提供静态文件,gunicorn提供其他文件。)

项目设置文件夹中的dev.py文件具有以下设置-

STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'

nginx.conf看起来像这样-

server {
listen 8000;   
server_name  localhost;

access_log /home/user/access.log;
error_log /home/user/error.log;

location / {
    proxy_pass http://localhost:8001;
}

location /static/ {
    autoindex on;
    alias /home/user/project_revamped/project/static;
}

}

我将服务器运行为-

gunicorn project.wsgi:application --bind=127.0.0.1:8001

在URL上位于localhost:8001的服务器上访问时,显示的页面没有任何静态文件。

未填充nginx的error.log。 错误是浏览器试图从URL请求静态文件

localhost:8001/static/...

但是它应该从URL请求文件-

localhost:8000/static/...

因为nginx配置为通过该网址提供服务。

server {
  listen 8000;   
  server_name  localhost;
  root /home/user/project_dir/project;

  access_log /home/user/access.log;
  error_log /home/user/error.log;

  location /static/ {
    alias /home/user/project_dir/project/static/;
  }
  location / {
      proxy_set_header Host $http_host;
      proxy_pass http://localhost:8001;
      proxy_redirect default;
  }
  • 我认为这应该是针对您的情况的正确配置。
  • 理想情况下,您应该在端口80上监听所有http请求,因此在生产中,监听8000应该更改为80。
  • 如果您在代码之前/位置写静态内容,而在代码之后静态内容将永远无法到达位置/ static /
  • 必须有“ proxy_set_header主机$ http_host;” 这样,在debug = False(在生产中)时,允许的主机会获得一些价值

暂无
暂无

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

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