English 中文(简体)
Nginx raises 404 when using format => js
原标题:

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


    }
最佳回答

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.

问题回答

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

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

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.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签