I developed two ruby on rails apps. I own to domains potato.domain.com and orange.domain.com both pointing to the same IP address of my linux server.
I want to deploy one rails application to each domain. To do this i used Puma and Nginx and followed this tutorial and succesfully deployed one of the sites (lets call it potato.domain.com).
Now, when i try to deploy the other domain, i follow the exact same steps i followed for potato.domain.com(replacing references to the potato app with de orange app) but once im finished if i try to access orange.domain.com i end up always in potato.domain.com.
Here is the puma.rb file that i'm using for both apps:
# Change to match your CPU core count
workers 1
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
And here are the files under nginx/sites-available (and both are mapped to sites-enabled automatically):
Default file (potato.domain.com):
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:/home/username/potato/site/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name potato.domain.com;
root /home/username/potato/site/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
And orange file (orange.domain.com):
upstream events {
# Path to Puma SOCK file, as defined previously
server unix:/home/username/orange/site/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name orange.domain.com;
root /home/username/potato/site/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Does anyone notice something wrong in any file? or know any good tutorial to do this?
Sorry for the long post and thanks!
UPDATE:
I merged the two files in nginx/sites-enabled and by looking at the logs it seems to be routing OK.
The problem now is that the puma.sock file is not being created for one of the apps, log error:
17:45:05 [crit] 2444#0: *16 connect() to unix:/home/username/orange/site/shared/sockets/orange-puma.sock failed (2: No such file or directory) while connecting to upstream, client: IP, server: orange.domain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/username/orange/site/shared/sockets/orange-puma.sock:/", host: "orange.domain.com", referrer: "http://orange.domain.com/"
The puma.rb for orange looks like this now:
# Change to match your CPU core count
workers 1
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/events-puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/events-puma.pid"
state_path "#{shared_dir}/pids/events-puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
I seem to have something missconfigures with puma somewhere, any help? Thanks
I thing the main reason is that you are giving both virtual servers the same root directive.
Change orange to, I guess:
root /home/username/potato/site/public;
But maybe it's a typo for the example only.
Secondly, using upstream events, you are then calling @app
. You should really call @events
.... Like so:
try_files $uri/index.html $uri @events;
location @events {
proxy_pass http://events;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}