Today is liu Xiaoai’s 142nd day of learning Java by herself.

Thank you for watching. Thank you.


Study plan arrangement, mainly to solve two problems:

  • One is to use SwitchHosts to solve the domain name problem.
  • Use of an nginx to resolve port issues.

SwitchHosts resolves the domain name

Be aware that there will be different environments in actual development.

  • Development environment: your own computer.
  • Test environment: The environment provided for testers to use.
  • Pre-release environment: the data is consistent with the build environment data, run the latest project code into the test.
  • Production environment: The environment in which the project is finally released online.

Some problems may occur if different environments use different IP addresses for access. So unify the environment.

1 Unified Environment

It is not possible to register a domain name to use, so we can forge the local hosts file.

First find the hosts file on your computer:


  • Under Windows hosts file address: C: / Windows/System32 / drivers/etc/hosts
  • In Linux, the hosts file is stored in /etc/hosts

Then change the local hosts to:

  • 127.0.0.1 api.liuxiaoai.com: Our gateway Zuul.
  • 127.0.0.1 manage.liuxiaoai.com: our background system address.

This solves the problem of accessing the domain name in the browser instead of using the IP.

A domain name is resolved to one or more IP addresses. The browser searches for the IP address mapped to the domain name in the hosts file.

The use of 3 switchhosts

If you need to find the host file every time, the path is cumbersome, so you can use SwitchHosts:


Note, again:

The real project will buy a domain name, configured, all through the domain name access.

However, there is no domain name purchase, so use SwitchHosts to fool yourself, in order to know there is such a thing.

Nginx resolves port problems

The domain name is settled, but now if we want to access it, we have to add port 9001 ourselves.

So the problem comes, for example, we go to visit Taobao, how does it not need to add a port number?

So what we want to do is just use the domain name to access manage.liuxiaoai.com.

If you do not enter a port, the browser defaults to 80. How do YOU divert requests to port 9001?

This is where the reverse proxy tool is used: Nginx


① Nginx as a Web server

Web servers fall into two categories:

  • Web application server, such as Tomcat.
  • Web server, such as Apache server, Nginx

They are distinguished by:

  • Nginx cannot parse pages such as JSPS. It can only handle static resources such as JS, CSS, and HTML.

  • However, the concurrency capability of Nginx is much higher than that of Web application servers such as Tomcat.

Therefore, a combination of Nginx and Tomcat was used in the project.

② Nginx serves as the reverse proxy server

What is a reverse proxy?

By using a server to proxy the real server, users are no longer accessing the real server but the proxy server.

Using reverse proxy, you can solve the port problem:


Note: If no port is specified for any path entered in the browser, the browser defaults to port 80.

Nginx reverse proxy rules:

Different requests are sent to different real servers for processing.

When a request arrives at Nginx, nGINx forwards the request according to defined rules, thus implementing reverse proxy.

③ Nginx installation and use

Nginx: nginx: nginx: nginx: nginx: nginx: nginx


Nginx can be clicked directly, but it will blink so that it is not started, when in fact it has been started in the background.

The downside is that multiple clicks can start multiple Nginx servers.

Therefore, the command line is used to start the system. The command is as follows:

  • Start nginx.exe
  • Exe -s stop: nginx.exe -s stop
  • Nginx. exe -s reload

Conf folder to complete the configuration:


Type manage.liuxiaoai.com in the browser. Since there is no port, the browser defaults to port 80.

Nginx is configured to listen on port 80, so it accesses the Nginx server.

The nginx server will be configured to reverse proxy to our local path +9001 port.

Third, to achieve process analysis

Now realize the domain name to visit the website, analyze the process again:


① The browser is ready to initiate a request to access the corresponding domain name, but the domain name needs to be resolved.

The request is sent to the resolved IP address 127.0.0.1, and port 80 is used by default.

③ Nginx listens on port 80, so it catches the request.

④ The reverse proxy rule is configured in nginx to enable manage.liuxiaoai.com to be proxy to local IP +9001 port.

⑤ The backend system port is 9001. The request is received and processed, and the response is returned to the Nginx server after completion.

⑥ Nginx returns the result to the browser.

The last

Thanks for watching.

If you can, please give it a thumbs up. Thank you.