How to serve index.html file using Nginx on CentOS

Nginx was first released in October 2004. Nginx is a free and open source web server that can be used for reverse proxy, caching, video streaming, email proxy, and load balancing. The developers aimed to create a web server that provides maximum performance and stability. It was originally conceived as a software solution to the C10k problem.

For the busiest websites on the Internet, Nginx optimizes content and application delivery, improves security, and ensures availability and scalability. It is also an ideal web server to serve your static website files or files generated by static website generators. In this post, we will set up a server and show you how to serve an index.html file using Nginx. Let’s start!

How to install Nginx on CentOS

If you don’t have Nginx installed, first install it on your system using the following procedure:

In the first step, open your CentOS terminal by pressing “CTRL + ALT + T” and then write the below command:

                      $ sudo yum install nginx

The error-free output states that Nginx has been successfully installed on your system.

How to enable Nginx on CentOS

Now use the below command to enable Nginx on CentOS system:

                      $ sudo systemctl enable nginx

After that, we start the Nginx service:

                      $ sudo systemctl start nginx

How to set firewall rules for Nginx on CentOS

The next thing we’re going to do is configure the firewall settings to allow external connections for Nginx, which runs on port 80 by default. Firewall-CMD is a command that is used to manage persistent and execute firewalld configuration.

To always enable HTTP connections on port 80, write the below command to your CentOS terminal:

                      $ sudo firewall-cmd --permanent -add-service=http

To check if the HTTP firewall service was added to the system correctly, run this command:

                      $ sudo firewall-cmd --permanent --list-all

Now restart the firewall service:

                      $ sudo firewall-cmd --reload


How to serve index.html file using Nginx

To follow the procedure for serving HTML files, we will create a “www” directory using the mkdir command. The “mkdir” command is used on Linux based systems such as CentOS to create one or more directories.

Run the below command in your terminal to create “www” in your current working directory:

                      $ sudo mkdir www

Then we create a sample index.html file in our www directory:

                      $ sudo nano ~/www/index.html

Add anything to index.html as per your requirement. However, we’ll add the following test content to our index.html file:

                      <!doctype html>



<title>Обслуживание index.html с Nginx</title>



<h1>Как обслуживать index.html с Nginx </h1>

<p>Nginx - это свободно доступный веб-сервер с открытым исходным кодом, который может быть обратным прокси-сервером, балансирующим нагрузку.</p>




Now press CTRL + O to save the content we added to the index.html file located in the www directory.

After that we will change the permissions on the “www” directory using the chmod command. Chmod, which stands for change mode, is the command that Linux users use to change file permissions.

Here, in the command below, we will try to assign, read, write and execute permissions to anyone who is going to use the “www” directory:

                      $ sudo chmod 0755 ~/www

Now open the nginx configuration file “/etc/nginx/nginx.conf” in the nano editor:

                      $ sudo nano /etc/nginx/nginx.conf

The /etc/nginx/nginx.conf file contains various blocks such as http, server, and location for Nginx configuration. Find the following line in the file:

                      include /etc/nginx/sites-enabled/*;

The above line states that the configuration file present in “site-available” is considered part of the Nginx configuration.

Now add the following server block to your Nginx config file:

                      server {

listen 80;


root /home/andreyex/www;

index index.html;


This server block specifies that for all connections Nginx will listen on port “80”, our server name is “”, the index file for serving with Nginx is “index.html”, and all files related to the server is located in the directory “/ home / andreyex / www”.

Press CTRL + O to save the changes we made to the open file.

Run the “nginx” command with the “-t” parameter to check the configuration file and its syntax:

                      $ sudo nginx -t

Now restart the Nginx service on your system:

                      $ sudo systemctl restart nginx

After restarting the Nginx service, go to your domain that you added in the “server_name” field. As a result, your web page index.html will be served.


Nginx was designed to provide excellent performance as a web server, especially when you need to handle many concurrent connections or static content. This is why it is optimized for serving static files. In this post, you saw the method for installing Nginx and how we serve index.html with Nginx on our system.