Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Nginx Server Configuration and Deployment Strategies

Tech 1

Nginx serves as a high-performance HTTP server, reverse proxy, and mail proxy (supporting IMAP/POP3 protocols). Originally developed by Igor Sysoev, it efficiently manages concurrent connections while maintaining low CPU and memory footprints, capable of handling tens of thousands of simultaneous requests.

Core Applications

HTTP Web Server Operates as a standalone HTTP service for serving static content including HTML, CSS, JavaScript files, and images.

Virtual Hosting Enables multiple websites to run on a single physical server through virtual host configurations, commonly used in shared hosting environments.

Reverse Proxy and Load Distribution Acts as an intermediary between clients and backend servers, distributing incoming traffic acros multiple application servers to prevent single points of failure and optimize resource utilization.

Prerequisites

Install the C compiler environment:

yum install -y gcc gcc-c++

Install PCRE (Perl Compatible Regular Expressions) for URI pattern matching:

yum install -y pcre pcre-devel

Install Zlib for gzip compression support:

yum install -y zlib zlib-devel

Install OpenSSL for HTTPS and SSL/TLS functionality:

yum install -y openssl openssl-devel

Installation Process

Download the source distribution from nginx.org. This example uses version 1.24.0:

wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

Generate the Makefile with custom configuration parameters:

./configure \
--prefix=/opt/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module

Compile and install:

make
make install

Create required temporary directories:

mkdir -p /var/cache/nginx/client_temp

Service Management

Start the Nginx daemon:

nginx

Or using the full path:

/usr/sbin/nginx

Verify process status:

ps -ef | grep nginx

Graceful shutdown:

nginx -s quit

Immediate stop:

nginx -s stop

Reload configuration without dropping connections:

nginx -s reload

For systems running firewalld, open port 80:

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

Test local connectivity:

curl http://localhost

Static Content Hosting

Deploy static assets by placing files in the document root directory (default: /opt/nginx/html or as configured). Modify server blocks in the configuraton to point to custom content directories:

server {
    listen 80;
    server_name localhost;
    
    location / {
        root /var/www/html;
        index index.html index.htm;
    }
    
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/error;
    }
}

Virtual Host Configurations

Port-Based Hosting Distinguish multiple sites using different port numbers:

server {
    listen 8080;
    server_name localhost;
    
    location / {
        root /var/www/site-alpha;
        index index.html;
    }
}

server {
    listen 8081;
    server_name localhost;
    
    location / {
        root /var/www/site-beta;
        index start.html;
    }
}

Access via http://ip-address:8080 and http://ip-address:8081.

Name-Based Hosting Map domains to specific document roots by configuring DNS or local hosts file entries, then updating server_name directives:

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

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

Reverse Proxy Implementation

Configure Nginx to forward client requests to backend application servers:

upstream backend_cluster {
    server 10.0.1.50:8080;
}

server {
    listen 80;
    server_name app.example.com;
    
    location / {
        proxy_pass http://backend_cluster;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        index index.html;
    }
}

Load Balancing Configuration

Distribute traffic across multiple backend instances:

upstream app_servers {
    server 10.0.1.50:8080 weight=3;
    server 10.0.1.51:8080 weight=2;
    server 10.0.1.52:8080 backup;
}

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://app_servers;
    }
}

Weight parameters control traffic distribution ratios, while the backup designation activates only when primary nodes fail.

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.