Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Quick Setup Guide for Nginx on CentOS 7

Tech May 11 2

Installing and Starting Nginx

Install the Nginx package using yum:

sudo yum install nginx -y

Start the Nginx service:

sudo systemctl start nginx

Enable Nginx to start on system boot:

sudo systemctl enable nginx

Verifying Service Status

Check the current status of the Nginx service:

sudo systemctl status nginx

A successfully running Nginx instance will display active (running) status.

Firewall Configuration

If the firewall is enabled, open the necessary ports for web traffic:

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

Skip this step if the firewall is disabled.

Managing Nginx Service

Reloading Configuration

After modifying Nginx configuration files, reload the service to apply changes without interrupting active connections:

sudo systemctl reload nginx

Use nginx -t to validate the configuration syntax before reloading:

sudo nginx -t

Restarting Service

For significant changes or updates, perform a full restart:

sudo systemctl restart nginx

Nginx Configuration Guide

The main configuration file resides at /etc/nginx/nginx.conf, with additional configuration files loaded from /etc/nginx/conf.d. These files control Nginx's behavier, including server blocks that define how different sites or applications are handled.

Changing Default Listen Port

By default, Nginx listens on port 80 for HTTP traffic. To use a different port, modify the listen directive in the server block:

server {
    listen 8080;
    server_name mysite.example;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
}

Setting Up Reverse Proxy

Nginx is commonly used as a reverse proxy to forward requests to applications running internally:

server {
    listen 80;
    server_name mysite.example;

    location /api {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Enabling HTTPS

To enable HTTPS, configure SSL certificates in the server block:

server {
    listen 443 ssl;
    server_name mysite.example;

    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.