使用format =>时,Nginx会引发404; 'JS'

时间:2009-11-15 11:37:36

标签: ruby-on-rails ajax format nginx

我使用Ajax和iframe将图像上传到我的应用程序。在开发中,一切都像魅力一样。但在制作中,Nginx突然提出了404错误。当我查看日志时,请求永远不会访问Rails应用程序。所以我想这与我的Nginx配置(可能是gzip压缩)有关。

失败的请求将发送到“/images.js”。

关于如何解决这个问题的任何想法?谷歌无法帮助我...

我的Nginx配置:

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


    }

4 个答案:

答案 0 :(得分:1)

nginx将从你的root / var / www / apps / myapp / current / public提供/images.js的请求,因为它匹配

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

(break指令仅适用于重写规则afaik,因此可以删除)

如果您想从rails提供/images.js,您需要为该位置启用rails。

答案 1 :(得分:1)

值得注意的是,如果您使用上面的正则表达式,任何.json请求都会被捕获。添加$以避免这种情况。

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

答案 2 :(得分:1)

为此我使用了这个位置指令:

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

答案 3 :(得分:0)

我遇到了一个非常类似的问题,并将format.json代替format.js - 这会产生一个.json扩展名的网址,但允许我不修改我的Nginx配置。