Rails 4和Nginx + Passenger的缓存问题

men*_*lic 5 caching ruby-on-rails nginx

有时我的Rails应用程序可能因任何错误而崩溃,并且在生产时访问特定URL时,有人可能会登陆/500.html页面.

一切都很正常.我们可以通过日志查看问题是什么然后修复.但是,为了正确查看页面,我们必须清除浏览器缓存,否则我们会再次重定向到/500.html

反正有没有阻止它?

我在下面描述一个示例工作流:

  1. 导航到www.whatever.com/order/view/4444
  2. 由于我们的数据/代码存在问题,用户被重定向到www.whatever.com/500.html
  3. 我们查看日志,确定问题并进行修复
  4. 如果我不清除浏览器缓存,尝试导航到www.whatever.com/order/view/4444后,我再次被重定向到/500.html
  5. 如果我清除缓存,一切正常

我们可以在Rails或Nginx配置中做些什么,这样我就不必在更改Rails应用程序后清除浏览器缓存了吗?

nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.html index.htm;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

myapp.conf

server {
    listen 80;
    server_name example.com www.example.com;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/xxx/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /usr/local/rvm/gems/ruby-2.0.0-p643/wrappers/ruby;
    passenger_friendly_error_pages on;
}
Run Code Online (Sandbox Code Playgroud)

men*_*lic 0

我发现了错误。我们在 config/routes.rb 中输入了以下内容以显示自定义错误消息。因此,实际上 500 和 422 响应代码被作为重定向处理(因此被缓存)。

get '/500', :to => redirect('/500.html')
get '/422', :to => redirect('/422.html')
Run Code Online (Sandbox Code Playgroud)