rails app on nginx+passenger not showing custom error pages - ruby-on-rails

I have a Rails app running on nginx 1.2.0 and passenger 3.0.7. I would like to have the custom error pages in the rails app (e.g. /rail_app/public/500.html) be displayed when the appropriate http error occurs within the app.
Here is my current nginx config file:
http {
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.7;
passenger_ruby /usr/bin/ruby;
include mime.types;
default_type application/octet-stream;
#access_log /opt/nginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
server {
listen 80;
server_name localhost;
root /var/www/dashboard/current/public;
passenger_enabled on;
passenger_min_instances 1;
# listen 443;
# ssl on;
# ssl_certificate /opt/nginx/conf/server.crt;
# ssl_certificate_key /opt/nginx/conf/server.key;
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/dashboard/current/public/;
}
}
}
This configuration does not show the rails app customer error page rather just sends the http error status code to the client.
Anyone know what it takes to have nginx/passenger send the rails app custom error page to the client with the http error status code?

Please try the following:
# We use the x just because it is for all 5xx errors.
error_page 500 502 503 504 /5xx.html;
location = /5xx.html {
alias /var/www/dashboard/current/public/;
}
Reconfiguring the root directive makes no sense, as it is already set to the path you specified before. The alias ensures that the specific location is internally matched to a different location on the file system. All incoming request parameters should be passed along and if your Rails app is taking care of things at this point it should answer. Just make sure that your Rails app isn't answering with a 500 status again (I don’t know what would happen then).
Related Links
alias

You're probably missing passenger_intercept_errors on; in your nginx config
see the passenger docs for this directive for more info

The config I use:
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

Related

Spree not loading images on Nginx

I'm trying to make images load on Spree 3.1.0 + Passenger 5.0.30 + Nginx + Capistrano 3.5 + Digital Ocean, I have followed Go Rails Tutorial and Spree documentation but no luck at the moment.
Weird... Is loading my css files but not loading my image-background
my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name rodeobest.com;
passenger_enabled on;
rails_env production;
root /home/deployer/RMG_rodeobest/current/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# if the request is for a static resource, nginx should serve it dir$
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
location ~ ^/(spree|system|assets)/ {
root /home/deployer/RMG_rodeobest/current/public;
expires max;
break;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
root /home/deployer/RMG_rodeobest/current/public;
}
}
As documentation says, I've changed location ~ ^/(system|assets)/ to location ~ ^/(spree|system|assets)/ and restart the server, but as I said, no luck. Any idea? thanks in advance
References:
Reference 1
Reference 2

Rails 3 running in subdirectory, assets not found

I'm serving two Rails sites using nginx. Rails1 does not use the asset pipeline but Rails2 does. Rails2 also uses a prefix to differentiate it from Rails1. For example:
http://myhost -> Rails1
http://myhost/abc -> Rails2
Both sites are running, however any reference to assets on the Rails2 site are not found.
Here's what my pseudo nginx.conf looks like:
http {
upstream rails1 {
server 127.0.0.1:3000;
}
upstream rails2 {
server 127.0.0.1:3030;
}
server {
location ~ ^/assets/ {
expires max;
add_header Cache-Control public;
access_log off;
}
location /abc {
proxy_pass http://rails2;
}
location / {
proxy_pass http://rails1;
}
}
}
Also, the routes.rb in my Rails 2 app:
Rails2App::Application.routes.draw do
scope '/abc' do
resources :projects
root :to => 'home#index'
end
end
Browsing to http://myhost/abc/ for the Rails2 app, brings up the page with no css, and the following error:
GET http://myhost/assets/application-asdasd.css 404 (Not Found)
I've tried using config.assets.prefix = '/abc' in the production.rb file but it didn't work. I've also tried different variations in the ngnix.conf file to no avail either.
Anyone know what I'm doing wrong, or missing?
UPDATE
I'm not quite sure why, but I was able to get this to (incorrectly) work using an #location instead of an upstream. But I had to move the assets folder from the Rails2 app to the Rails1 app. Not exactly ideal.
Changes to the server section:
location ~ ^/(assets)/ {
expires max;
add_header Cache-Control public;
access_log off;
}
location ~ ^/(abc)/ {
root /rails2/public;
try_files $uri/index.html $uri.html $uri #rails2;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location / {
root /rails1/public;
try_files $uri/index.html $uri.html $uri #rails1;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location #rails1 {
proxy_pass http://127.0.0.1:3000;
}
location #rails2 {
proxy_pass http://127.0.0.1:3030;
}
From a similar question: https://stackoverflow.com/a/3355589/417872
Have you tried this?
config.action_controller.relative_url_root = '/rails_app'
I would recommend googleing for how to correctly serve a rails app from a subdirectory. That's the real issue you're dealing with, and a quick search returned several pages of useful looking links.
I had a similar problem running Rails 4 on Nginx in production environment. The solution I found was to specify the root path for the asset location in nginx.conf:
location ^~ /assets/ {
root /home/rails/myapp/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
Hope this helps

nginx with passenger don't handle static assets

I have rails app running with helpfull nginx and passenger, and I want to add static page (conteins code coverage analysis tool - simplecov).
Localy this works fine (without passenger), but on the server this don't works.
My nginx.conf:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
#speed up for linux 2.6+
use epoll;
}
http {
passenger_root /home/demo/.rvm/gems/ruby-1.9.3-p0#gm/gems/passenger-3.0.9;
passenger_ruby /home/demo/.rvm/wrappers/ruby-1.9.3-p0#gm/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name *.dev.mysite.com;
root /var/www/projects/mysite/qa/current/public;
passenger_enabled on;
rails_env qa;
charset utf-8;
error_log /var/www/projects/mysite/qa/shared/log/host.error.log;
}
#Coverage code tool (SimpleCov gem)
server {
listen 4444;
server_name coverage.mysite.com;
location / {
root /var/lib/jenkins/jobs/WebForms/workspace/coverage;
index index.html index.htm;
}
}
#Yard server
server {
listen 5555;
server_name yard.mysite.com;
location / {
proxy_pass http://127.0.0.1:8808;
}}}
And nothing receive when I try to hit to coverage.mysite.com:4444.
I think I remember coming across something similar to this on one of my rails apps.
Have you tried commenting and uncommenting the lines below?:
# in config/environments/production.rb
# Specifies the header that your server uses for sending files
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
# For nginx:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
It should be near the top, around line 12 through 16.
Try that, then redploy and test in the browser.

Maintenance page with nginx for one domain_name among others

I have a rail app that serves multiple domain_name and is deployed by nginx & passenger. I need to put one domain under maintenance mode while the other still work as usual. Here is my config:
server {
listen 80;
server_name domain1.com domain2.com domain3.com domain4.com;
error_page 503 http://$host/maintenance.html;
location /maintenance.html {
# Allow requests
}
location / {
root /var/www/myapp/public; # <--- be sure to point to 'public'!
error_page 503 http://$host/maintenance.html;
passenger_enabled on;
rails_env development;
passenger_use_global_queue on;
if (-f /var/www/myapp/public/maintenance.html) {
return 503;
}
}
}
The above config would cause all domains under maintenance. However, I want to put domain1.com is under maintenance mode. How would I achieve this?
you can add another server entry for server "domain1.com" which serve request for this domain only. like:
server {
listen 80;
server_name domain1.com
error_page 503 http://$host/maintenance.html;
root /your/root/directory/
if (-f $document_root/maintenance.html){
rewrite ^(.*)$ /maintenance.html last;
break;
}
location /maintenance.html {
# Allow requests
}}
you need to ensure following
The "domain1.com" should be removed from previous server
entry
maintenance.html page should be
present in /your/root/directory/

nginx rewrite rules with Passenger

I'm trying to migrate to nginx from Apache using Passenger in both instances to host a Rails app. The app takes a request, which is for an image- if the image exists at /system/logos/$requestedimage then it should get served, or it should be allowed to hit the Rails app to generate it if needed (where it is then cached to /system/logos).
In Apache I used the following:
RewriteCond %{DOCUMENT_ROOT}/system/logos/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1
This worked fine. The assets. subdomain is another subdomain but with the same root, just Passenger disabled, specifically set up for hosting static files (expires-wise).
In nginx I am using the following:
server {
listen 80;
passenger_enabled on;
server_name clg.eve-metrics.com www.clg.eve-metrics.com;
root /opt/www/clg/current/public;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml text/css application/javascript;
gzip_disable msie6;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
if (-f $document_root/system/logos$request_filename) {
rewrite ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1 break;
}
}
This doesn't work so well. At all, in fact. It never redirects to the cached path and it never hits the Rails app. It's like nginx is assuming it's a static asset so not passing it on to Passenger. Is there a way to stop this behaviour so it hits the app?
My rails application is running on nginx and passenger. I have moved my rails cache directory from the default /public to /public/system/cache/. To make it work, I had to insert this into my vhost config file:
if (-f $document_root/system/cache/$uri/index.html) {
rewrite (.*) /system/cache/$1/index.html break;
}
if (-f $document_root/system/cache/$uri.html) {
rewrite (.*) /system/cache/$1.html break;
}
I remember that I too tried to make it work with $request_filename, but didn't get it to work. Try with $uri instead and see if it works :-)
James, please try this configuration file
https://gist.github.com/711913
and pay attention on this location config:
location ~* \.(png|gif|jpg|jpeg|css|js|swf|ico)(\?[0-9]+)?$ {
access_log off;
expires max;
add_header Cache-Control public;
}
passenger won't let Rails to manage your assets files if you have right permissions (user run nginx should has permissions to access to file directly)

Resources