How to Install and Configure Nginx on CentOS 7?


Nginx is a popular web server that is known for its high performance, scalability, and low resource usage. It is commonly used as a reverse proxy, load balancer, and HTTP cache. In this article, we will discuss how to install and configure Nginx on CentOS 7.

Step 1: Update System

Before we start installing any packages, it's essential to update system to latest version. To do this, run following command −

sudo yum update

Step 2: Install Nginx

Once system is updated, we can proceed with installation of Nginx. We can install Nginx using following command −

sudo yum install nginx

The installation process will start, and you will be prompted to confirm installation by typing 'y' and pressing Enter.

Step 3: Start Nginx and Enable it to Start on Boot

After installation is complete, we can start Nginx service by running following command −

sudo systemctl start nginx

To enable Nginx to start automatically at boot time, run following command −

sudo systemctl enable nginx

Step 4: Configure Firewall to Allow Nginx Traffic

By default, CentOS 7 comes with a firewall enabled. We need to allow HTTP traffic through firewall to access web server. We can allow HTTP traffic using following command −

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

Step 5: Test Nginx Installation

Now that we have installed and configured Nginx, we can test it by visiting web server's IP address in a web browser. In a web browser, type following −

http://your_server_ip_address/

If everything is configured correctly, you should see default Nginx page.

Step 6: Configure Nginx

The default Nginx configuration file is located at /etc/nginx/nginx.conf. Before we make any changes to configuration file, we need to make a backup of it by running following command −

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

Now that we have made a backup of configuration file, we can edit original file using a text editor of our choice. In this example, we will use nano text editor −

sudo nano /etc/nginx/nginx.conf

The main Nginx configuration file is divided into several blocks, including http, server, and location blocks. http block is main configuration block and contains global settings for web server.

After making any changes to configuration file, you must test configuration file for syntax errors using following command −

sudo nginx -t

If there are no syntax errors, you can reload Nginx configuration using following command −

sudo systemctl reload nginx

Step 7: Configure Virtual Hosts

Nginx allows us to configure virtual hosts, which are used to serve different websites or applications on same server. To create a virtual host, we need to create a new server block in Nginx configuration file.

First, we need to create a new configuration file for our virtual host −

sudo nano /etc/nginx/conf.d/example.com.conf

In this file, we can add server block for our virtual host, which will contain configuration settings for our website. For example −

server {
   listen 80;
   server_name example.com;
   root /var/www/example.com;
   index index.html;

   location / {
      try_files $uri $uri/ /index.html;
   }
}

In this example, we are configuring a virtual host for a website with domain name example.com. root directive specifies document root directory for website, and index directive specifies default index file to serve.

The location block specifies configuration settings for requests that match a specific URL or URL pattern. In this case, we are using try_files directive to attempt to serve requested file or directory, and if it doesn't exist, serve index.html file.

After adding configuration for our virtual host, we need to test configuration file for syntax errors −

sudo nginx -t

If there are no syntax errors, we can reload Nginx configuration to apply changes −

sudo systemctl reload nginx

Step 8: Enable HTTPS with SSL/TLS Certificates

To secure our website with HTTPS, we need to obtain an SSL/TLS certificate from a trusted certificate authority (CA). We can obtain a free SSL/TLS certificate from Let's Encrypt using Certbot tool.

First, we need to install Certbot tool −

sudo yum install certbot python2-certbot-nginx

Next, we can run following command to obtain and install an SSL/TLS certificate for our virtual host −

sudo certbot --nginx -d example.com

This command will automatically obtain a certificate from Let's Encrypt and configure Nginx to use it for our virtual host.

After obtaining certificate, we need to configure Nginx to redirect all HTTP traffic to HTTPS. We can do this by adding following server block to our virtual host configuration −

server {
   listen 80;
   server_name example.com;
   return 301 https://$server_name$request_uri;
}

This server block will redirect all HTTP traffic to HTTPS using a 301 redirect.

Step 9: Enable Nginx to Proxy Requests to Application Servers

Nginx is often used as a reverse proxy server to distribute traffic across multiple application servers. This can improve performance and scalability of web applications.

To enable Nginx to proxy requests to application servers, we need to create a new server block in Nginx configuration file that defines upstream servers.

upstream app_servers {
   server 10.0.0.1:8000;
   server 10.0.0.2:8000;
}

In this example, we are defining an upstream group named app_servers that contains two application servers with IP addresses 10.0.0.1 and 10.0.0.2, listening on port 8000.

Next, we can add a new server block to our Nginx configuration file that proxies requests to upstream servers −

server {
   listen 80;
   server_name example.com;

   location / {
      proxy_pass http://app_servers;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout 60s;
      proxy_send_timeout 60s;
      proxy_read_timeout 60s;
   }
}

In this example, we are configuring Nginx to proxy requests to upstream servers defined in app_servers group. proxy_pass directive specifies URL to forward requests to, and proxy_set_header directives add additional headers to forwarded requests.

Step 10: Monitor Nginx with Systemd

Systemd is a system and service manager for Linux that provides a range of tools for managing services, including Nginx. We can use systemd to monitor Nginx service and view its status and logs.

To view status of Nginx service, run following command −

sudo systemctl status nginx

This command will show current status of Nginx service, including whether it is running or not, any errors or warnings, and time since it was last started or restarted.

To view logs for Nginx service, run following command −

sudo journalctl -u nginx

This command will show logs for Nginx service, including any errors, warnings, or informational messages.

We can also use following command to follow Nginx logs in real-time −

sudo journalctl -u nginx -f

This command will display Nginx logs in real-time as they are generated, allowing us to monitor service more closely.

Step 11: Configure Nginx as a Load Balancer

One of most common uses of Nginx is as a load balancer. In this configuration, Nginx distributes incoming requests across multiple backend servers, providing high availability and scalability for web applications.

To configure Nginx as a load balancer, we need to create an upstream group that contains IP addresses or hostnames of backend servers −

upstream backend_servers {
   server 10.0.0.1:8000;
   server 10.0.0.2:8000;
}

In this example, we are defining an upstream group named backend_servers that contains two backend servers with IP addresses 10.0.0.1 and 10.0.0.2, listening on port 8000.

Next, we can add a new server block to our Nginx configuration file that defines load balancing configuration −

server {
   listen 80;
   server_name example.com;

   location / {
      proxy_pass http://backend_servers;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout 60s;
      proxy_send_timeout 60s;
      proxy_read_timeout 60s;

      # Load balancing configuration
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Proto $scheme;

      proxy_redirect off;
      proxy_buffering off;
      proxy_http_version 1.1;

      # Load balancing algorithm
      # Options: round-robin, least_conn, ip_hash
      # Default: round-robin
      proxy_set_header X-Nginx-Upstream-LB-Method least_conn;
   }
}

In this example, we are using proxy_pass directive to forward requests to backend servers defined in backend_servers group. We are also adding additional headers to forwarded requests using proxy_set_header directives.

Conclusion

Nginx is an excellent web server that offers high performance, scalability, and low resource usage. In this article, we have discussed how to install and configure Nginx on CentOS 7. With steps outlined above, you should be able to set up a basic Nginx web server and start serving web pages.

Updated on: 12-May-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements