Windows Server下基于NGINX实现多服务域名过滤与端口转发咨询
Hey there! Since you're completely new to Nginx, let's walk through this step by step—no confusing jargon, just straightforward actions to get your subdomains pointing to your two Web servers and two FTP servers correctly.
1. Install Nginx on Windows Server
- Head to the Nginx official site and grab the stable Windows zip package
- Extract it to an easy-to-find folder, like
C:\nginx - Test if it works: Open Command Prompt, navigate to
C:\nginx, runstart nginx.exe, then visithttp://localhostin your browser. If you see the Nginx welcome page, you're good to go!
2. Backup Your Original Config File First
- Locate
C:\nginx\conf\nginx.confand make a copy of it (name it something likenginx.conf.backup). This way, if you mess up the config later, you can easily revert.
3. Configure Subdomain Forwarding for Your Web Servers
Nginx uses server blocks to handle different domains. Let's assume:
- Your first Web server runs on
127.0.0.1:8080and should map toone.example.com - Your second Web server runs on
127.0.0.1:8081and should map totwo.example.com
Open nginx.conf, find the http block, and replace the default server block with this code:
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # Forward one.example.com to your first Web server server { listen 80; server_name one.example.com; location / { proxy_pass http://127.0.0.1:8080; # Pass through important request headers proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # Forward two.example.com to your second Web server server { listen 80; server_name two.example.com; location / { proxy_pass http://127.0.0.1:8081; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
4. Configure Forwarding for Your FTP Servers (Two Options)
FTP uses a different protocol than HTTP, so Nginx handles it a bit differently. Here are two simple options depending on how you want users to access your FTP servers:
Option 1: Forward Native FTP Traffic (For FTP Clients)
If users will connect via FTP clients (like FileZilla), you'll need to use Nginx's stream module to handle TCP traffic. Let's say:
- First FTP server runs on
127.0.0.1:2121(map toftp.one.example.com) - Second FTP server runs on
127.0.0.1:2122(map toftp.two.example.com)
Add this stream block outside the http block (at the same level) in nginx.conf:
stream { # Forward ftp.one.example.com to your first FTP server server { listen 21; server_name ftp.one.example.com; proxy_pass 127.0.0.1:2121; proxy_bind $server_addr; # Important for FTP passive mode } # Forward ftp.two.example.com to your second FTP server server { listen 21; server_name ftp.two.example.com; proxy_pass 127.0.0.1:2122; proxy_bind $server_addr; } }
⚠️ Heads up: FTP has passive mode which requires additional port ranges. You'll need to open those ports in Windows Firewall, and configure your FTP servers to use the same port range—otherwise, clients might fail to connect.
Option 2: Use Web-Based FTP (Easier for Newbies)
If your FTP servers have a web interface (WebFTP), you can forward just like you did with the Web servers. For example, if ftp.one.example.com should point to a WebFTP interface on 127.0.0.1:8082, add another server block inside the http block:
server { listen 80; server_name ftp.one.example.com; location / { proxy_pass http://127.0.0.1:8082; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
5. Test Your Config and Restart Nginx
- First, check for syntax errors: In Command Prompt, run
nginx.exe -t. If you seetest is successful, you're all set. - Restart Nginx to apply changes: Run
nginx.exe -s reload.
Final Checks
- Make sure your subdomains (
one.example.com,two.example.com, and any FTP subdomains) are pointed to your Windows Server's public IP address via your domain registrar's DNS settings. - Open port 80 (for Web traffic) and port 21 (if using native FTP) in Windows Firewall to allow external access.
If something doesn't work, check the Nginx error logs at C:\nginx\logs\error.log—it'll tell you exactly what's going wrong.
内容的提问来源于stack exchange,提问作者DevelopedLogic

