Reputation: 150
I have one Rails app running on Digital Ocean with Unicorn, NGINX on Ubuntu and I'm trying to handle a bunch of subdomains such as app1.example.com, app2.example.com, etc.
In my routes I'm doing this:
constraints(Subdomain) do
match '/', to: 'pages#landing', via: [:get, :post]
end
Which is helping me catch subdomain prefixes and show the corresponding landing page from the controller. Locally it works great but NGINX seems to be redirecting to the root path of the app no matter what.
Here is my NGINX config:
upstream app_server {
server unix:/var/run/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com *.example.com;
return 301 https://$server_name$request_uri;
}
#When the wildcard above didn't work,
#I tried hardcoding below but still nothing
server {
listen 80;
server_name app1.example.com;
return 301 https://$server_name$request_uri;
}
server {
root /home/rails/example/public;
index index.htm index.html;
listen 443 ssl spdy;
listen [::]:443 ssl spdy;
...
ssl_protocols TLSv1.1 TLSv1.2;
# ssl_ciphers
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
...
resolver 8.8.8.8 8.8.4.4 valid=86400;
resolver_timeout 10;
location / {
try_files $uri/index.html $uri.html $uri @app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
try_files $uri @app;
}
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
What am I missing here?
Upvotes: 3
Views: 586
Reputation: 1639
If anyone having the issue that all subdomain routes are redirecting to the root path (on localhost and in production), please check that your subdomains are defined before the root domains in the routes.rb
. Otherwise they will always redirect to the landing page.
routes.rb
# all subdomains go first
get '/', to: 'pages#status', constraints: { subdomain: 'status' }
# home (landing page)
root to: 'pages#landing', as: 'landing'
routes.rb
# landing page
root to: 'pages#landing', as: 'landing'
# subdomains => will be redirected to landing page...
get '/', to: 'pages#status', constraints: { subdomain: 'status' }
Upvotes: 0
Reputation: 766
server {
listen 80;
server_name example.com;
}
This above should be all you need. You don't need to redirect to accept subdomains, nor do you need to use a wildcard. And remove the "return"s
Upvotes: 0
Reputation: 26
I think you should be using the $host variable rather than $server_name, as $server_name can be just the first value in the list of hostnames and you want to use the hostname that the user has specified in the http headers.
server {
listen 80;
server_name example.com *.example.com;
return 301 https://$host$request_uri;
}
Also if your hard coded example is after the wildcard server block then it wouldn't ever get matched because the requests will always match the one before it and thats probably why that didn't work.
Upvotes: 1