一段时间后,我的 nginx 服务器会出现有关静态文件的错误,我在一台服务器上使用 NGINX 来提供静态文件,并在另一台服务器上将其作为 Django 应用的代理。
我将静态文件从 Django 服务器复制到 NGINX 服务器,然后运行./manage.py collectstatic
命令。
错误日志
2016/12/07 20:36:17 [error] 26548#0: *1359 open() "/home/ws-admin/foo/media/branches/gmap__ndaUhjI.jpg" failed (2: No such file or directory), client: x.x.x.x, server: _, request: "GET /media/branches/gmap__ndaUhjI.jpg HTTP/1.1", host: "www.foo.com", referrer: "http://www.foo.com/"
2016/12/08 21:34:14 [error] 20474#0: *8608 open() "/home/ws-admin/foo/static/js/notify.js" failed (2: No such file or directory), client: x.x.x.x, server: _, request: "GET /static/js/notify.js HTTP/1.1", host: "www.foo.com", referrer: "http://www.foo.com/signup/"
nginx.conf
user ws-admin;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# TCP/UDP proxy and load balancing block
#
#stream {
# Example configuration for TCP load balancing
#upstream stream_backend {
# zone tcp_servers 64k;
# server backend1.example.com:12345;
# server backend2.example.com:12345;
#}
#server {
# listen 12345;
# status_zone tcp_server;
# proxy_pass stream_backend;
#}
#}
已启用站点/foo
upstream app_server {
server x.x.x.x:8000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/ws-admin/foo/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/ws-admin/foo/static;
expires 365d;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
设置.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')