We should run this step after deployment of our app and it is better to do it then because we will be configuring nginx to forward requests to puma server (our application server) which we have not deployed yet so if you try to run your site right after configuring nginx on server, you are likely to get an error. Apart from that, once you have done this, nginx will be ready to serve your Rails app once it is deployed. {: .notice—warning}
Server Blocks
If you are coming from the world of Apache’s HTTP Server (often called as httpd), then server blocks are nearly the same thing as a VirtualHost in apache. However, if you are new to hosting your apps, just ignore the Apache jargon. Server Blocks are a way of hosting multiple sites on a single machine with nginx. They collectively define how requests for a particular website should be handled.
Proxy Pass
We will be defining our app as a new site and sending all incoming requests from the web to the puma server. This is called proxy_pass in nginx terminology.
Configuration file
Typically, the main nginx configuration file from where it reads all its configuration is /etc/nginx/nginx.conf
. You can add a server block in that file. However, if you need to change a particular site’s configuration, you would have to edit the main confiuration file. This is not a good idea because if something goes wrong with the syntax then all sites (or server blocks) would go down. To avoid this, nginx supports keeping various configuration files separate and reading them when the server restarts (it also allows a system administrator to allow certain users to alter the configuration for their sites while denying them the ability to change configuration of other sites).
If you have not changed your nginx config (which means that your main configuration file is still untouched), then opening that file would show you a line in the http
definition which reads like this:
include /etc/nginx/sites-enabled/*;
This line tells nginx to load all configuration files in /etc/nginx/sites-enabled
directory and load them. Now, if we need to disable the site, then we will have to remove the file, or at least move it some place else. A better way of doing that is to first create the configuration file in /etc/nginx/sites-available
and then create a symbolic link to the config file in /etc/nginx/sites-enabled
directory.
Contents of the config file
Launch the nano text editor using this command:
And paste these contents to the file:
upstream mysite_backend {
server 127.0.0.1:3000;
}
server {
server_name mysite.example.com;
listen 80;
try_files $uri $uri/ @mysite_backend;
location @mysite_backend {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://mysite_backend;
}
root /home/ubuntu/mysite/current/public;
}
TIP: Remember to change the server_name
from mysite.example.com;
to whatever domain (or subdomain) you are hosting your site on.
{: .notice—info}
Press Control + X (^X
) to save the file (press y
and then the enter key when nano asks for the prompts).
Link the config file in the sites-enabled directory
Type this command to go to the concerned directory.
Then type the following command to create a link in the directory
Check the configuration
Run sudo nginx -t
to test the configuration files syntax. If you find an error, fix it and save the file.
Restart the server to enable the site
Restart nginx service with
Then visit the URL which you configured your server_name
as. If you had deployed the Rails app already, you should see your index page of the app. If you hadn’t you should see a gateway error message.