Setup Records For Domain/SubDomain in Domain Provider(NameCheap/GoDaddy/Any)
The first step in the NgInx configuration is to check whether domain/subdomain is pointed to server(Hosting) address.
To point any Domain(www.tutuorialflow.com) to the hosting server, we need to configure server address in Manage DNS.
- Login to your Domain Provider, select your domain and click on “Manage DNS” or “Advance DNS”.
- Add “A” record to point your Domain to the Hosting Address.
- Select “A” in the Type Field
- Enter “@” in the Host Field, and Hosting server IP address in the value textfield.,
- Enter any recommended value in TTL field, and click the confirm and save Settings.
Like above, create “A” record to point your subdomain to the Hosting Address, so that when you try the subdomain name in browser, request will be sent to the hosting server.
- Select “A” in the Type Field
- Enter subdomain name, “chatservice” in the Host Field, and Hosting server IP address in the value textfield.
- Enter any recommended value in TTL field, and click the confirm and save Settings.
Repeat the above process to add any number of subdomains required.
Once the above setup is done, the configured domains/subdomains- e.g., (https://tutorialflow.com and https://chatservice.tutorialflow.com) when checked in browser will be reaching out the server IP address configured.
Installing NgInx server in any linux server
For installing NgInx, use the following commands step by step.
1 2 |
$ sudo apt update $ sudo apt install nginx |
The above command will install the NgInx will simply install the NgInx in your server. Once installed you can check the version of NgInx using below command.
1 2 |
$ sudo nginx -v nginx version: nginx/1.6.3 |
The commands to start/stop/restart the ngInx are below.
Start ngInx Server
1 |
$ sudo systemctl start nginx |
Stop ngInx Server
1 |
$ sudo systemctl stop nginx |
Restart ngInx Server
1 |
$ sudo systemctl restart nginx |
Setup Directories for NgInx Domain/SubDomain in Hosting server
Once NgInx installed, the next setup is to modify the Configuration file and create our configuration.
In NgInx, the usual approach is to create configuration file for each domain, subdomain.
- Create separate .conf file for each domain/subdomain
- Place the configuration file in the configuration folder of ngInx
- Create a link to the configuration file
When NgInx started, all the conf files in the NgInx standard path will be included and NgInx will create routing for each configuration.
Configuration Path: /etc/nginx/sites-available/
For our example, we have to create 2 configuration files, one for (tutorialflow.com)domain & another for (chatservice.tutorialflow.com) subdomain.
- Create a configuration file for tutorialflow.com & chatservice.tutorialflow.com and save it in the configuration path.
- /etc/nginx/sites-available/tutorialflow.com.conf
- /etc/nginx/sites-available/chatservice.tutorialflow.com.conf
- Once the configuration is done, please create a link to this configuration from sites-enabled folder of NgInx as below.
- sudo ln -s /etc/nginx/sites-available/tutorialflow.com.conf /etc/nginx/sites-enabled/tutorialflow.com.conf
- sudo ln -s /etc/nginx/sites-available/chatservice.tutorialflow.com.conf /etc/nginx/sites-enabled/chatservice.tutorialflow.com.conf
Hence the when you list the configuration files in ngInx folder it will list down like below.
1 2 3 4 5 |
$ ls -ltr /etc/nginx/sites-enabled/ total 0 lrwxrwxrwx 1 root root 34 Sep 23 19:28 default -> /etc/nginx/sites-available/default lrwxrwxrwx 1 root root 44 Sep 23 19:40 tutorialflow.com.conf -> /etc/nginx/sites-available/tutorialflow.com.conf lrwxrwxrwx 1 root root 56 Nov 10 19:39 chatservice.tutorialflow.com.conf -> /etc/nginx/sites-available/chatservice.tutorialflow.com.conf |
Configure NgInx(Conf), for Domain/SubDomain
Now we have created conf files., next step is to configure the domains/subdomains and point to the local html page/application urls.
File: /etc/nginx/sites-available/tutorialflow.com.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
server { listen 80 default_server; listen [::]:80 default_server; server_name tutorialflow.com www.tutorialflow.com; location / { proxy_pass http://127.0.0.1:5000; proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; # try_files $uri $uri/ =404; } } server { listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/ssl/tutorialflow.com/ca_bundle.crt; ssl_certificate_key /etc/ssl/tutorialflow.com/private.key; server_name tutorialflow.com www.tutorialflow.com; location / { proxy_pass http://127.0.0.1:5000; proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; } } |
In the above server configuration, we have configured the server_name which shows the domain names, when invoked, automatically the URL(pages) present in the proxy_pass will be reached.
We can point directly to the html files, if we use configuration like below.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server { # Binds the TCP port 80. listen 80; # Directory when the domain server_name(tutorialflow.com) is called root /var/www/html/tutorialflow.com; # Index page used from above location index index.html index.htm; # SubDomain/Domain Name server_name tutorialflow.com; location / { // No proxy_pass is configured, since the static index.html will be called. try_files $uri $uri/ =404; } } |
For the Subdomain(https://chatservice.tutorialflow.com), we have to use the .conf similar like above, only difference we can change the server_name to subdomain, and proxy_pass we can point to another webapplication running on different port or different url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$ more /etc/nginx/sites-available/chatservice.tutorialflow.com.conf server { listen 80 default_server; listen [::]:80 default_server; server_name chatservice.tutorialflow.com www.chatservice.tutorialflow.com; location / { proxy_pass http://127.0.0.1:5100; proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; # try_files $uri $uri/ =404; } } server { listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/ssl/chatservice.tutorialflow.com/ca_bundle.crt; ssl_certificate_key /etc/ssl/chatservice.tutorialflow.com/private.key; server_name chatservice.tutorialflow.com www.chatservice.tutorialflow.com; location / { proxy_pass http://127.0.0.1:5100; proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; } } |
Once we done with above configuration files, for both domain and subdomains,
Restart the NgInx Server.
1 |
sudo systemctl restart nginx |
Now try to access the urls https://<domain>.com and https://chatservice.<domain>.com.
It should be showing the pages whatever we configured in the proxy_pass in the conf files.
We can always the check the status of NgInx using the below command., below which shows service is running fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ systemctl status nginx.service nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-11-11 11:36:00 UTC; 1 day 6h ago Docs: man:nginx(8) Process: 306 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 310 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 313 (nginx) Tasks: 2 (limit: 614) Memory: 16.9M CGroup: /system.slice/nginx.service ├─313 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─314 nginx: worker process Nov 11 11:36:00 tutorialflow.com systemd[1]: Starting A high performance web server and a reverse proxy server... Nov 11 11:36:00 <meta charset="utf-8">tutorialflow.com systemd[1]: Started A high performance web server and a reverse proxy server. |