Contents
  1. 1. Downloading Nginx for Windows
  2. 2. Configuring hosts file of Windows
  3. 3. Configuring Nginx Reverse Proxy
  4. 4. Starting Nginx
    1. 4.1. Starting Nginx
    2. 4.2. Visit
    3. 4.3. Stop Nginx
    4. 4.4. Reload configuration of Nginx

Sometimes we need to access our web projects running on local environments by domain names. We can use Nginx reverse proxy virtual domain, 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

1
2
127.0.0.1 example1.com
127.0.0.1 example2.com

check virtual domains are worked

Open cmd, run the virtual domain test command. e.g. ping example1.com

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
cd D:\nginx-1.18.0
start nginx.exe

Check Nginx is started

Visit http://localhost, see welcome to nginx! page

Visit

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

1
2
3
4
5
6
7
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://127.0.0.1:8081;
}
}

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

Stop Nginx

CMD of Windows

1
2
3
4
5
6
7
cd D:\nginx-1.18.0

# fast shutdown
nginx -s stop

# graceful shutdown
nginx -s quit

Git bash

1
2
3
4
5
6
7
cd D:\nginx-1.18.0

# fast shutdown
./nginx.exe -s stop

# graceful shutdown
./nginx.exe -s quit

Reload configuration of Nginx

CMD of Windows

1
2
cd D:\nginx-1.18.0
nginx -s reload

Git bash

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

Note

  1. Make sure that the ports 80, 433… where Nginx is listening are not occupied.
  2. Make sure 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 to stop the Nginx. Unless to kill Nginx processes on Task Manager - Details of Windows, or to restart the Windows system.
  4. Before run start nginx, you need to check is there an Nginx server is running. If there is an Nginx server is running, you must run nginx -s stop firstly. Repeatedly running start nginx, it will start multiple Nginx server and you can’t stop all Nginx servers. Unless to kill Nginx processes on Task Manager - Details of Windows, or to restart the Windows system.
Contents
  1. 1. Downloading Nginx for Windows
  2. 2. Configuring hosts file of Windows
  3. 3. Configuring Nginx Reverse Proxy
  4. 4. Starting Nginx
    1. 4.1. Starting Nginx
    2. 4.2. Visit
    3. 4.3. Stop Nginx
    4. 4.4. Reload configuration of Nginx