How to Configure Nginx Web Server in CentOS : cybexhosting.net

Hi there, if you’re searching for a lightweight, high-performance web server that also works as a reverse proxy, then Nginx is your answer. Nginx is an open-source web server that was created to handle high traffic websites with ease. It is known to be fast, efficient, and reliable. In this tutorial, we will show you how to configure Nginx Web Server in CentOS.

Prerequisites

Before we get started, there are a few requirements that you should meet:

Prerequisites Description
CentOS Server You should have a CentOS server available to use.
Non-root user with sudo privileges You should have a non-root user account set up on your server with sudo privileges.
Basic knowledge of Linux commands You should have basic knowledge of Linux commands in order to be able to follow this guide.
SSH client You will need an SSH client to remotely connect to your CentOS server.

Step 1 – Installing Nginx

The first step is to install Nginx on your CentOS server. Follow the steps below to do this:

    1. Update the CentOS package repository:

“`
sudo yum update -y
“`

    1. Install Nginx:

“`
sudo yum install nginx -y
“`

After executing the above commands, you should have Nginx installed on your CentOS server.

Step 2 – Configuring Firewall

In order for Nginx to work properly, you need to open the HTTP and HTTPS ports in your server’s firewall. Here are the commands to do this:

    1. Open port 80 for HTTP traffic:

“`
sudo firewall-cmd –permanent –add-service=http
“`

    1. Open port 443 for HTTPS traffic:

“`
sudo firewall-cmd –permanent –add-service=https
“`

    1. Reload the firewall to apply changes:

“`
sudo firewall-cmd –reload
“`

Now, your CentOS server should be ready to receive HTTP and HTTPS traffic.

Step 3 – Configuring Nginx

The default Nginx configuration file is located at `/etc/nginx/nginx.conf`. You will need to edit this file to configure Nginx.

    1. Open the configuration file using nano editor:

“`
sudo nano /etc/nginx/nginx.conf
“`

    1. Change the server_name directive to your server’s domain name or IP address:

“`
server {

listen 80;
server_name yourdomain.com;

# …
}
“`

    1. Save and close the configuration file:

Press `CTRL+X`, then `Y`, then hit `ENTER` to save and exit the file.

After saving the changes, you need to test the configuration file for any syntax errors:

“`
sudo nginx -t
“`

If there are no errors, restart the Nginx service:

“`
sudo systemctl restart nginx
“`

Your Nginx server should now be configured and ready to use.

Frequently Asked Questions (FAQs)

Q1. How can I check the Nginx version?

You can check the Nginx version by running the following command:

“`
nginx -v
“`

Q2. Where is the default root directory for Nginx?

The default root directory for Nginx is `/usr/share/nginx/html/`.

Q3. How can I create a virtual host in Nginx?

To create a virtual host in Nginx, you need to create a new configuration file in the `/etc/nginx/conf.d/` directory. Here is an example configuration file for a virtual host:

“`
server {

listen 80;
server_name example.com;

location / {
root /var/www/example.com;
index index.html index.htm;
}
}
“`

Save the file with a `.conf` extension, then test and reload Nginx.

Q4. How can I enable SSL in Nginx?

To enable SSL in Nginx, you need to obtain an SSL certificate from a trusted certificate authority (CA). Once you have the certificate, you can configure Nginx to use SSL by adding the following lines to your virtual host configuration file:

“`
server {

listen 443 ssl;
server_name example.com;

ssl_certificate /path/to/your/certificate;
ssl_certificate_key /path/to/your/private/key;

location / {
root /var/www/example.com;
index index.html index.htm;
}
}
“`

Save the file, test and reload Nginx.

Q5. How can I optimize Nginx for high traffic websites?

To optimize Nginx for high traffic websites, you can use techniques like caching, load balancing, and gzip compression. You can also tweak Nginx settings like worker processes, worker connections, and keepalive_timeout to improve performance.

Q6. How can I troubleshoot Nginx errors?

To troubleshoot Nginx errors, you can check the Nginx error log file located at `/var/log/nginx/error.log`. You can also use the `nginx -t` command to check the syntax of the configuration file for any errors.

Q7. How can I add basic authentication to Nginx?

To add basic authentication to Nginx, you need to create a password file using the `htpasswd` command. Here is an example configuration file for a location that requires authentication:

“`
location /protected {
auth_basic “Restricted”;
auth_basic_user_file /etc/nginx/.htpasswd;
# …
}
“`

Save the file, then create the password file using the `htpasswd` command:

“`
sudo htpasswd -c /etc/nginx/.htpasswd username
“`

Enter a password for the user when prompted.

Q8. How can I redirect HTTP traffic to HTTPS in Nginx?

To redirect HTTP traffic to HTTPS in Nginx, you need to add the following lines to your virtual host configuration file:

“`
server {

listen 80;
server_name example.com;

return 301 https://$server_name$request_uri;
}
“`

Save the file, test and reload Nginx.

Q9. How can I set up Nginx as a reverse proxy?

To set up Nginx as a reverse proxy, you need to create a new virtual host configuration file with the following lines:

“`
server {

listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# …
}
}
“`

Save the file, then configure the backend server in the `/etc/nginx/conf.d/` directory. Test and reload Nginx.

Q10. How can I limit the rate of requests in Nginx?

To limit the rate of requests in Nginx, you need to use the `limit_req` module. Here is an example configuration file for a location that limits the rate of requests:

“`
location /api {
limit_req zone=api burst=10 nodelay;
# …
}
“`

Save the file, test and reload Nginx.

Congratulations! You have now successfully configured Nginx Web Server in CentOS. Thanks for reading!

Source :