Nginx raises 404 when using format => 'js'

2019-07-16 13:27发布

问题:

I upload images to my App using Ajax and an Iframe. In Development everything works like a charm. But in production Nginx suddenly raises a 404 error. When I look into the log, the request never hits the Rails app. So I guess it has something to do with my Nginx configuration (maybe gzip compression).

The failing requests is send to "/images.js".

Any ideas on how to solve this? Google couldn't help me...

My Nginx config:

server {
      listen 80;
      server_name www.myapp.de;
      root /var/www/apps/myapp/current/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
      rails_env production;

      # set the rails expires headers: http://craigjolicoeur.com/blog/setting-static-asset-expires-headers-with-nginx-and-passenger
         location ~* \.(ico|css|js|gif|jp?g|png)(\?[0-9]+)?$ {
           expires max;
           break;
         }                                                                                     

      gzip  on;
      gzip_http_version 1.0;
      gzip_vary on;
      gzip_comp_level 6;
      gzip_proxied any;
      gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      # make sure gzip does not lose large gzipped js or css files
      # see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
      gzip_buffers 16 8k;

      # this rewrites all the requests to the maintenance.html
      # page if it exists in the doc root. This is for capistrano?^?^?s
      # disable web task
      if (-f $document_root/system/maintenance.html) {
        rewrite  ^(.*)$  /system/maintenance.html last;
        break;
      }

      # Set the max size for file uploads to 10Mb
      client_max_body_size 10M;
      error_page   500 502 503 504  /500.html;
     location = /500.html {
       root /var/www/apps/myapp/current/public;
     }


    }

回答1:

nginx will serve the request for /images.js from your root /var/www/apps/myapp/current/public since it matches

     location ~* \.(ico|css|js|gif|jp?g|png)(\?[0-9]+)?$ {
       expires max;
       break;
     }                                                                                     

(the break directive only applies to rewrite rules afaik so it can be removed)

If you want to serve /images.js from rails you need to enable rails for that location.



回答2:

It's worth noting that any .json requests will get caught if you use the regex above. Add a $ to avoid that.

location ~* \.(ico|css|js$|gif|jp?g|png)(\?[0-9]+)?$ {
    expires max;
    break;
}


回答3:

For this purpose I use this location directives:

location ~* ^.+\.(jpg|jpeg|gif|png|css|swf|ico|mov|flv|fla|pdf|zip|rar|doc|xls)$
{
    expires 12h;
    add_header  Cache-Control  private;
}
location ~* ^/javascripts.+\.js$
{
    expires 12h;
    add_header  Cache-Control  private;
}


回答4:

I ran into a very similar issue, and put format.json instead of format.js - this made a URL that had a .json extension, but allowed me to not modify my Nginx config.