Nginx, unicorn and multiple rails apps in subrirectories - ruby-on-rails

I'm messing around with aws, and want to deploy multiple apps to my free tier aws account.
I'd like to have nginx point to "ec-2-site.com/first-app" and "ec-2-site.com/second-app.
Here are my current config files (basically guess and checking from this railscast
upstream unicorn_chaos {
server unix:/tmp/unicorn.chaos.sock fail_timeout=0;
}
upstream unicorn_blog {
server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}
server {
listen 80 default deferred;
location /chaos/ {
#server_name http://ec2-50-16-81-170.compute-1.amazonaws.com/chaos;
root /home/deployer/apps/chaos/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_chaos;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
location /blog/ {
# server_name example.com;
root /home/deployer/apps/blog/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)blog;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
}
Here is the error I'm getting:
nginx: [emerg] named location "#unicorn_chaos" can be on the server level only in /etc/nginx/sites-enabled/chaos:23
Obviously that #unicorn_appname directive shouldn't be there, but where should it be? Am I going bout this all wrong?
Thanks

I didn't check the railscast but I've found some problems in your configuration.
First is on line 50 you misspelled the address.
Second is that you should put named locations out of the locations.
Your hierarchy looks like this
server
location app1
try_files #unicorn
location #unicorn
proxy_pass unicorn_app1
location app2
try_files #unicorn
location #unicorn
proxy_pass unicorn_app2
Try this
server
location app1
try_files unicorn_app1
location #unicorn_app1
proxy_pass unicorn_app1
location app2
try_files #unicorn_app2
location #unicorn_app2
proxy_pass unicorn_app2
The important is to keep their names unique, and put them in the same server level.

Related

Rails + Nginx + Unicorn - The webpage has a redirect loop

I have a rails project that I just deployed to my AWS server. But when I access the IP address of the instance it returns:
The webpage has a redirect loop
This is my nginx conf file
/etc/nginx/conf.d/myapp.conf
upstream unicorn {
server unix:/tmp/sockets/unicorn.sock
fail_timeout=0;
}
server {
listen 80;
server_name 12.34.56.78;
root /home/ubuntu/var/www/myapp/current/public;
return 301 http://12.34.56.78$request_uri;
access_log on;
}
server {
listen 443 default;
server_name 12.34.56.78;
root /home/ubuntu/var/www/myapp/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 20M;
keepalive_timeout 10;
}
I am using nginx + unicorn.
Can anybody shed some light into what I might be doing wrong here. Any help will be greatly appreciated.
Thank you.

rails application deployment nginx, unicorn,capistrano socket error

Hi I am deploying rails application to digital ocean VPS, I have followed https://coderwall.com/p/yz8cha this blog , all things done well, but now the browser shows only
a blank white page
In nginx log file it shows
invalid host in upstream "/tmp/unicorn.testvpsdo.sock" in /etc/nginx/sites-enabled/testvpsdo:2
What causes the error?,
this is my nginx.conf file
upstream unicorn {
server unix:/tmp/unicorn.testvpsdo.sock fail_timeout=0;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/navin/apps/testvpsdo/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 20M;
keepalive_timeout 10;
}

Rails page cache multiple servers with nginx unicorn

I am using the page caching gem on Rails 4. I have a web server running nginx, app server running unicorn and rails, and db server running postgre.
When page cache is generated on the app server, nginx will not serve the static files. Only after I set
config.serve_static_assets = true
where the page cache will work in production. I don't think this is ideal though given that now rails is serving the static file.
How do I get nginx to serve my page caches located on the app server?
Here is my nginx config:
upstream unicorn {
server <%= app_private_ip %>:8080 fail_timeout=0;
}
server {
# listen [::]:80 ipv6only=on default_server;
listen 80 default deferred;
server_name <%= domain %>;
# rewrite ^(.*) https://<%= domain %>$1 permanent;
root <%= current_path %>/public;
sendfile on;
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
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;
server_tokens off;
}
server {
listen 443;
server_name <%= domain %>;
ssl on;
ssl_certificate /home/<%= user %>/ssl/<%= domain %>.pem;
ssl_certificate_key /home/<%= user %>/ssl/<%= domain %>.key;
root <%= current_path %>/public;
sendfile on;
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
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;
server_tokens off;
}
The nginx try_files directive lets you set cascading ways to resolve the static file for a URI in different locations/backends.
It looks like you need to move your try_files directive into a location block for it to work properly:
location / {
try_files $uri #unicorn;
}
This should tell nginx to try resolve paths locally by URI before passing the request to your unicorn backend.
The page caching gem asks you to set the cache directory to 'public/cache' in
application.rb;
config.action_controller.page_cache_directory = "#{Rails.root.to_s}/public/cache"
so your try_files line should be;
try_files /cache/$uri/index.html /cache/$uri #unicorn;
otherwise you can just set the page_cache_directory to;
"#{Rails.root.to_s}/public" and not change your current nginx config.

NGINX CONF with RAILS on LINODE

This is my nginx.conf file
upstream unicorn {
server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/deployer/apps/blog/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;
}
I tried to add SSL - I redeplyed and restarted NGINX and the server. I undid everything redeployed and it was back up.... Now I tried the same thing. Tried to add SSL it failed. Undid. Now server is down. Here is the github with revisions!
Ran /etc/init.d/unicorn_blog start and restarted unicorn. It worked

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