Difference and principle analysis of Nginx forward and reverse proxy

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and reverse proxy

A forward proxy acts as a proxy for the client, and a reverse proxy acts as a proxy for the server.

1.1 Forward Proxy

A forward proxy server is located between the client and the server. In order to obtain data from the server, the client sends a request to the proxy server and specifies the target server. The proxy server then forwards the data returned by the target server to the client. Here the client needs to make some forward proxy settings.

Example: Climbing the Wall

In a forward proxy, the client's request is proxied

1.2 Reverse Proxy

In the reverse proxy, the client is unaware of the proxy and can access it without any configuration. The client sends the request to the reverse proxy server, which selects the target server to obtain the data and then returns it to the client. At this time, the reverse proxy server and the target server are one server to the outside world. What is exposed is the proxy server address, and the real server IP address is hidden.

2. Use of nginx reverse proxy

Create a new virtual host configuration in the nginx configuration file nginx.conf,

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

In the above configuration,

listen indicates the port that nginx will listen on;

server_name is the domain name entered in the browser when accessing nginx. You can directly fill in the IP address. If you want to bind multiple, you can separate them with spaces;

location indicates the URL to be matched when nginx listens to the port. If the URL accessing nginx contains /category/, the proxy is executed.

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

Note the path writing of proxy_pass here. If the path ends without / as above, it means a relative path. Then nginx will not intercept /category/ in the original URL when forwarding the request. For example, if the browser accesses http://localhost:8080/category/findAll, the request address actually sent by nginx is http://localhost/category/findAll.

If the target path of proxy_pass is configured with a / at the end, indicating an absolute path, then nginx will intercept the /category/ in the original URL when forwarding the request. For example, if the browser accesses http://localhost:8080/category/findAll, the request address actually sent by nginx is http://localhost/findAll.

When configuring, you need to decide whether to include / according to your needs, otherwise nginx will always report a 404 error in actual use.

My configuration here contains /category in my backend interface path, so I use a relative path and keep this.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Using nginx forward proxy to implement intranet domain name forwarding process analysis
  • Diagram of the process of implementing direction proxy through nginx
  • Detailed explanation of nginx forward proxy and reverse proxy
  • A universal nginx interface to implement reverse proxy configuration
  • Interview questions about forward proxy and reverse proxy in distributed architecture

<<:  js realizes horizontal and vertical sliders

>>:  MySQL 8.0.18 installation and configuration method graphic tutorial

Recommend

Linux debugging tools that developers and operators must look at [Recommended]

System performance expert Brendan D. Gregg update...

Native js to achieve seamless carousel effect

Native js realizes the carousel effect (seamless ...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

How to use the realip module in Nginx basic learning

Preface There are two types of nginx modules, off...

17 404 Pages You'll Want to Experience

How can we say that we should avoid 404? The reas...

MySQL database rename fast and safe method (3 kinds)

Table of contents How to rename MySQL database Th...

Introduction to SSL certificate installation and deployment steps under Nginx

Table of contents Problem description: Installati...

Vue shopping cart case study

Table of contents 1. Shopping cart example 2. Cod...

React dva implementation code

Table of contents dva Using dva Implementing DVA ...