Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching issue with Rails 4 and Nginx + Passenger

Occasionally my Rails Application might crash because of any bug and when accessing specific URLs at production, someone might land at the /500.html page.

So far everything is normal. We can see through the logs what the problem was and then fix. However, in order to view the page correctly we have to clear the browser cache, otherwise we are redirected again to /500.html

Is there anyway to prevent that?

I describe an example workflow below:

  1. Navigate to www.whatever.com/order/view/4444
  2. Because of a problem in our data/code the user is redirected to www.whatever.com/500.html
  3. We view the logs, identify the problem and fix it
  4. If I DO NOT clear the browser cache, after trying to navigate to www.whatever.com/order/view/4444 I am redirected to /500.html again
  5. If I clear the cache, everything is working normally

Is there anything we can do in Rails or Nginx configuration so that I won't have to clear the browser cache after changing the Rails app?

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

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;
}
like image 455
mentalic Avatar asked Nov 10 '22 02:11

mentalic


1 Answers

make sendfile off on nginx config file like the following

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        off;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.html index.htm;
like image 136
ratnakar vanapalli Avatar answered Nov 14 '22 23:11

ratnakar vanapalli