【问题标题】:NGINX sometimes has (No such file or directory) errorNGINX 有时会出现 (No such file or directory) 错误
【发布时间】:2017-04-24 23:52:51
【问题描述】:

一段时间后,我的 nginx 服务器会出现关于静态文件的错误,我在一台服务器中使用 NGINX,该服务器提供静态文件并作为另一台服务器上 Django 应用程序的代理。

我将静态文件从 Django 服务器复制到 NGINX 服务器,然后运行./manage.py collectstatic 命令。

error.log

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;
    #}
#}

sites-enabled/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;
    }
}

settings.py

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

【问题讨论】:

  • 该错误似乎与媒体文件有关,而不是与静态文件有关。 settings.py 中的静态和媒体根目录和 url 是什么?
  • @AntonisChristofides 我已经更新了问题,但它也与静态文件有关。

标签: django ubuntu nginx


【解决方案1】:

这意味着文件不存在或文件具有错误的权限。

检查文件是否存在:

ls -lah /home/ws-admin/foo/media/branches/gmap__ndaUhjI.jpg
ls -lah /home/ws-admin/foo/static/js/notify.js

根据需要更新权限

chown -R ws-admin: /home/ws-admin/foo/media
chown -R ws-admin: /home/ws-admin/foo/static

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多