The difference between forward and reverse proxies

The essential difference

In forward proxy, the server does not know who the real client is. In a reverse proxy, the client does not know who the real server is

The illustration

  • The active agent of forward proxy is the user, which is mainly used to resolveCross domainQuestion, andhiddenThe role of user access records

  • The active side of a reverse proxy is the server, which mainly providesLoad balancing,Safety protectionThe action such as

The difference on the level of action

  • Forward proxy is mainly used for resolutionCross domainQuestion, andhiddenThe role of user access records
  • Reverse proxy is mainly providedLoad balancing,Safety protectionThe action such as

Why can forward proxies solve cross-domain problems

Because servers do not need to follow the same origin policy

Why can a forward proxy hide user access records

Because the forward proxy server does not know the specific user accessing the server resources, the server cannot know the real IP address of the user

Why can a reverse proxy provide load balancing

  • The number of requests sent by the client (received by the Nginx reverse proxy server) is what we are talking aboutload
  • Distributing the number of requests to different servers according to certain rules is oneequilibrium

Therefore, the process of distributing the requests received by the server according to certain rules is called load balancing

This is the core technical principle of CDN

Why can a reverse proxy provide security protection

In fact, forward proxy can also provide security protection function, that is, proxy can implement security protection, the principle is as follows:

If user B is blacklisted, his request can be blocked by a proxy server

Application of cache

In addition, proxy can solve the role of cross-domain, load balancing, and security protection

CDN cache refers to the reverse proxy in which the proxy server caches the resources of the server. In the region under the jurisdiction of the proxy server, as long as the first user requests resources, all users in the region can quickly access resources from the proxy server, and there is no need to request resources from the server all the way

Use Nginx to implement reverse proxy

1. Run two simple programs using IDEA, occupying two ports 8081 and 8082 respectively. Here I still borrow the program in the last blog, and the code is very simple. Take nginx1 running port 8081 as an example:

@RestController
public class HelloController {
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String say(a){return "This is port 8081.";}
}
Copy the code

application.yml

server:
  port: 8081
Copy the code

2. Modify the local hosts file (C:\Windows\System32\drivers\etc).



Change the Local Domain Name

3. Run and start Nginx. Modify the nginx.conf configuration file

upstream  aaa{    
    server  127.0. 01.:8081;  
}
upstream  bbb{    
    server  127.0. 01.:8082;  
}     
server {
    listen  80; server_name www.aaa.com; Location /{proxy_pass HTTP://aaa/index; # proxy address
	index  index.html index.htm;
    }
}
server {
    listen  80;
    server_name www.bbb.com;
    location  /{
        proxy_pass http://bbb/index;index index.html index.htm; }}Copy the code

4. Run the nginx -s reload command to reload the nginx





You can see that the inputs www.aaa.com and www.bbb.com were assigned to 8081 and 8082 servers by the Nginx reverse proxy, respectively.

Refer to the article

  • Graphic forward proxy, reverse proxy, transparent proxy
  • The difference between forward and reverse proxies 
  • Finally someone put forward agent and reverse agent explain clearly!
  • Analysis of Nginx reverse proxy and load balancing