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

How to successfully retrieve VMware Esxi root password after forgetting it

Prepare a CentOS6 installation disk (any version)...

JavaScript implements bidirectional linked list process analysis

Table of contents 1. What is a doubly linked list...

How to use crontab to backup MySQL database regularly in Linux system

Use the system crontab to execute backup files re...

MySQL joint index effective conditions and index invalid conditions

Table of contents 1. Conditions for joint index f...

Vue3 list interface data display details

Table of contents 1. List interface display examp...

How to set up the use of Chinese input method in Ubuntu 18.04

In the latest version of Ubuntu, users no longer ...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

A complete guide on how to query and delete duplicate records in MySQL

Preface This article mainly introduces the method...

Detailed explanation of the installation steps of the MySQL decompressed version

1. Go to the official website: D:\mysql-5.7.21-wi...

Descending Index in MySQL 8.0

Preface I believe everyone knows that indexes are...

HTML table tag tutorial (23): row border color attribute BORDERCOLORDARK

In rows, dark border colors can be defined indivi...

Basic knowledge points of mysql worm replication

Worms replicate, as the name implies, by themselv...