0

Xử lý bài toán "Issues running IPsec VPNs on Linux"

Cách cấu hình Nginx làm Proxy trên Windows để forward request

Bài viết này hướng dẫn cách thiết lập Nginx trên Windows để làm Reverse Proxy, giúp forward request từ client tới một backend service (đặc biệt là các backend chạy HTTPS như Azure hoặc IIS).

Guide Steps (Dành cho Windows)

Bước 1: Tải và giải nén Nginx

  1. Truy cập trang chủ nginx.org và tải bản Mainline version dành cho Windows (ví dụ: nginx/Windows-1.25.x).
  2. Giải nén vào một thư mục cố định, ví dụ: C:\nginx.

Bước 2: Cài đặt Nginx làm Windows Service (Khuyên dùng)

Mặc định trên Windows, Nginx sẽ tắt khi bạn logout. Để Nginx chạy ngầm liên tục, hãy dùng công cụ NSSM (Non-Sucking Service Manager):

  1. Tải NSSM tại nssm.cc.
  2. Mở Command Prompt (Admin) và gõ: nssm install nginx.
  3. Trong bảng hiện ra, chọn đường dẫn đến file C:\nginx\nginx.exe.
  4. Nhấn Install service. Sau đó, bạn có thể quản lý qua lệnh net start nginx hoặc services.msc.

Bước 3: Cấu hình SSL Forwarding & SNI

Khi backend là HTTPS, Nginx trên Windows cần được cấu hình SNI (Server Name Indication) để khớp với chứng chỉ SSL của server đích. Nếu thiếu bước này, bạn sẽ gặp lỗi 400 Bad Request - Invalid Header.

Bước 4: Kiểm tra và Reload cấu hình

Mỗi khi sửa file conf/nginx.conf, bạn cần kiểm tra lỗi cú pháp:

  • Kiểm tra: nginx -t (chạy trong thư mục C:\nginx).
  • Áp dụng thay đổi: nginx -s reload.

Config nginx.conf với sample values

Mở file C:\nginx\conf\nginx.conf và cập nhật nội dung tương tự bên dưới:

# GLOBAL SETTINGS
worker_processes  1; # Standard for Windows

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    # PERFORMANCE & TIMEOUTS
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    # REVERSE PROXY CONFIGURATION
    server {
        listen       8888;
        server_name  localhost;

        location / {
            # Define backend
            set $backend "https://example.com";
            resolver 8.8.8.8 valid=30s; 
            
            proxy_pass $backend;

            # 1. FIX: Only use ONE Host header. 
            # The backend MUST receive the domain name it expects.
            proxy_set_header Host example.com;

            # 2. ESSENTIAL for SNI (since the backend is HTTPS)
            proxy_ssl_server_name on;
            proxy_ssl_name example.com;

            # 3. Standard Proxy Headers
            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;

            # 4. Buffers (Keep these as you had them for large headers)
            proxy_buffer_size 128k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;

            proxy_redirect off;
        }
    }

    # HTTPS EXAMPLE (Uncomment and update paths to enable SSL)
    #
    # server {
    #    listen       443 ssl;
    #    server_name  example.com;
    #
    #    ssl_certificate      C:/nginx/ssl/cert.crt;
    #    ssl_certificate_key  C:/nginx/ssl/cert.key;
    #
    #    location / {
    #        proxy_pass http://127.0.0.1:3000;
    #        proxy_set_header Host $host;
    #    }
    # }
}

Các lưu ý kỹ thuật (Technical Notes) cho Windows:

  • Path: Luôn sử dụng dấu gạch chéo xuôi / trong file cấu hình Nginx thay vì gạch chéo ngược \ của Windows (ví dụ: C:/nginx/logs/error.log).
  • Logs: Nếu Nginx không khởi động được, hãy kiểm tra file C:\nginx\logs\error.log để biết chi tiết lỗi.

Cấu hình cho FortiClientVPN


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí