简体   繁体   中英

Running Multiple Rails Apps on Single Nginx with Subdomains

I'm trying to figure out how to get a 2nd Rails app running on my server under a subdomain. Here's my setup:

(App1) Primary Rails app -> http://humani.se

(App2) Secondary Rails app -> http://supportme.humani.se

The domain name is purchased through GoDaddy, and currently I have the subdomain pointing to http://humani.se:3000 (through the GoDaddy panel), which you can see works by just visiting that URL. I know, I know -- App1 is running in production and App2 is running in development, not good practice. I'm just trying to see if I can get it working.

Nginx Conf:

# Primary Application Server
upstream humanise {
  # for UNIX domain socket setups:
  server unix:/home/jeff/srv/humani.se/tmp/sockets/unicorn.sock fail_timeout=0;
  # server localhost:8080;
}

server {
  server_name humani.se www.humani.se;

  # path for static files
  root /home/jeff/srv/humani.se/public;

  try_files $uri/index.html $uri.html $uri @humanise;

  location @humanise {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    # proxy_pass http://unix:/home/jeff/srv/humani.se/tmp/sockets/unicorn.sock;
    proxy_pass http://humanise;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

# Secondary Application Server
upstream supportme {
  server localhost:3000;
}

server {
  server_name supportme.humani.se;

  try_files $uri/index.html $uri @supportme;

  location @supportme {
    proxy_pass http://supportme;
    proxy_set_header Host $host;
    proxy_buffering off;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

Looks like it works, right? Nope! So all the routes seem to work EXCEPT from the Rails side. If you attempt to login (with any/no credentials), you will see the error, too. Basically, the URL helpers aren't working from the controller side. The ones in the views (anchor hyperlinks) work perfectly fine. From the server log:

Started POST "/login" for 127.0.0.1 at 2014-06-20 13:51:56 -0400
Processing by UsersController#login as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"eJvcm5auwCWFzw2BRY8DLzv8W5I3z0W529MXAJRLJb0=", "user"=>{"email"=>"ijt", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'ijt' LIMIT 1
Redirected to http://supportme.humani.se/
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)


Started GET "/.humani.se/" for 127.0.0.1 at 2014-06-20 13:51:56 -0400

ActionController::RoutingError (No route matches [GET] "/.humani.se"):
  actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.4) lib/rails/engine.rb:511:in `call'
  railties (4.0.4) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /home/jeff/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /home/jeff/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /home/jeff/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

You can clearly see it's redirecting to the correct URL but then gets /.humani.se/ instead. Someone somewhere must know about this!

Any and all help is appreciated!

Thanks in advance, - Jeff

First of all you should not let godaddy point to the port, point both humani.se and supportme.humani.se to the same IP, and let nginx redirect each to the right app, like you can run app1 on port 3000 and app2 on port 3001 then do a simply proxy on nginx

server {
  server_name humani.se;
  location / {
    proxy_pass http://localhost:3000;
  }
}
server {
  server_name support.humani.se;
  location / {
    proxy_pass http://localhost:3001;
  }
}

this should do the trick, tell me what you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM