1. What is Nginx?

Nginx is a high-performance HTTP and reverse proxy server, commonly used for building websites, load balancing, or acting as the “face” of backend services. For beginners, its core advantages are lightness, stability, and reverse proxy functionality, which allows you to easily forward client requests to backend services (e.g., Node.js, Python applications).

2. Installing Nginx

Installation commands vary slightly across Linux distributions. Here are examples for the most common Ubuntu/Debian and CentOS/RHEL systems.

2.1 Ubuntu/Debian Systems

Open the terminal and run:

# Update package list
sudo apt update  
# Install Nginx (-y confirms installation automatically)
sudo apt install nginx -y  

2.2 CentOS/RHEL Systems

# Install Nginx
sudo yum install nginx -y  

Verify Installation
After installation, check the Nginx version:

nginx -v  # Example output: "nginx version: nginx/1.18.0"

3. Starting and Basic Management

After installation, start Nginx and set it to auto-start to avoid service failure after server reboots.

3.1 Start Nginx

# Universal for Ubuntu/Debian/CentOS
sudo systemctl start nginx  

3.2 Set Auto-Start on Boot

sudo systemctl enable nginx  

3.3 Check Nginx Status

sudo systemctl status nginx  

If it shows “active (running)”, Nginx is running properly.

3.4 Common Management Commands

  • Stop Nginx: sudo systemctl stop nginx
  • Restart Nginx: sudo systemctl restart nginx
  • Reload Configuration (no service restart needed): sudo systemctl reload nginx (required after modifying configs)

4. Basic Configuration File Explanation

Nginx configuration consists of multiple files, with the main config file and site-specific config files being the core. Beginners only need to focus on site configs.

4.1 Main Configuration File

Location: /etc/nginx/nginx.conf
Purpose: Defines global settings (e.g., number of worker processes, log paths). Generally, no modification is needed.

4.2 Site Configuration Files

Location: /etc/nginx/conf.d/ (recommended) or /etc/nginx/sites-available/
Each site corresponds to a .conf file (e.g., default.conf or a custom name).
Example: Suppose your backend service runs on local port 3000 (e.g., a Node.js app), and you want Nginx to forward port 80 requests to 3000.

5. Detailed Reverse Proxy Configuration

The core of reverse proxy is “Client Request → Nginx → Backend Service”, where Nginx acts as a “middleman”. Here’s how to configure it:

5.1 Create a Site Configuration File

For example, create myapp.conf:

sudo nano /etc/nginx/conf.d/myapp.conf  

(nano is a text editor; vim works too. Recommended for beginners: press Ctrl+X to save and exit.)

5.2 Write Reverse Proxy Configuration

Paste the following into myapp.conf and save:

server {
    # Listen on port 80 (default HTTP port)
    listen 80;  
    # Domain or server IP (e.g., example.com or 192.168.1.100)
    server_name localhost;  

    # Match all client requests ("/" matches all paths)
    location / {
        # Backend service address (forward to local port 3000; ensure backend is running)
        proxy_pass http://127.0.0.1:3000;  
        # Pass client info to backend (so backend gets real IP/domain)
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
    }
}

5.3 Key Parameter Explanations

  • listen 80;: Port Nginx listens on (HTTP uses 80, HTTPS uses 443).
  • server_name: Domain or IP to access (use localhost or server IP if no domain).
  • proxy_pass: Backend service address (format: http://ip:port or https://ip:port; must start with http:///https://).
  • proxy_set_header: Pass HTTP headers. Host $host lets the backend know the requested domain; X-Real-IP $remote_addr records the real client IP.

6. Apply Configuration and Test

After configuration, verify syntax, reload, and test the effect.

6.1 Check Configuration Syntax

sudo nginx -t  

If it says “test is successful”, configs are error-free. If errors occur, fix the file as prompted.

6.2 Reload Configuration

sudo systemctl reload nginx  

(No service restart needed; new configs take effect immediately.)

6.3 Test Reverse Proxy

Assume the backend runs on port 3000 (e.g., curl http://127.0.0.1:3000 returns content):
- Access http://server-ip (or localhost) in a browser; you should see the backend’s response.
- Or test via terminal:

  curl -I http://localhost  # Check response headers

7. Notes

  • Firewall Setup: Ensure 80/443 ports are open in the server firewall.
  • Ubuntu: sudo ufw allow 80/tcp (enable ufw first: sudo ufw enable)
  • CentOS: sudo firewall-cmd --add-port=80/tcp --permanent && sudo firewall-cmd --reload
  • Backend Status: If the backend is not running, Nginx returns “502 Bad Gateway”. Check with netstat -tulnp | grep 3000.
  • Path Errors: proxy_pass must start with http:///https://; never omit the port (e.g., http://127.0.0.1:3000 not http://127.0.0.1).

8. Common Issues

  • 404 Error: Check if the backend is running or if proxy_pass is correctly configured (e.g., match the backend’s path).
  • HTTPS Reverse Proxy: If the backend uses HTTPS (e.g., https://127.0.0.1:443), set proxy_pass to https://127.0.0.1:443 and add proxy_ssl_server_name on;.

With the above steps, you’ve completed Nginx installation and reverse proxy configuration. Next, explore advanced features like HTTPS or load balancing—Nginx’s flexibility will help you master “server operation tips”!

Xiaoye