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

Nginx Configuration Guide: HTTP, HTTPS, Proxy Pass

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

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 `proxypass` 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

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 two cities are currently the country’s largest technology recruitment hubs, attracting outsourcing companies, product-based firms, fintech businesses, AI startups, and international tech corporations.

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 — a step that most new graduates skip entirely because they believe "freshers have no bargaining power." The reality is the opposite: nearly every company builds a negotiation buffer into their offers even for entry-level candidates, and not negotiating means you're leaving m

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 rejection can make you question your abilities, but in the vast majority of cases, interview failures have specific, identifiable, fixable causes that can be addressed in a matter of weeks if you know where to look.

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 developer's experience after rebuilding their CV using AI, discovering the harsh truth about how HR actually reads resumes in 2026.

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 scammers. Never before have invitations for "easy work with high pay," "earn millions from home," or "no experience required, A-Z training provided" appeared as frequently as they do today.

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 cơ sở dữ liệu slave. Điều này rất hữu ích vì nhiều lý do bao gồm việc tạo điều kiện cho sao lưu cho dữ liệu, một cách để phân tích nó mà không sử dụng các cơ sở dữ liệu chính, hoặc chỉ đơn giản là một phương tiện để mở rộng ra.

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 IT lecturer at universities, colleges, or training centers is a stable and long-term valuable career choice for Vietnamese developers in 2026.

Cách Tạo CV IT Cho Fresher Bằng LaTeX Và AI

Bạn vừa tốt nghiệp, cầm tấm bằng IT trên tay nhưng gửi hàng chục CV đi mà chỉ nhận lại sự im lặng? Tôi cũng từng như bạn, loay hoay với Microsoft Word, cố gắng căn chỉnh từng dòng kẻ để rồi khi xuất PDF, định dạng lại nhảy lung tung. Nỗi đau lớn nhất của dân kỹ thuật không phải là thiếu kỹ năng, mà là không biết cách "đóng gói" giá trị bản thân vào một bản CV chuyên nghiệp.

Programmer's Nightmare: Losing All Google Indexing in 1 Night Over a Single Chat Widget Code Line!

Did your website suddenly vanish from Google for no apparent reason? Discover this real-life case study of how a chat widget snippet blocked Googlebot and learn the ultimate fix. Read now!

TeamSoHoa.com: Discover the Fascinating World of Technology & Life

In the exploding digital era, accessing accurate, diverse, and useful information is more important than ever. Amidst the vast sea of information on the internet, where is the ideal stop for you to update the latest technology trends, read profound articles about life, and enjoy a smooth web browsing experience? The answer lies right at TeamSoHoa.com - a blog forum that is increasingly asserting


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