Sidekiq UI is not loading assets - 404 not found - ruby-on-rails

I am having rails 4.1 application running with sidekiq on production. I have deployed it using nginx + unicorn. Also I have mounted sidekiq UI as follows,
mount Sidekiq::Web => '/sidekiq'
but since last few days when ever I try to access sidekiq UI, all assets of sidekiq returning 404, not found. But it was working previously fine. But not able to find what leads 404.
Here is my settings
nginx+unicorn settings for my app
upstream sample_app {
server unix:/tmp/sample_app.sock fail_timeout=0;
}
server {
listen 80;
server_name www.sample_app.com;
root /home/deploy/applications/sample_app/current/public;
# set expire to all assets
location ~* \.(?:ico|css|js|gif|jpe?g|png|svg)$ {
expires max;
}
try_files $uri/index.html $uri #sample_app;
location #sample_app {
proxy_set_header X-Request-Start "t=${msec}";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://sample_app;
}
error_page 500 502 503 504 /500.html;
error_page 404 413 /404.html;
client_max_body_size 50M;
keepalive_timeout 10;
}

After debugging, I able to solve it by adding following line
# set expire to all assets
location ~* \.(?:ico|css|js|gif|jpe?g|png|svg)$ {
expires max;
try_files $uri #sample_app;
}

Related

Rails 4.2 Deploy NGINX, Puma, Capistrano, RBenv with SSL - 403 errors

I am getting 403 Forbidden. I have checked my nginx/error.log and get directory index of /home/deploy/apps/myapp/current/public/ is forbidden
I have SSL and did a check on https://www.digicert.com/help/ and see my certificates are set up OK.
I am wondering if it could be be perhaps because I don't have any index.html file in my public folder. I am running on Rails 4.2. My public folder has assets, 404.html, 422.html, 500.html, favicon.ico and robots.txt.
My question is, should I be doing something to set up an index.html in my public folder that lets me use my /app/views/static_pages/home.html.erb (set up as root static_pages#home in my routes file)?
I would guess Nginx isn't liking that there is not an index.html to find.
I also went into my static pages controller and added:
def home
render :file => 'public/index.html'
end
This is my nginx config file without SSL
upstream puma {
server unix:///home/laurie/apps/creativehub/shared/tmp/sockets/creativehub-puma.sock;
}
server {
listen 80 default_server deferred;
server_name creativecalgary.ca www.creativecalgary.ca;
root /home/laurie/apps/creativehub/current/public;
access_log /home/laurie/apps/creativehub/current/log/nginx.access.log;
error_log /home/laurie/apps/creativehub/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
This is my first Rails deploy. I am deploying on a digital ocean droplet using SSH on Cloud9 (with capistrano, puma, and nginx).

nginx won't serve all my assets (Ruby on Rails 4)

I have a Ruby on Rails app running under unicorn/nginx. The problem is that nginx won't serve all my assets. The CSS & JS files seem to be loaded but the images aren't served.
Here is my nginx conf file :
upstream unicorn {
server unix:/tmp/unicorn.aiccrr.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name exemple.com;
root /home/aiccrr/aiccrr/public;
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 1G;
keepalive_timeout 10;
}
unicorn.rb file :
root = "/home/aiccrr/aiccrr"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.aiccrr.sock"
worker_processes 2
timeout 30
I did rake assets:precompile at least 10 times today and added this line to the production.rb :
config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]
I'm running out of ideas. Do you have any idea please?
Thank you in advance.
Try the following:
Disable Rails asset server
# config/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
try following changes to your `nginx.conf
upstream unicorn {
server unix:/tmp/unicorn.aiccrr.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name exemple.com;
location / {
root /home/aiccrr/aiccrr/public;
location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
expires max;
gzip_static on;
add_header Cache-Control public;
break;
}
# If the file exists as a static file serve it directly without
# running all the other rewrite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
}
error_page 500 502 503 504 /500.html;
client_max_body_size 1G;
keepalive_timeout 10;
}
If it still doesn't work, post back with result of Log files & also URL to example nginx where you can replicate this issue. Thanks

setting up a virtual host with unicorn, nginx and capistrano in rails

I have been able to deploy my rials app into a vps system using nginx, unicorn and capistrano with no errors. Now, i want to deploy another rails app using the same nginx config(the two scripts are below) inside the same vps server and after running cap deploy:setup and cap deploy:cold it sets up correctly and the rails app is sent to the server. The problem i get is this. when i type service nginx restart i get the following error
nginx: [emerg] duplicate upstream "unicorn" in /etc/nginx/sites-enabled/cf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
my nginx script for the first app which is currently running is
upstream unicorn {
server unix:/tmp/unicorn.cf.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name cfmagazineonline.com;
root /home/deployer/apps/cf/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
my nginx config for the second rails app which fails to run but instead trows an error for the first rails app and makes it to crash is
upstream unicorn {
server unix:/tmp/unicorn.gutrees.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name gutrees.com;
root /home/deployer/apps/gutrees/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
any ideas how i can fix this issue and set up the virtual host correctly. Thank you
Name your second apps upstream differently (upstream names need to be unique). So instead of unicorn use i.e. the name #my_shiny_app_server. Then use this name proxy_pass directive http://my_shiny_app_server. Replace the my_shiny string with the name of your app e.g. gutrees, cf.

Serving various sites inside one VPS with nginx

I followed a tutorial in Rails Casts: #335 Deploying to a VPS. I have a linode VPS set up with an Ubuntu 10.04 image and I can deploy to it. Now it's serving:
/home/user0/first-rails-app/public (domain1.com)
I want to use nginx to serve another rails app and a php app, asume they are stored here:
/home/user1/php-app/public (domain2.com)
/home/user2/another-rails-app/public (subdomain.domain1.com)
My nginx.conf
upstream unicorn {
server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/user0/first-rails-app/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
All the domains and sub domain point to the server (x.x.x.x). I'm not sure how to use unicorn with two rails apps in the same server, even if I get virtual hosts correctly setup in nginx.conf.
Any suggestions where to start looking or what to modify?
Thanks!
You just have to declare multiple upstreams in your conf
upstream app1 { server unix:/tmp/app1.sock; }
upstream app2 { server unix:/tmp/app2.sock; }
server {
server_name http://www.example.com
location /foo {proxy_pass http://app1; break;}
location /bar {proxy_pass http://app2; break;}
}

Error Messages Showing in Production - Ruby on Rails 3.1, Nginx, Unicorn

I have a Rails 3.1 app running in production using Nginx and Unicorn. And for some reason, my custom 404 and 500 html error pages are not showing. Instead I'm getting the actual error message ("Routing Error", for example).
In my production.rb file, I have config.consider_all_requests_local = false
And on the same server with a nearly identical configuration, I have a 'staging' site that works just fine. The only difference, as far as I can tell, is that the production one has SSL while the staging does not.
Here is the Nginx config for the production app:
upstream unicorn_myapp_prod {
server unix:/tmp/unicorn.myapp_prod.sock fail_timeout=0;
}
server {
listen 80;
server_name myapp.com;
root /home/deployer/apps/myapp_prod/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_myapp_prod;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
server {
listen 443 default;
ssl on;
ssl_certificate /home/deployer/apps/myapp_prod/shared/ssl_certs/myapp_prod.crt;
ssl_certificate_key /home/deployer/apps/myapp_prod/shared/ssl_certs/myapp_prod.key;
server_name myapp.com;
root /home/deployer/apps/myapp_prod/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_myapp_prod;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Any ideas? Thanks!
The https listener's location #unicorn block is missing the X-Forwarded-For directive.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
It's in your http listener, but not the https listener.
Assuming that Rails' force_ssl is successfully redirecting all of the http requests and your only errors are happening on https requests, it seems that would explain it.
Also, to be very clear, there is a well known problem in Rack/Rails3 with respect to routing errors, which you specifically mention.
https://rails.lighthouseapp.com/projects/8994/tickets/4444-can-no-longer-rescue_from-actioncontrollerroutingerror
If you're using haproxy along with nginx and unicorn (e.g. you're on Engineyard), this fix won't be enough. You'll need to override Rails with something like this:
class ActionDispatch::Request
def local?
Rails.env != 'production'
end
end
Good luck!
not sure if this is applicable but we also have a link in our nginx config after the error_page line which handles the location of the /500.html pages
location = /500.html { root /path/to/rails/app/public; }
obviously substitute the path to rails app portion with your path.

Resources