Nginx forward reverse proxy distinction and principle analysis

  • 2020-05-24 06:50:51
  • OfStack

1. Difference between forward agent and reverse agent

Forward proxy proxy client, reverse proxy proxy server.

1.1 forward agency

The forward proxy server is located between the client and the server. In order to obtain data from the server, the client will send a request to the proxy server and specify the target server. The proxy server will forward the data returned by the target server to the client. Here the client needs to set up some forward agents.

Example: over the wall

The proxy in the forward proxy is the client's request

1.2 reverse proxy

Reverse proxy, the client's agent is no perception, the client can access does not require any configuration, the client sends a request to the reverse proxy server, the reverse proxy server to select the target server to get data, returned to the client, the reverse proxy server and the target server external is a server, exposure is a proxy server address, and hide the real server IP address.

2. Use of nginx reverse agent

In the configuration file nginx.conf of nginx, create a new configuration of a virtual host,


server {
    listen    8080;
    server_name localhost;
    
    location /category/ {
      proxy_pass http://localhost;
    }
  }

In the above configuration,

listen represents the port on which nginx is listening;

server_name refers to the domain name entered in the browser when accessing nginx. You can directly fill in the ip address.

location represents the url to match when nginx listens on the port, and if the url of access nginx contains /category/, the agent is executed

proxy_pass represents the target to which nginx will proxy the client's request.

Note proxy_pass path to write here, if you like it, don't take/at the end of the path, represents the relative path, so nginx in forwarding requests can not fall in the original url category/intercept, such as a browser to access http: / / localhost: 8080 / category/findAll, http is the actual request from nginx address: / / localhost category/findAll.

If proxy_pass target path taken on/at the end of configuration represents an absolute path, then nginx forward the request of the original url will intercept fall/category /, such as a browser to access http: / / localhost: 8080 / category findAll http is the actual request from nginx address: / / localhost/findAll.

When configuring, you should decide whether you need to bring/or not according to your own needs, otherwise nginx will report 404 error in 1.

My configuration here is because my back-end interface path contains /category, so I use the relative path and keep this.


Related articles: