make simple configuration to two projects using Nginx (Part 4)
make simple configuration to two projects using Nginx (Part?4)
introduction:
in the previous articles about Nginx, we are talked about powerful of Nginx, how we can install Nginx in our machine, and we talked about the Nginx files system and explained important files and folders, but didn’t worked with the Nginx configuration until now.
in this article, we will make a simple project and test it, and we better understand Nginx files, and we will use some of the Nginx commands.
I think this article can provide you with a good understanding of the Nginx at general and Nginx Configuration and Files.
okay, let’s go to forward with this article.
all parts of Nginx Articles?:
nginx config component:
The http block contains directives for handling the web traffic, which are often referred to as universal, since, they are passed to all the website configurations that NGINX serves. The configuration file for handling different types of web traffic requests is passed using include directive, followed by configuration file path.
The server block contains server configuration directives, containing information such as server name, and TCP port. The listen directive tells NGINX hostname/IP and the TCP port where it should listen for HTTP connections. The server_name directive directs NGINX to select the server (out of multiple server blocks), when a request hits the server.
The location block enables you to handle several types of URIs/routes, within a server block. Typically, you’d make use of one or more regular expressions to define and handle a category of routes. The lookup then proceeds to comparing and matching the location blocks, and serves the one which is closest to the request URI hit.
configuration of two application
for example, we need to handle two different applications in our server by default Nginx contains one server block but in our case we need create other server block to handling requests for any application.
but the first step in this article we need create a simple HTML page for our applications.
let’s go to create a simple Html pages
in the first step we need to go to /var/www path by the following command?:
cd /var/www/
and create two files in var/www path
mkdir test.com/html example.com/html
inside our HTML page, we need to create file.html this page Nginx will provide to user when sending a request.
nano /var/www/test.com/html/index.html
<html>
<head>
<title>Welcome to test.com!</title>
</head>
<body>
<h1>Success! The test.com server block is working!</h1>
</body>
</html>
and create another HTML page by the same above way for example.com
you can use this way to copy html file in other path
cp /var/www/test.com/html /var/www/example.com/html/index.html
dont forget Modify it so that it refers to our second domain?
now we have an HTML page but Nginx doesn’t know where our HTML page and we don’t any configuration to handle requests, in this part we need to create server_block to make the correct configuration and handle all requests correct path.
let's go to create server_block in our configuration.
By default, Nginx contains one server block called default which we can use as a template for our own configurations. We will begin by designing our first domain’s server block, which we will then copy over for our second domain and make the necessary modifications.
currently, if we go to /etc/nginx/available-sites path, we can find one file called default, and we will copy all the content in this file to other files example.com and test.com.
?we can copy the content of the default file by the following command?:
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
dont forget repeat above command for test.com file
and open this file example.com file by the following command?:
nano /etc/nginx/sites-available/example.com
when executing the above command, you can see the following result we need to update this result with the correct config to example.com and we need to be linked example.com/html/index.html with this file.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
let's go to update and make the correct configuration for example.com.
default_server: this syntax means any request coming without server_name will redirect to this server.
we can define this command one time in Nginx
and now we need to change the HTML file to exmaple.com HTML file:
we can make this modify when apply the following syntax in example.com file.
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
}
and now we need to modify server_name to www.example.com we make this modify when making the following changes on server_name attribute:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
server_name example.com www.example.com;
}
领英推荐
now you can save and exit example.com file?
and we need to make the same changes in test.com file to linked with the domain name and HTML file by the same above way?.
but don’t forget change:
server_name test.com www.test.com
and change the HTML directory
after this we need enable server block we can execute follwoing command to enable it?:
in the last session, I explained sites-enabled files in this point we need to be linked /etc/nginx/sites-available with /etc/nginx/sites-enabled and the following command can link available file with enable file:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
this is a task of /sites-enabled
okay now we have three server_block is enable by Nginx:
but where is nginx.config file what the work of it?.
okay the next part of this article will go nginx.config and modify it to see new server.
we need open nginx.config we can use the following command to open file.
nano /etc/nginx/nginx.config
after executing the above command you can see the following file:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
client_max_body_size 1024M;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
....
}
note: the above file is not a default file of nginx but I updated itConclusion:
now we need to be linking /sites-enable files with /nginx.config
we must add the following attribute inside the above file to linked /sites-enabled files with nginx.config.
include /etc/nginx/sites-enabled/test.com;
include /etc/nginx/sites-enabled/example.com;
and we need remove # from nginx.config on the follwoing line
http {
. . .
server_names_hash_bucket_size 64;
. . .
}
testing nginx config for restart all services
we need to test all configurations to ensure we write config files without any error or not.
we can write the following command to test all config files in Nginx.
sudo nginx -t
if you don't have any problem you can resart Nginx service by the following command?.
sudo systemctl restart nginx
if you are using Nginx in the local machine you must modify the host’s file to test the result in the browser.
in the first step, we need to go to /etc/hosts path, you can execute the following command to go to this path.
nano /etc/hosts
and after executing the above command you can see the following result.
127.0.0.1 localhost
. . .
to linked local host with test.com or example?.com we can write follwoing command in /etc/hosts
your-ip-address example.com www.example.com
your-ip-address test.com www.test.com
your-ip-address eg:128.25.255.45
testing result in our?browser
now we need open the browser to test and see what is the final result from our configuration.
in the browser, we can visit www.test.com or www.example.com
and the first block provide me follwoing content?
and the second block provide me follwoing content
with above result, all of the requests is correct without any problem.
Conclusion:
in this article we saw how we can make our configuration? and create a simple app with provided HTML page to see in the browser with hostname?, and we can see how the Nginx is easy with configuration.
and we saw how we can map two applications in the same Nginx with different hosts name.
you can open the original Nginx website or digital ocean website to read deep about Nginx by original Nginx documentation
all parts of Nginx Articles?:
References: