Nginx - Basic Configuration - ruby-on-rails

I've just deployed a Rails application using Capistrano under /home/username/app_name.
Now, I'm not quite sure on how to setup Nginx, I've followed this tutorial, http://coding.smashingmagazine.com/2011/06/28/setup-a-ubuntu-vps-for-hosting-ruby-on-rails-applications-2/comment-page-1/#comment-594321. I have this on my nginx.conf file, I modified the first server block I saw:
server {
listen 80;
server_name www.yourdomain.com;
root /home/johndoe/test_app/current/public;
passenger_enabled on;
...
}
But now, I'm not sure how to procede. What exactly is the server_name in the example above? And how should I access the application? I've tried typing in my ip address and nothing shows up. I'm using Linode by the way. And how do I set up the nameservers for my domain? Detailed explanations and tutorials would be very helpful. Thanks in advance!

server_name on Nginx is exactly the same as ServerName in Apache, i.e. the domain name you'd like to use for this directory (named virtual host).
With passenger set to on you should be able to access the application directly via the domain.

Related

Nginx Passenger not serving Rails application

I setup Nginx with passenger on centos 7 VPS. I installed nginx and passenger as a gem. In addition I installed passenger-install-nginx-module.
When I start sudo service nginx and type passenger-status I get "Phusion Passenger is currently not serving any applications.". From my nginx conf file
http {
passenger_root /usr/local/rvm/gems/ruby-2.4.1/gems/passenger-5.1.8;
passenger_ruby /usr/local/rvm/gems/ruby-2.4.1/wrappers/ruby;
passenger_app_env production;
passenger_instance_registry_dir /var/lib/passenger-instreg;
...
server {
listen 80;
server_name localhost;
root /home/myuserhere/current/public;
passenger_enabled on;
File passenger.* creates normally when I restart nginx.
echo $PASSENGER_INSTANCE_REGISTRY_DIR returns the same path as in nginx conf file.
What I do wrong that passegner does not start application?
Finally I got it. Everything was all right. Passenger does not start apps during startup, but during the first request so all I needed to do it was press enter in my browser ... Hope that answer save time to others.
I was having a similar problem; where passenger wasn't serving any applications. This is on Focal, rails 6, Ruby 3.0.1, passenger-version: 6.0.8.
Turns out that it will start serving requests upon first request. So all I did was to temporarily allow http on the UFW then used 'curl' to http a request. Once the landing page was displayed, I was good to go.

Add GoDaddy naked domain to Heroku app

Heroku custom domains
I've setup two custom domains for my Heroku app.
example.com example.com.herokudns.com
*.example.com wildcard.example.com.herokudns.com
Domain configuration
I configured my domain as follows:
I added a CNAME Record for * pointing to wildcard.example.com.herokudns.com.
Works fine.
I forwarded my URL using GoDaddy's Domain Forwarding tool, because I can only specify IP addresses as A records.
Problem
The domain forwarding points to example.com.herokudns.com. Unfortunately GoDaddy automatically prepends http://, so it actually does not open my app and instead shows a Heroku message:
There's nothing here, yet.
Goal
Setting up my GoDaddy root domain to point to my Heroku app.
Note: GoDaddy automatically added an A record for # pointing to >>++FWD1++<<
Cloudflare does the job!
Finally, I achieved my goal of using my naked domain as host by choosing CloudFlare to handle my DNS configuration.
Resources:
CloudFlare allows CNAME Records to be the naked domain
How CNAME Flattening works
Note: CloudFlare has a pretty good documentation and setup process, you just need to:
add your domain to CloudFlare
follow the CloudFlare setup guide
updating your nameservers (in my case GoDaddy) to point to CloudFlare
What did you set your DNS to forward to? I had this same problem, but solved this creating a Heroku DNS entry for www.myapp.com. Heroku creates a DNS target of www.myapp.com.herokudns.com.
Here is my setup:
DNS forwarding to www.myapp.com
DNS CName of www to www.myapp.com.herokudns.com
Heroku DNS added www.myapp.com
You can also achieve this by setting up a nginx server with docker in five minutes.
First follow the instruction on this link: https://pentacent.medium.com/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71
Then add your nginx server ip to your A Record with host name #
In you nginx server you can use this code to redirect all traffic to your naked domain to www domain. Just replace all your-domain.com with your domain.
server {
listen 80;
server_name your-domain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://www.your-domain.com;
}
}
server {
listen 443 ssl;
server_name your-domain.com;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
location / {
return 301 https://www.your-domain.com$request_uri;
}
}
In Setup Heroku and GoDaddy? allegutta solves the issue by masking the heroku-app-name domain with the .com domain. Instead of slooob.com.herokudns.com, use your original heroku app url ([heroku-app].herokuapp.com) and it should work. Just worked for me.

Same server with different Website names

I have given two different different websites names with same IP(192.168.1.142)
Now i am using both these for configuring the reverse proxy using nginx.
Is it ok whether i can run ?
Kindly suggest any problems if any that i might face in future.
Yes this is fine. Use different server {} blocks and the server_name option to specify which configuration is which:
server {
listen 80;
server_name domain.com;
# rest of domain.com options go here
}
server {
# this will be the default site on this host
listen 80 default_server;
server_name other.com;
# rest of other.com options go here
}
In practice, splitting the two server {} blocks into different files would make maintenance easier and is often the norm.
If you only want the sites to be available on that one IP, change the listen directive to:
listen 192.168.1.142:80;
Also if you want to use SSL/HTTPS then you may run into complications as you can only have one SSL certificate per IP address. There are solutions if this is the case.

Phusion Passenger + Nginx Virtual Host Configuration for Ruby on Rails Application in Debian 6

What my question is how do i map all the instances with port 80 with appropriate identical sub domains using Phusion Passenger + Nginx under Debian 6 for the following scenario.
Note : i am newbie to ruby on rails and server hosting , i unable to express my vision as question , please forgive me if i am wrong or correct me.
Hi , i am having an one product developed using ruby on rails , going to host in an dedicated server with 32 GB Ram and 8 Core Server Processor. Each client should have seperate DB and separate rails instance. I am replicating - duplicating code for each client with separate folders and giving different database names and so each serving as an different instance. Basically Source code is same for all client but only database name differs, so i am putting source code into different folders and staring as an separate rails application for each client.
For Example:
I am having one primary temp domain
www.product.com
For each client i need to have separate sub domain mapped to same server with same port(80)
but with different rails instance name
www.client1.product.com
www.client2.product.com
www.clientn.product.com
As i know if i start rails instance , each will start at seperate port no's
client1 with start at port 3001
client2 with start at port 3002
client3 with start at port 3003
What my question is how do i map all the instances with port 80 with appropriate identical sub domains using Phusion Passenger + Nginx under Debian 6
if i access
www.client4.product.com , i need to get app running in port 3004
I hope you found the solution by now, but for the sake of stackoverflow archive I'm going to provide an answer.
Generaly meaning you should use reverse proxy (http://wiki.nginx.org/RubyonRailsMongrel) with nginx, while your upstream will be a passenger|thin|unicorn|puma|mongrel (pick your favorite) server runnning your dedicated rails application (I will use duplicated code in order to isolate your client running environment.) You'll be able to provide load balancing with this method.
Now, you stipulate the use of Passenger, you could use a really simple setup if you compile nginx with passenger extention. (go to http://www.modrails.com/documentation/Users%20guide%20Nginx.html for better explanation)
server {
listen 80;
listen [::]:80 default_server ipv6only=on; # please for a better world be IPv6 compliant !
server_name www.client1.product.com;
root /my/rails/app1;
passenger_enabled on;
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name www.client2.product.com;
root /my/rails/app2;
passenger_enabled on;
}
.......

Hosting Multiple websites on nginx similar to apache

I am trying to host multiple websites in nginx similar to apache. In apache we have folder called htdocs. We put directories and files inside htdocs and if the URL is something like
www.example.com
then we can access the directories and files inside the htdocs folder as
www.example.com/exampledir1 and www.example.com/exampledir2
I want to do the same with nginx. Is it possible. I have a server in my company which has a static IP. Ive installed nginx on it and by default i have hosted one rails app on it with the following lines in the nginx.conf file
server {
listen 80;
server_name mycomp.does-it.net;
root /var/www/mysamplerails/public;
passenger_enabled on;
rails_env development;
}
This site is accessible to me if i go to the url "mycomp.does-it.net"
now what i would like to do is have another rails app and access it from mycomp.does-it.net/mysecondrailsapp as the root.
Ive googled a lot and im ending up no where. I'm from apache background and these are some of my first tries in nginx.
Use passenger_base_uri option. See documentation: Deploying to a sub URI

Resources