NGINX sometimes has (No such file or directory) error
<p>After sometime my nginx server will have an error regarding static files, I'm using NGINX in one server that serve static files and as proxy for Django app on another server.</p>

<p>I copies the static files from the Django server to NGINX server, and run <code>./manage.py collectstatic</code> command.</p>

<p><strong>error.log</strong></p>

<pre><code>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/"
</code></pre>

<p><strong>nginx.conf</strong></p>

<pre><code>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;
    #}
#}
</code></pre>

<p><strong>sites-enabled/foo</strong></p>

<pre><code> 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;
    }
}
</code></pre>

<p><strong>settings.py</strong></p>

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