Caching issue with Rails 4 and Nginx + Passenger

Sometimes a Rails application may crash due to some kind of error, and when accessing certain URLs at the factory, someone may land on the /500.html page.

It's okay for now. We can see through the logs what the problem is and then fix it. However, to view the page correctly, we must clear the browser cache, otherwise we will be redirected to /500.html again

Is there any way to prevent this?

The following describes an example workflow:

  • Go to www.whatever.com/order/view/4444
  • Due to a problem with our data / code, the user is redirected to www.whatever.com/500.html
  • We review logs, identify the problem and fix it.
  • If I DO NOT clear the browser cache, after trying to go to www.whatever.com/order/view/4444, I am redirected to /500.html again
  • If I clear the cache everything works fine

Is there something we can do in the Rails or Nginx configuration so that I donโ€™t have to clear the browser cache after changing the Rails application?

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 '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; } 
+5
source share
2 answers

I found a mistake. To display custom error messages, we entered config / routes.rb. Thus, in fact, 500 and 422 response codes were handled as redirects (and therefore cached).

 get '/500', :to => redirect('/500.html') get '/422', :to => redirect('/422.html') 
0
source

make sendfile disabled in nginx configuration file as follows

 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; 
0
source

Source: https://habr.com/ru/post/1235319/


All Articles