0%

nginx 匹配 path,请求地址去掉path

需求

某个接口,通过api进行匹配反向代理到后端,实际请求的url地址需要去掉api

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
upstream api {
server localhost:8080 weight=5;
}

server {
listen 80;
server_name abc.com;
access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;

location ^~/api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;

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