How to setup nginx server blocks on centos 7
Nginx is a free, open source, powerful, high-performance and scalable HTTP and reverse proxy server.It is great alternative to apache & has been gaining popularity in recent times. Though Nginx has been known to being a web server with less modules/add-ons & more complex settings than apache but if you are hosting a single website that you want to work at fast speed, than Nginx is right fit for you.
Nginx is now the preferred web server software for powering heavily loaded sites, due its scalability and performance.
Step 1: Install nginx
Since Nginx is available with default RHEL/CentOS repositories, we need to install Epel repository like below.
[root@lampblogs ~]# yum install epel-release
After epel repo is installed install nginx
[root@lampblogs ~]# yum install nginx
Now start the nginx service & also enable it after reboot
[root@lampblogs ~]# systemctl start nginx
[root@lampblogs ~]# systemctl enable nginx
[root@lampblogs ~]# systemctl status nginx
you can verify status with netstat command also
[root@lampblogs ~]# netstat -antp | grep nginx
We can also test the installation by opening web browser & entering IP address/FQDN of our server
Step 2: create directory structure
The default Nginx configuration file can be located at /etc/nginx/nginx.conf Also, the root of the default Nginx website points to /usr/share/nginx/html.we are going to create a root directory for each server block.
[root@lampblogs ~]# mkdir -p /var/www/example1.com/public_html
[root@lampblogs ~]# mkdir -p /var/www/example2.com/public_html
Once the directory structure is in place we can create a sample web page on the root of the example1.com directory
[root@lampblogs ~]# vi /var/www/example1.com/public_html/index.html
<html>
<body>
<p>welcome and example1.com is up and running!</p>
</body>
</html>
and create same as example2.com also
[root@lampblogs ~]# vi /var/www/example2.com/public_html/index.html
<html>
<body>
<p>welcome and example2.com is up and running!</p>
</body>
</html>
Step 3: create config files
The server block configuration files must be placed in the /etc/nginx/conf.d directory.Open your editor of choice and create a server block configuration file for example1.com
[root@lampblogs ~]# vi /etc/nginx/conf.d/example1.com.conf
and paste below configuration file
server {
listen 80;
server_name example1.com;
location / {
root /var/www/example1.com/public_html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
and create server block for example2.com also
[root@lampblogs ~]# vi /etc/nginx/conf.d/example2.com.conf
server {
listen 80;
server_name example2.com;
location / {
root /var/www/example2.com/public_html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Step 4: Testing nginx configuration syntax
[root@lampblogs ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 5: Restart nginx web server to effect configuration changes
[root@lampblogs ~]# systemctl restart nginx
Step 6: Testing in browser
If you didn't have functional domain address, open /etc/hosts file and edit it using the command below.Replace with your system ip address.
[root@lampblogs ~]# vi /etc/hosts
192.168.0.12 example1.com
192.168.0.12 example2.com
In case of windows edit the file C:\Windows\System32\drivers\etc\hosts
Finally to verify the server block is working as expected open example1.com in your browser of choice, and you will see like as below
and example2.com will see something like below.
Now you have successfully configured nginx virtual hosts.
A brief explanation of the directives used in nginx configuration:
- listen: specifies the port the server listens on.
- server_name: defines the server name which can be exact names, wildcard names, or regular expressions.
- root: specifies the directory out of which Nginx will serve web pages and other documents.
- index: specifies the type(s) of index file(s) to be served.
- location: used to process requests for specific files and folders.