Skip to content
KhaiziNam Blog KhaiziNam Blog
Go back
Đọc bằng tiếng Việt

Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass

Nguyễn Hữu Khải - khaizinam

Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass

Discover how to configure Nginx effectively for HTTP, HTTPS, Proxy Pass, and URL blocking. Optimize your website today!

In the modern web world, owning a powerful and flexible web server is key to ensuring performance and security for your website. Nginx, with its asynchronous architecture and high traffic handling capabilities, has become the top choice for many developers and system administrators. This article will dive deep into how to use Nginx, explaining important configuration rules such as HTTP, HTTPS, proxy pass, cache, and how to block unwanted URLs.

Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass
Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass

Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass

Understanding how to configure Nginx not only helps you optimize page load speeds but also enhances your website’s protection against threats. We will explore the core aspects of Nginx together, from basic concepts to advanced configuration techniques, helping you master this powerful tool.

If you are looking for solutions to improve website performance and security, mastering Nginx configuration is an indispensable step. Let’s begin this journey of discovery!

Table of Contents

What is Nginx?

Nginx (pronounced “engine-x”) is an open-source web server known for its high performance, scalability, and low system resource usage. It doesn’t just serve as a web server; it can also act as a load balancer, reverse proxy, and HTTP cache.

A standout feature of Nginx is its asynchronous, event-driven architecture. Instead of creating a new process for each connection like traditional web servers, Nginx uses an event-driven architecture to handle thousands of concurrent connections with a relatively small number of processes. This allows Nginx to consume less memory and CPU, which is especially effective when handling large volumes of traffic.

Benefits of using Nginx

Choosing Nginx for your web server brings many significant benefits, contributing to enhanced performance and user experience.

Key components in Nginx configuration

To understand how to use Nginx, you need to master the main configuration blocks:

Step-by-step Nginx configuration guide

We will go through the basic configuration steps for a website.

1. Installing Nginx

The installation process for Nginx varies depending on your operating system. On Debian/Ubuntu-based systems, you can run:

sudo apt update
sudo apt install nginx

On CentOS/RHEL, use:

sudo yum update
sudo yum install nginx

2. Basic HTTP Configuration

The main Nginx configuration file is usually located at /etc/nginx/nginx.conf. Configuration files for individual websites are typically placed in the /etc/nginx/sites-available/ directory and symbolically linked to /etc/nginx/sites-enabled/.

Create a new configuration file for your website, for example: /etc/nginx/sites-available/mywebsite.conf:

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
    root /var/www/mywebsite/html;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
}

Then, activate this configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/mywebsite.conf /etc/nginx/sites-enabled/

Check the configuration syntax and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

3. HTTPS Configuration

To enable HTTPS, you need an SSL/TLS certificate. You can get a free certificate from Let’s Encrypt. Suppose you have certificates at /etc/letsencrypt/live/your_domain.com/fullchain.pem and /etc/letsencrypt/live/your_domain.com/privkey.pem.

Edit the mywebsite.conf file:

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
    return 301 https://$host$request_uri; # Redirect HTTP to HTTPS
}
server {
    listen 443 ssl http2;
    server_name your_domain.com www.your_domain.com;
    ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;
    root /var/www/mywebsite/html;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
}

Restart Nginx after the changes.

4. Proxy Pass Configuration

proxy_pass is used to forward requests to another backend server. This is very useful when you have a web application running on a different port or server.

For example, to forward all requests to a Node.js application running on port 3000:

location / {
    proxy_pass http://localhost:3000;
    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_set_header X-Forwarded-Proto $scheme;
}

The important proxy_set_header directives help the backend server know information about the original request.

5. Cache Configuration

Nginx can cache responses from backend servers or static files to speed things up. You can use proxy_cache_path and proxy_cache.

Define the cache path in the http block:

http {
    # ... other configurations ...
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
    server {
        # ... server configuration ...
        location / {
            proxy_pass http://localhost:3000;
            proxy_set_header Host $host;
            # ... other headers ...
            proxy_cache my_cache; # Use the defined cache zone
            proxy_cache_valid 200 302 10m; # Cache time for 200, 302 codes
            proxy_cache_valid 404 1m; # Cache time for 404 codes
            add_header X-Cache-Status $upstream_cache_status; # Add header to check cache status
        }
    }
}

Remember to create the /var/cache/nginx directory and grant write permissions to the Nginx user.

6. Blocking URLs (Block URL)

You can block access to specific URLs or URL patterns using a location block with conditions.

For example, blocking access to a sensitive .env file:

location ~ /\.env {
    deny all;
    return 403;
}

Or blocking requests containing a certain string:

location ~* "bad_pattern" {
    deny all;
    return 403;
}

The ~ symbol indicates a regex match, ~* for case-insensitive regex match, and ! reverses the condition.

Common mistakes when configuring Nginx

During the process of working with Nginx, several common mistakes can cause issues:

Frequently Asked Questions about Nginx

Q: Can Nginx completely replace Apache?

A: Nginx is often used as a reverse proxy in front of Apache or other application servers. It excels at handling static connections and load balancing. In many cases, Nginx can handle all web requests, but combining Nginx and Apache remains a popular architecture.

Q: How do I view Nginx logs?

A: The access log and error log for Nginx are usually located in /var/log/nginx/. You can configure the paths and log formats in the nginx.conf file.

Q: What should worker_processes be set to?

A: The most common value is setting it equal to the number of CPU cores on the server to optimize performance.

Q: Can I use Nginx to serve static files and act as a proxy for an application at the same time?

A: Certainly. You can use different location blocks to specify how different types of requests are handled. One location can serve static files from root, while another uses proxy_pass.

Q: How can I configure Nginx to prevent basic DDoS attacks?

A: You can use directives like limit_req_zone and limit_conn_zone to limit the number of requests and connections from a single IP address. Effective cache configuration also helps reduce server load.

Mastering Nginx configuration is a crucial skill for anyone working with web servers. From setting up HTTP and HTTPS to using proxy_pass to connect with backend applications and leveraging cache for speed, Nginx provides a powerful toolkit. Knowing how to block unwanted URLs also contributes to protecting your website.

If you need SEO support or website optimization, contact khaizinam.io.vn for in-depth consultation.

Continue practicing and exploring more features of Nginx to optimize performance and security for your projects.


Share this post:

Related Posts

Will AI Replace Developers in Vietnam? What the 2026 IT Market Data Shows

Will AI replace developers in Vietnam? A data-backed look at Vietnam's 2026 IT talent market, AI adoption, hiring plans, and the skills foreign companies...

I Ditched Antigravity IDE and Went Back to VSCode + Codex

After Antigravity 2.0 silently tanked my IDE's performance to force an upgrade, I uninstalled it. Here's what actually happened - and why Gemini's...

Salary Negotiation for Fresh IT Graduates: Stop Leaving Money on the Table

Salary negotiation for IT freshers is the process of discussing and adjusting your compensation after receiving a job offer. Most new graduates skip this...

Does AI Help You Make Money or Burn It? Real Talk From Real Devs

AI is booming, and more devs than ever are shipping products. But is throwing ideas at AI enough to make money? Here's the honest answer - from people...

IT Salary Levels in Ho Chi Minh City and Hanoi (2025 - 2026 Overview)

The IT industry in Vietnam continues to maintain a higher salary range compared to many other industries, especially in Ho Chi Minh City and Hanoi. These...

I Failed My IT Job Interview - Here's What I Changed to Get Hired

Getting rejected from an IT job interview is something almost every fresher and junior developer goes through at least once - usually more. That first...

1-Page vs 3-Page IT CV: Hard Lessons Learned After Letting AI Build Your Resume

1-page IT CV or multi-page - this seemingly simple question determines whether HR will call you or quietly drop your application. This article shares a real...

Warning: Black MMO PayinApp Scams in 2026

In the digital age, as social media platforms like Facebook and Telegram become bustling marketplaces, potential prey are increasingly being targeted by...

Giới thiệu về MySQL Replication Master-Slave

MySQL Replication là một quá trình cho phép bạn dễ dàng duy trì nhiều bản sao của dữ liệu MySQL bằng cách cho họ sao chép tự động từ một master tạo ra một...

NVSP Teaching Certificate: A Smart Career Alternative for IT Graduates in the AI Era 2026

A complete guide to Vietnam's Nghiệp vụ Sư phạm (NVSP) teaching certificate - what it is, eligibility requirements, costs, and why the path to becoming an...


Previous Post
Programmer's Nightmare: Losing All Google Indexing in 1 Night Over a Single Chat Widget Code Line!
Next Post
Optimizing Project Environment Setup Time with Docker for Low-Config VPS