How to use Nginx reverse proxy virtual domain on Windows 10

Sometimes we need to access our web projects running on local environments by domain names. We can reverse proxy a virtual domain by Nginx. Then we can use configured domain names to visit our local projects.

Downloading Nginx for Windows

Downloading Nginx for Windows from Nginx.

Configuring hosts file of Windows

Configuring virtual domains in C:\Windows\System32\drivers\etc\hosts

127.0.0.1 example1.com
127.0.0.1 example2.com

Check if the virtual domains are working

Open CMD terminal on Windows, and run the virtual domain test command. e.g. ping example1.com

Pinging example1.com [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
...

Configuring Nginx Reverse Proxy

Configuring Nginx reverse proxy in conf\nginx.conf

http {
...
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://127.0.0.1:8081;
}
}

server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://127.0.0.1:8082;
}
}

}

Starting Nginx

Starting Nginx

cd D:\nginx-1.18.0
start nginx.exe

Check if your Nginx server is up and running

Visit http://localhost to see the “welcome to nginx!” page

Visit

Visit the virtual domain (server name) URL http://example1.com/{your_app_request_path}

Warning: Ensure you are not using an HTTP proxy client such as v2rayN before accessing virtual domain URLs.

Stop Nginx

CMD of Windows

cd D:\nginx-1.18.0

# fast shutdown
nginx -s stop

# graceful shutdown
nginx -s quit

Git bash

cd D:\nginx-1.18.0

# fast shutdown
./nginx.exe -s stop

# graceful shutdown
./nginx.exe -s quit

Reload the configuration of Nginx

CMD of Windows

cd D:\nginx-1.18.0
nginx -s reload

Git bash

cd D:\nginx-1.18.0
./nginx.exe -s reload

Note

  1. Ensure that the ports 80, 433… where Nginx is listening are not occupied.
  2. Ensure you are not using an HTTP proxy client such as v2rayN before accessing virtual domain URLs.
  3. You must use Nginx commands to start and stop Nginx. Else you can’t stop the Nginx. Unless the Nginx process is killed in the task manager-Windows details or the Windows system is restarted.
  4. Before running start nginx, you must check if an Nginx server is running. If an Nginx server is running, you must run nginx -s stop first. Repeatedly running start nginx will start multiple Nginx servers, and you can’t stop all Nginx servers. Unless the Nginx process is killed in the task manager-Windows details or the Windows system is restarted.