![](/img/trans.png)
[英]docker nginx django gunicorn not serving static files in production
[英]nginx+gunicorn+django/mezzanine not serving static files
我有一个夹层应用程序。 我正在使用gunicorn作为网络服务器,并使用nginx作为反向代理。 但是,不会提供静态文件(但是,它们是通过内置的django开发服务器提供的)。 我已经运行了python manage.py collectstatic。
这是我的nginx.conf文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to serve static files
location ^~ /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /root/myapp/myapp/static/;
}
# Serve a static file (ex. favico)
# outside /static directory
location = /favico.ico {
root /app/favico.ico;
}
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
我该怎么做才能提供静态文件?
这看起来像是在nginx中错误使用root
vs alias
。 假设您在settings.py文件中具有:
STATIC_URL = '/static/'
那么您想要一个看起来像这样的位置:
location /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /root/myapp/myapp/;
# - or -
# alias /root/myapp/myapp/static/;
}
对于alias
:
定义指定位置的替换。 例如,使用以下配置
location /i/ { alias /data/w3/images/; }
根据“ /i/top.gif”的请求,将发送文件/data/w3/images/top.gif。 ...当location与指令值的最后一部分匹配时:
location /images/ { alias /data/w3/images/; }
最好改用root指令:
location /images/ { root /data/w3; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.