How to Install and Configure Nginx on CentOS 8?

Nginx is a popular web server that is known for its performance, reliability, and ease of use. It is widely used for hosting websites, serving as a reverse proxy, load balancer, and caching server. In this article, we will discuss how to install and configure Nginx on CentOS 8.

Before we begin, make sure that you have root access to your CentOS 8 server. Additionally, you should have a basic understanding of Linux command line and be comfortable working with terminal-based applications.

Step 1: Update System

The first step is to ensure that your CentOS 8 system is up-to-date. You can do this by running following command −

sudo dnf update

This will download and install any available updates for your system.

Step 2: Install Nginx

Once your system is up-to-date, you can install Nginx by running following command −

sudo dnf install nginx

This will download and install Nginx and all its dependencies.

Step 3: Configure Firewall

By default, CentOS 8 comes with firewalld firewall pre-installed. You need to allow HTTP and HTTPS traffic to access webserver. Run following commands to open required ports −

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Step 4: Start and Enable Nginx

Once Nginx is installed, you can start it by running following command −

sudo systemctl start nginx

To ensure that Nginx starts automatically on system boot, run following command −

sudo systemctl enable nginx

You can check status of Nginx by running −

sudo systemctl status nginx

If everything is working correctly, you should see an output similar to following −

nginx.service - nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-04-24 15:37:24 UTC; 5s ago
   Process: 18625 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 18626 (nginx)
   Tasks: 2 (limit: 11524)
   Memory: 4.9M
   CGroup: /system.slice/nginx.service
           ├─18626 nginx: master process /usr/sbin/nginx
           └─18627 nginx: worker process

Step 5: Configure Nginx

Nginx's main configuration file is located at /etc/nginx/nginx.conf. You can open this file using your favorite text editor. Before making any changes to file, make a backup of original file by running following command −

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Now, open nginx.conf file −

sudo nano /etc/nginx/nginx.conf

Inside http block, you can add or modify server blocks to configure your virtual hosts. For example, if you want to create a new virtual host for, you can add following server block −

server {
   listen 80;
   root /var/www/;
   index index.html;
   location / {
      try_files $uri $uri/ =404;

This server block listens on port 80 for requests to and ` It sets document root to /var/www/example.comand specifiesindex.htmlas default index file. Thelocation` block specifies how to handle requests to URLs under this virtual host.

Once you have made changes to configuration file, save and exit text editor.

Step 6: Test Nginx Configuration

Before restarting Nginx, it's always a good idea to test configuration to ensure that there are no syntax errors. You can do this by running following command −

sudo nginx -t

If there are any errors, Nginx will display an error message and line number where error occurred. If there are no errors, Nginx will display a message indicating that configuration file is OK.

Step 7: Restart Nginx

After you have made any changes to configuration file, you need to restart Nginx for changes to take effect. You can do this by running following command −

sudo systemctl restart nginx

Step 8: Verify Nginx Installation

To verify that Nginx is serving requests correctly, you can open a web browser and enter your server's IP address or domain name in address bar. You should see default Nginx welcome page.

If you want to verify that your virtual hosts are working correctly, you can create a simple index.html file in document root directory for each virtual host and then access virtual host's domain name or IP address in your web browser. If everything is working correctly, you should see contents of index.html file.

One of most important features of Nginx is its ability to serve as a reverse proxy. This allows you to forward requests to multiple backend servers, distribute traffic evenly across multiple servers, and perform load balancing. You can also use Nginx as a caching server to improve performance of your web applications by caching frequently accessed content.

Another powerful feature of Nginx is its ability to handle SSL/TLS encryption. You can configure Nginx to serve HTTPS traffic by obtaining and installing an SSL/TLS certificate. This ensures that all traffic between your web server and your users is encrypted, providing an additional layer of security.

If you want to further optimize performance of your Nginx installation, you can enable gzip compression to reduce size of transmitted data, set up browser caching to reduce server load, and use gzip_static module to serve pre-compressed files for even faster delivery.

There are also many third-party modules and plugins available for Nginx that can extend its functionality. These include modules for web application firewalls, content filtering, and authentication.

In addition to its web server capabilities, Nginx can also be used as a streaming server, serving video and audio content over HTTP. This makes it an excellent choice for hosting live streaming events, delivering on-demand video content, and serving audio files.

Another important aspect of configuring Nginx is security. By default, Nginx is a secure web server, but there are still some additional measures you can take to improve its security. One of most important is to configure access controls to restrict access to your server and prevent unauthorized access.

You can configure access controls using Nginx's built-in authentication and authorization mechanisms, or you can use third-party modules like ModSecurity and Naxsi to provide additional security features like web application firewalls and content filtering.

Another important security measure is to keep your Nginx installation up-to-date with latest security patches and updates. You should regularly check for updates and install them as soon as they become available to ensure that your server is protected against latest security threats.

Finally, you should also consider using a Content Delivery Network (CDN) to further improve performance and security of your Nginx installation. A CDN can distribute your content to servers located around world, reducing load on your web server and improving speed and reliability of your website. Many CDN providers also offer additional security features like DDoS protection and web application firewalls.


In this article, we have discussed how to install and configure Nginx on CentOS 8. We have covered installation process, firewall configuration, starting and enabling Nginx, configuring Nginx, testing configuration, and verifying installation. By following these steps, you should be able to set up Nginx on your CentOS 8 server and serve your web applications with ease.

Updated on: 12-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started