【问题标题】:Nginx raises 404 when using format => 'js'Nginx 在使用 format => 'js' 时引发 404
【发布时间】:2009-11-15 11:37:36
【问题描述】:

我使用 Ajax 和 iframe 将图像上传到我的应用程序。在开发中,一切都像魅力一样工作。但在生产中 Nginx 突然引发 404 错误。当我查看日志时,请求从未命中 Rails 应用程序。所以我想这与我的 Nginx 配置有关(可能是 gzip 压缩)。

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

关于如何解决这个问题的任何想法? Google 帮不了我...

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


    }

【问题讨论】:

    标签: ruby-on-rails ajax format nginx


    【解决方案1】:

    nginx 将从您的根 /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。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        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】:

          我遇到了一个非常相似的问题,将 format.json 替换为 format.js - 这使得 URL 具有 .json 扩展名,但允许我不修改我的 Nginx 配置。

          【讨论】:

            猜你喜欢
            • 2017-12-01
            • 2018-11-06
            • 1970-01-01
            • 2017-05-16
            • 1970-01-01
            • 2013-08-23
            • 2019-05-12
            • 2019-05-10
            • 2019-05-10
            相关资源
            最近更新 更多