Issue with Nginx redirecting to Rails app - ruby-on-rails

So, I have a domain: coolapp.com Now I want to make it so I can have two subdomains, such as game.coolapp.com and sports.coolapp.com. I have two server configurations in the nginx config as follows:
server {
listen 80;
server_name sports.coolapp.com;
location / {
root /home/deployer/Sports/current/public;
index index.php;
}
passenger_enabled on;
}
server {
listen 80;
server_name game.coolapp.com;
location / {
root /home/deployer/Game/current/public;
index index.php;
}
passenger_enabled on;
}
For some reason, both of these domains are redirecting to the 'sports' app. They SHOULD each by redirecting to their own application, but are interfering with each other.
I am running the Sports app via passenger, with something like 'passenger start --port 80', and I'm running the second app with 'passenger start --port 81'. Should I be running these with different arguments, or what exactly is causing the issue here?

Your problem is not quite clear to me. I am making the following assumptions in my answer.
You are running 3 apps all on Phusion with Nginx.
Nginx configuration is pretty straight forward. Probably you have configured nginx.conf properly because you main app is working.
The next thing you should do is read the phusion passenger doc on how to run Rails on subdomain. Reading https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#deploying_a_rack_app will give you a lot of insight. I recommend you read the entire doc.
I think there is a problem in the way you have configured your nginx.conf and how you are starting you phusion passenger (BTW I think you do not even have to start the phusion passenger explicitly and that NGINX should take care of that)
In your nginx configuration you are listening to port 80 and in your phusion passenger command you are asking it to listen to port 81 and 82. Change you nginx configuration to read like (BTW it is better to use sites-available & site-enabled for this)
server {
listen 80;
server_name sports.coolapp.com;
root /home/deployer/Sports/current/public;
passenger_enabled on;
}
server {
listen 80;
server_name games.coolapp.com;
root /home/deployer/Sports/current/public;
passenger_enabled on;
}
I hope you have configured your subdomains properly. You should double check that as well.
Save changes. Restart Nginx and you should be good to go.

Related

Still seeing welcome to ngnix after setup

I have set up and deployed an application using Capistrano and set up the server and ngnix config file, however, I'm still seeing the Welcome to ngnix welcome screen.
Here is what I have in the /etc/nginx/sites-enabled/default file
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name IP;
passenger_enabled on;
rails_env production;
root /home/poladmin/poetry-out-loud-v2/current/public/;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
But as I said when I go to the server IP all I see is
Since you edited /etc/nginx/sites-enabled/default file, try to reload nginx with nginx -s reload command (if you are using Linux, or similar depending on your operating system and nginx version).
As you have edited the nginx config file you can try to restart nginx by doing sudo /etc/init.d/nginx restartto make the config file take effort. Also do not forget to start the real ruby server and make sure it is listening to the same port you configured through nginx.(some times the ruby app may be running on a different port)

cannot assess rails app on amazon ec2

I'm using Amazon EC2 and tried to deploy a rails project.
I have install nginx/passenger and successfully made nginx server run.
I started my rails project with name "forfun"
then I set the root of nginx to /home/ubuntu/rails/forfun/public
I initialized a file named "index.html", then I could see the page in browser
(http://[my ip]:80)
However, what i really wanna see is the welcome page of rails app.
I tried do remove index.html and see what i got. I saw 404 Forbidden error.
/var/log/nginx/error.log reveals that directory index of "/home/ubuntu/rails/forfun/public/" is forbidden
What step i actually missed?
by the way, do i need to do "rails server" while nginx is running?
ps:
(1) /opt/nginx/conf/nginx.conf
http {
passenger_root /usr/local/rvm/gems/ruby-2.3.0/gems/passenger-5.0.26;
passenger_ruby /usr/local/rvm/gems/ruby-2.3.0/wrappers/ruby;
...
server {
listen 80;
server_name 52.196.XX.XXX;#my amazon public ip
root /home/ubuntu/rails/forfuni/public;
}
(2)/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/ubuntu/rails/forfun/public;
index index.html index.htm;
}
Try adding /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
server_name 52.196.XX.XXX;#my amazon public ip;
passenger_enabled on;
passenger_app_env development; #or whatever rails env you want.
The whole process of deploying a rails app using nginx/passenger is documented here. See the nginx section.
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-ubuntu-14-04

Running multiple rails app using passenger

I am hosting a Rails app using passenger and nginx.
I have an nginx.conf with a working server block for the first app that looks somewhat like this.
server {
listen 80;
server_name app1.example.com www.app1.example.com;
passenger_enabled on;
root /home/ec2-user/app1/public;
}
However, I want to deploy a second rails app and on the same nignx.conf wrote:
server {
listen 80;
server_name app2.example.com www.app2.example.com;
passenger_enabled on;
root /home/ec2-user/app2/public;
}
I think there is a conflict with the the ports but I am having trouble finding answers to solving this.

Should I enable Rails force_ssl although nginx handles redirects?

Scenario
I am running Phusion Passenger through nginx.
I have configured nginx to use SSL and reroute all HTTP traffic to HTTPS.
Do I still need to enable the force_ssl option in my Rails app?
If so, why and what are the advantages?
If I don't enable it, what security risks do I present?
nginx config:
server {
listen 80;
server_name myapp.com
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name myapp.com;
ssl_certificate /etc/ssl/certs/nginx.pem;
ssl_certificate_key /etc/ssl/certs/nginx.key;
root /home/user/rails/app/public;
passenger_enabled on;
}
The force_ssl option accomplishes the same thing as the nginx rewrite rule, so you don't need the force_ssl option. force_ssl can give you more granularity, such as making it easier to require SSL in certain subdomains or certain deployment environments. It could also be better if you are more comfortable doing this kind of configuration in Rails rather than in nginx. It will not give you any extra security, however.

nginx rewrite not working (with passenger on Mac OS X)

I have nginx with rewriting working correctly on my server in production.
But when I tried to set the same rule on my local development machine (mac) the rewrite doesn't seem to be working.
I want "universitytutor.local" to redirect to "www.universitytutor.local"
Here is the relevant part of my nginx.conf
server{
listen 80;
server_name universitytutor.local;
rewrite ^/(.*) http://www.universitytutor.local/$1 permanent;
}
server {
listen 80;
server_name www.universitytutor.local *.universitytutor.local;
root /Users/barmstrong/NetBeansProjects/universitytutor/public; # <--- be sure to point to 'public'!
passenger_enabled on;
rails_env development;
}
The page loads correctly whether I type "universitytutor.local" or "www.universitytutor.local" and it does not redirect.
I have the *.universitytutor.local in there because I use subdomains for different cities so I need this, but I want a blank subdomain to redirect to "www".
Any ideas?
Found the solution for this. I was not restarting Nginx correctly so it was not picking up the changes. Doh!
You can restart like this
sudo kill `cat /opt/nginx/logs/nginx.pid `
sudo /opt/nginx/sbin/nginx
or add this to your .bashrc for easier use
alias nginx_restart='nginx_stop; nginx_start'
alias nginx_start='sudo /opt/nginx/sbin/nginx'
alias nginx_stop='sudo kill `cat /opt/nginx/logs/nginx.pid `'

Resources