Nginx 404's all static content in reverse proxy setup

user2778232 asked:

Whats strange is that this exact configuration used to work fine, but now all static files will 404 on direct link i.e

  • domain.com/test.html
  • domain.com/test/test.html
  • domain.com/ and domain.com/test/ << does work if index.html is present

this is the same for all static filetypes.

Nginx.conf :

user www;
worker_processes 8;
worker_rlimit_nofile 65536;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
  worker_connections 1024;
  use epoll;
}

http {
  server_names_hash_bucket_size 64;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  sendfile off;
  tcp_nopush on;
  tcp_nodelay on;

  proxy_cache_path                /web-cache/cache levels=2:2 keys_zone=basic:8m max_size=512m inactive=60m;
  proxy_temp_path                 /web-cache/cache_temp;

  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;

  ssl_session_cache    shared:SSL:10m;
  ssl_session_timeout  10m;

  #gzip compression options
  gzip on;
  gzip_static off;
  gzip_http_version 1.0;
  gzip_comp_level 9;
  gzip_min_length 128;
  gzip_buffers 16 8k;
  gzip_proxied any;
  gzip_types text/plain text/css text/xml text/javascript application/xml         application/xml+rss application/javascript application/json;
  gzip_disable "MSIE [1-6].";
  gzip_vary on;

  # include virtual hosts configuration
  include /etc/nginx/virtual.d/*.conf;
}

Virtualhost example :

server {
  listen 80;
  server_name domain.com www.domain.com;
  access_log off;
  error_log on;

  location / {
    proxy_pass http://127.0.0.1:6666;
    include /etc/nginx/nocache.conf;
  }
  location ~* ^.+.(htm|html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|bz2|pdf|odt|txt|tar|bmp|rtf|js|swf|avi|mp4|mp3|ogg|flv)$ {
    expires 30d;
    root /home/test/public_html;
  }
}

My answer:


The obvious problem is that you have placed root inside a location block. Best practice is to place it in the server block. This is one of the most common nginx misconfigurations.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.