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

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 `'

Related

Issue with Nginx redirecting to Rails app

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.

Digitalocean - More than one application running on the same droplet?

Is it possible to run more than one Rails application on the same DigitalOcean droplet?
I recommend looking into using Dokku with Docker, which allows you to host applications along side each other. Digital Ocean has a One-click install available. I just started using it and deploying this way, and so far really like it.
Here are some links:
http://reallybusywizards.com/dokku-digitalocean-your-very-own-cheap-heroku-clone/
https://www.andrewmunsell.com/blog/dokku-tutorial-digital-ocean
https://www.digitalocean.com/community/tutorials/how-to-use-the-dokku-one-click-digitalocean-image-to-run-a-node-js-app
Yes you can do this, you just need to configure your application server, I have done this using nginx, is very quiet.
This tutorial is pretty cool to start with the server installer and rails application using Nginx server application:
Tutorial DigitalOcean
After doing this, open the configuration file for nginx:
sudo nano /opt/nginx/conf/nginx.conf
Now just add another block to configure a new application on another port, the default port is always 80. Enter note that port 8080 in this block.
server {
listen 8080;
server_name example.com;
passenger_enabled on;
root /var/www/my_new_rails_app/public;
}
Hope this helps!
YES
I am doing this currently. If your using Apache then in your httpd.conf file simply make two entries pointing to the public folders of two different application. Remember to identify different address for each.
I use phusion-passenger to make rails run with apache and my setup looks like this;
<VirtualHost ####################.com:80>
ServerName ####################.com
DocumentRoot /var/www/html/first_app/current/public/
<Directory /var/www/html/first_app/current/public>
Allow from all
Options -MultiViews
</Directory>
PassengerEnabled on
#RewriteEngine On
#RewriteCond %{HTTPS} on
#RewriteRule (.*) http://www.####################.com%{REQUEST_URI}
SetEnv GEM_HOME /usr/lib/ruby/gems/1.8
</VirtualHost>
<VirtualHost second_app.####################.com:80>
ServerName second_app.####################.com
DocumentRoot /var/www/html/second_app/current/public/
<Directory /var/www/html/second_app/current/public>
Allow from all
Options -MultiViews
</Directory>
PassengerEnabled on
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://www.####################.com%{REQUEST_URI}
SetEnv GEM_HOME /usr/lib/ruby/gems/1.8
</VirtualHost>
I had similar problem but #Leandro Figueredo answers didn't work for me.
Below i presenting what i do to achieve this.
Actually i have two website on one droplet.
I configured my server with this tutorial: GoRails How to setup server with Ubuntu 14.04 and nginx
After this configure file /etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name example.com www.example.com;
passenger_enabled on;
rails_env production;
root /home/user/appname/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;
}
}
server {
listen 80;
listen [::]:80;
server_name second_site.com www.second_site.com;
passenger_enabled on;
root /home/user/second_app/current/public;
}
Important:
remove default_server; from first server block

How to configure nginx with passenger with rails application

I have followed this tutorial https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu
I have installed passenger with nginx on my virtual machine and trying to access the site.
In the root I have specified path as root /var/rails_apps/public/; which give me Welcome to Nginx page,
server{
listen 80;
server_name localhost;
root /var/rails_apps/public/;
passenger_enabled on;
}
As my root page of my site is in the /var/rails_apps/app/views/home/index.html.erb
so I changed the path to root /var/rails_apps/app/views/home/;
server{
listen 80;
server_name localhost;
root /var/rails_apps/app/views/home/;
passenger_enabled on;
}
but still for both root I am getting Welcome to Nginx page.
My request URL is like this -> /#{IpAddressOfVirtualMachine}:80/
When I specified different port for listening eg 1027 then it gives me error Unable to connect
Please explain how I can get my site running using nginx and passenger, is there any other setting required?
I am able run my site just did following changes.
Install nginx init script
nginx init script by Jason Giedymin helps us to administer web server easily.
$ cd
$ git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
$ sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
$ sudo chown root:root /etc/init.d/nginx
After that rails g controller pages home
And point root to public folder root /var/rails_apps/helloworld/public;
then access my virtual machine through http #{IpAddressOfVirtualMachine}:1027/pages/home
port 80 was busy so I used different which is port 1027
And it works !!!
you can refer this blog for more information
http://ershadk.com/blog/2012/04/05/set-up-rails-3-2-2-with-passenger-rvm-and-ngnix/
Change the root back to public folder, and open the URL without the port number
You need to change the server name in the nginx config to the same IP you are connecting to, and yes keep the root in the public folder, that's how rails work.
server_name 123.456.789.000; # replace with your IP
Instead of localhost, then restart nginx.

NGINX Setup (Rails App in a subdirectory)

I'm using NGINX with Passenger to run a rails application on an Ubuntu server.
However, I'd like to have the rails app served from www.mydomain.com/store , and have
a wordpress install served from www.mydomain.com.
How would one go about setting up the nginx.conf?
From the official manual:
To do this, make a symlink from your Ruby on Rails application’s public folder to a directory in the document root. For example:
ln -s /webapps/mycook/public /websites/phusion/rails
Next, set passenger_enabled on and add a passenger_base_uri option to the server block:
server {
listen 80;
server_name www.phusion.nl;
root /websites/phusion;
passenger_enabled on; # <--- These lines have
passenger_base_uri /rails; # <--- been added.
}

Nginx and Passenger deploy issue

Currently I can only get the default nginx page to come up on my domain name. I am pretty sure the error is either in the /etc/hosts file or the enginx.config file.
my /etc/hosts file is
127.0.0.1 localhost.localdomain localhost
myip server.mydomain.com server
and nginx.config is:
server {
listen 80;
server_name server.mydomain.com;
root /whatever/pulic;
passenger_enabled on;
rails_env production;
I don't get any errors in the log. Incidentally I can run mongrel and on mydomain:3000 see the application there.
In your /etc/hosts, you have
123.45.67.78 server.domain.com servername
So in you nginx.conf you should have the line
server_name servername
as you defined in the third column of your /etc/host. Make sure you don't still have the default nginx server block in your nginx.conf file, too, otherwise it might be taking priority (based on it's relative position).
We just had this issue. The problem turned out to be that Nginx was using a different config file than we thought it was (possibly an issue with how it was compiled on the server?).
We discovered this by doing nginx -t, which lists the config file it's reading and tests the syntax. The one it said it was testing was not the one we expected.

Resources