current position:Home>[nginx] prefix removal in nginx proxy pass configuration

[nginx] prefix removal in nginx proxy pass configuration

2022-04-29 17:53:03Maosi

brief introduction

In our daily use , It is often necessary to remove the prefix , For example, when we are doing multiple service projects , stay nginx We need to put https://moonce.com/user-api/permission?userId=1 Forwarding to http://localhost:8089/permission?userId=1

Complete agent

server {
    
    listen              80;
    server_name         moonce.com;

    location /user-api/ {
    
        proxy_pass http://localhost:8089;
		index index.html index.htm;
        proxy_no_cache 1;
        proxy_cache_bypass 1;
        chunked_transfer_encoding on;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 30;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 512k;
    }

}

Go to prefix proxy

proxy_pass Back plus /

server {
    
    listen              80;
    server_name         moonce.com;

    location /user-api/ {
    
        proxy_pass http://localhost:8089/;
		index index.html index.htm;
        proxy_no_cache 1;
        proxy_cache_bypass 1;
        chunked_transfer_encoding on;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 30;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 512k;
    }

}

Use rewrite rewrite

server {
    
    listen              80;
    server_name         moonce.com;

    location /user-api/ {
    
		index index.html index.htm;
        proxy_no_cache 1;
        proxy_cache_bypass 1;
        chunked_transfer_encoding on;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 30;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 512k;

 		rewrite ^/user-api/(.*)$ /$1 break;
        proxy_pass http://localhost:8089;
    }

}

copyright notice
author[Maosi],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291611163166.html

Random recommended