SoFunction
Updated on 2025-03-04

How to implement request forwarding function using nginx

Nginx (reverse proxy server) main functions

1. Request for forwarding

nginx gets the client request, and then forwards it to the specific server according to the path matching.

For example, the request address contains eduservice, then forward to port 8001

Client (browser) → → → request → → nginx (port number 9001) → → → forward → → server eduservice (port number 8001)

euoss (port number 8002)

2. Load balancing

nginx gets the client request, and then shares the requests evenly among different services

Client (browser) →→→ Request →→→ nginx (port number 9001) →→→ Load balancing →→→ Cluster edu (port number 8081)

edu (port number 8082)

3. Separation of movement and stillness

Deploy java code, pages, pictures, etc. separately

For example, put the java code part in Tomcat, and then find a server to place static resources (html, pictures, etc.)

In operation, when determining that the access resource is Java code, request tomcat, and when determining that the access content is html, picture..., access the static server.

Configure nginx to implement the function of request forwarding

1. Find nginx configuration file

E:\develop\web\nginx-1.20.2\conf\

2. Configure in

(1) It is best to change the default 80 port of nginx to 81

(2) Configure nginx forwarding rules

    ~ Indicates regular match
    listen Listen to port
    server_name Host Name
    location Match paths
    proxy_pass The address of the server to be forwarded to

(3) Specific configuration

    http {
        server {
            listen       81;
            ......
        },
        ......
        server {
            listen       9001;
            server_name  localhost;

            location ~ /eduservice/ {
                proxy_pass   http://localhost:8001;
            }

            location ~ /eduoss/ {
                proxy_pass   http://localhost:8002;
            }

        }
    }

3. Modify BASE_API in config/

BASE_API: '"http://localhost:9001"', // Modify the front-end request address tonginxaddress

4. Restart nginx

Use cmd to start nginx. If you close the cmd window, nginx will not stop

  • Stop: -s -s -s
  • start up:

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.