SoFunction
Updated on 2025-03-04

nginx is based on secondary directory forwarding services and the difference between with/ and without/

nginx forwards services based on secondary directory and differences with/and without/

When configuring proxy forwarding in nginx, if / is added to the url after proxy_pass, it indicates the absolute root path;

If / is not present, it means the relative path and also passes the matching path part to the proxy.

Specific examples are as follows:

1. Configuration file location belt/ and proxy_pass belt/

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service/ {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080/;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, if accessed /service/

The actual access to the backend service http://192.168.1.51:8080/

2. Configuration file location with/ and proxy_pass does not

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service/ {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

That is to access /service/

The actual access to the backend service http://192.168.1.51:8080/service/

3. Configuration file location with / and proxy_pass with secondary directories and /

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service/ {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080/api/;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, access to/service/

The actual access to the backend service http://192.168.1.51:8080/api/

4. Configuration file location with/ and proxy_pass with secondary directory without/

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service/ {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080/api;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, access to/service/

The actual access to the backend service http://192.168.1.51:8080/

Without/ and proxy_pass with secondary directory without/

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080/api;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, access to/service/

The actual access to the backend service http://192.168.1.51:8080/api/

Without/and proxy_pass with/

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080/;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, access to/service/

The actual access to the backend service http://192.168.1.51:8080//

No/and proxy_pass does not come with/

server {

        listen 80;
        server_name ;
        #Access the background by accessing the service secondary directorylocation /service {
            # http://192.168.1.51: The slash behind 8080 is a key. If there is no slash, the service will be passed to the backend node.            proxy_pass      http://192.168.1.51:8080;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

That is, access to/service/

The actual access to the backend service http://192.168.1.51:8080/service/

Summarize

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