我有一个夹层应用程序。我使用gunicorn作为web服务器,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;
}
}
}
我可以做些什么来提供静态文件?
答案 0 :(得分:0)
这似乎是在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/;
}
定义指定位置的替换。例如,使用以下配置
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; }