1. The concept of forward proxy and reverse proxy
Whether it is a forward proxy, or reverse proxy, in the final analysis, is the derivative version of the proxy mode. We've all learned about proxy design patterns, and we all know that there are proxy roles and proxy roles in the proxy pattern, because these two roles are important for us to understand forward and reverse proxies, as described below.
Below I will introduce such a scene, many times our internet connection speed is particularly slow, or because of XXX problems that we can not access to foreign sites, usually these circumstances we will be configured by the browser a fast speed, can XXX proxy IP and port number to solve our problems, then the configuration is good, The approximate request flow is as follows:
We first request the proxy server, and then the proxy server to help us to quickly access foreign sites, for this proxy way, we call it a forward proxy. Keep in mind that in the two roles that refer to proxy mode, our current role is the proxy, which is the role of the browser. More importantly, the nature of the forward proxy is that we go to the request of external resources, if the producer, consumer model to differentiate, we belong to consumers.
Summarize:
1, positive agent, our role is to be the agent
2, is the agent, we do not provide services to the outside, but the external consumer services, belonging to consumers
Reverse proxy, it is clear, is the opposite of the proxy, if the positive proxy is male, then the reverse proxy is a female, pro, here no longer tangled in other situations! Below I use a picture to explain the reverse proxy:
After reading the above picture, please imagine this scenario, if you are now a company technical director, your company needs to provide a set of Web services, then what are you going to do?
The answer can be done through a reverse proxy. Usually your company has its own IDC room, computer room communication usually use LAN switch, Internet network user request is not directly accessible to the Web services within the LAN, so 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 then responding after the processing is complete. Therefore, the reverse proxy is probably one such scenario. Keep in mind that our role in the reverse proxy is the LAN Web service.
Summarize:
1, reverse proxy, our role is the LAN Web service
2, reverse proxy, we provide services to the outside, belong to the service provider
2, Nginx forward proxy and reverse proxy instance resolution
Nginx in the forward proxy aspect of the application is very small, therefore, for the forward proxy configuration directives are not many, the following is a nginx as a forward proxy server configuration instance, configuration is for reference only.
server {
Resolver 192.168.1.1; #指定DNS服务器IP地址
Listen 8080;
Location/{
Proxy_pass http://$http _host$request_uri; #设定代理服务器的协议和地址
}
}
Explain the above instructions, resolver configure the IP address of the DNS server, you can configure multiple. You might ask, why do you need to configure the IP address of the DNS server in the forward proxy? In fact, the answer is very simple, you imagine that if your browser is configured with a forward proxy server, you now enter the Web page link in the browser, according to the principle of forward proxy, the URL request will be executed to the proxy server, the problem is, if your proxy server does not configure the DNS resolution service, Nginx How to know you this onesite.cn is a what ghost, exactly corresponds to the Internet IP address is what? So that's why you need to configure the resolver directive.
The listen instruction configures the port number of the browser request that Nginx listens to.
Proxy_pass instruction configuration receives the request sent by the Proxy browser, what needs to help execute the request, $http _host$request_uri indicates the destination host and Uri, is the nginx variable, generally do not need to modify.
Nginx Reverse proxy configuration is as follows, here is a small set up two spring boot small demo simulation above the reverse proxy Web services, the relevant source code can be obtained on GitHub.
Demo project start port number for the 8081,DEMO1 project start ports of 8082, for all the request prefix with/demo will be forwarded to the demo project for processing, for all the request prefix with/DEMO1 will be forwarded to the DEMO1 project for processing.
The Nginx configuration is as follows:
server {
Listen 80;
Location/demo {
Proxy_pass Web Links
}
Location/demo1 {
Proxy_pass Web Links
}
}
After starting the demo and Demo1 two projects, the browser enters the following address:
As you can see, the external unified use of the 80 port Access service, Nginx based on the path prefix proxy, and then return to the execution results. For Nginx reverse proxy path configuration There are a few points to note, use very carefully.
The URL of the PROXY_PASS directive configuration above is a Web page link, note that after the URL can not be replaced with the/demo1 suffix, or error. Why? The first nginx will determine whether the URL address configured in the Proxy_pass directive contains a URI, and if the URL address configured in the PROXY_PASS directive does not contain a URI, then Nginx will use the URI of the request path for forwarding if the Proxy_ The URL address configured in the pass instruction contains the URI, and Nginx ignores the URI in the request location, overwriting it with the URI you configured in the Proxy_pass, forwarding it, and/or a URI, haha, be especially careful.
Example:
Assuming that the request address is: Http://localhost/demo/getServerInfo.json,location is configured for/demo,proxy_pass and configured as Http://xxxx:port, it will use HTTP// The Xxxx:port/demo/getserverinfo.json is forwarded with the correct results. If the Proxy_pass is configured as HTTP://XXXX:PORT/DEMO1, it is forwarded with Http://xxxx:port/demo1 because/demo1 overrides the/demo
Oh yes, don't forget to look at it.
Analysis of Nginx forward proxy and reverse proxy