Today, take the time to record the nginx reverse proxy knowledge, is also a useful before, but I think it’s still simply record, or you will forget good, maybe after you learn the new knowledge, and to forget it, go back to study, and find baidu this blog, that find a blog, learn how cost!

1. Concepts of forward and reverse proxies

Forward or reverse proxies are, after all, derivative versions of the proxy model. We have all studied the proxy design pattern, and we know that the proxy pattern has both the proxy role and the proxy role. Why? Because these two roles are very important to our understanding of forward and reverse proxies, as discussed below.

Below I will introduce such a scenario, most of the time we get to the Internet speed is particularly slow, or due to over the wall problem we cannot access to foreign website, usually these conditions we will configure your browser to a speed fast, can over the wall of the proxy IP and port number to solve our problems, so the configuration, about the request of the process as shown in the figure below:

We first request the proxy server, and then the proxy server helps us to quickly access foreign websites. For this proxy method, we call it forward proxy. Remember, of the two roles mentioned in proxy mode, our current role is that of the proxied, the browser. More importantly, the essence of positive agency is that we request external resources. If we are differentiated by producer and consumer models, we belong to consumers.

Conclusion:

  • 1. Positive proxy. Our role is to be represented
  • 2, positive agency, we do not provide external services, but external consumption services, belonging to consumers

Reverse proxy, obviously, is the opposite of the forward proxy, if the forward proxy is a male, then the reverse proxy is a female, pro, no longer entangled in other cases here! Here is a picture to illustrate the reverse proxy:

After looking at the picture above, please imagine this scenario. Suppose you are the technical director of a company and your company needs to provide a set of Web services to the outside world. What are you going to do?

The answer is that it can be done by reverse proxy. Usually your company has its own IDC room, and the communication in the room is usually through LAN switch. Internet users cannot directly access the Web services in the LAN. Therefore, at this time, you need a reverse proxy server to receive Internet Web requests. The request is then distributed to different hosts on the LAN for processing, and a response is made when the processing is complete. So a reverse proxy is probably one scenario. Remember, in the reverse proxy, our role is a LAN Web service.

Conclusion:

  • 1. Reverse proxy, our role is LAN Web service
  • 2. Reverse proxy: We provide services externally and belong to the service provider

Nginx forward proxy and reverse proxy instance resolution

Nginx in the forward proxy application is very little, therefore, for the forward proxy configuration instructions are not much, the following is a nginx as a forward proxy server configuration example, configuration for reference only.

Server {resolver 192.168.1.1;# Specify the DNS server IP address
    listen 8080;  
    location / {  
        proxy_pass http://$http_host$request_uri; Set the protocol and address of the proxy server}}Copy the code

Resolver Specifies the IP address of the DNS server. You can configure multiple IP addresses for the DNS server. You may ask, why do YOU need to configure the IP address of the DNS server in forward proxy? The answer is simple, you imagine if now your browser configuration is a proxy server, you now enter http://oneSite.cn/index.html in your browser, according to the principle of forward agent, the request url will be forward proxy server, question, If your proxy server is not configured with DNS resolution, how does Nginx know what onesite.cn is and what IP address it corresponds to on the Internet? This is why you need to configure the Resolver directive.

The Listen directive configures the port number on which Nginx listens for browser requests.

$http_host$request_uri specifies the destination host and URI. It is an nginx variable and does not need to be modified.

The configuration of nginx reverse proxy is as follows. Here we build two small Spring Boot demos to simulate the reverse proxy Web service. The relevant source code can be obtained on Github.

The demo project starts from port 8081, and the Demo1 project starts from port 8082. All requests prefixed with/Demo will be forwarded to the Demo project for processing, and all requests prefixed with/Demo1 will be forwarded to the Demo1 project for processing.

Nginx configuration is as follows:

server { listen 80; Location /demo {proxy_pass http://127.0.0.1:8081; } location /demo1 {proxy_pass http://127.0.0.1:8082; }}Copy the code

After starting the demo and Demo1 projects, type the following address in your browser:

As you can see, when the external uses port 80 to access the service uniformly, nginx proxies based on the path prefix and then returns the execution result. There are a few things to note about the Nginx reverse proxy path configuration that should be used with great caution.

The url configured for the proxy_pass command is http://127.0.0.1:8081. Note that the url cannot be replaced with /demo1 suffix, otherwise an error will be reported. Why is that? If the proxy_pass directive does not contain a URI, nginx will use the URI of the requested path for forwarding. If the url specified in the proxy_pass directive contains a URI, nginx ignores the uri specified in the request location and uses the uri specified in the proxy_pass directive to override, forward, and/as a URI

For example:

Suppose the request address is: http://localhost/demo/getServerInfo.json, the location configured for/demo, proxy_pass configured for http://xxxx:port, http://xxxx:port/demo/getSer will be used Verinfo. json is forwarded, and the result is correct. If proxy_pass is set to http://xxxx:port/demo1, http://xxxx:port/demo1 will be used for forwarding because /demo1 overrides /demo