移除上下文路径

location和proxy_pass都带/

proxy_pass http://api/ 的 api后面带/,则会移除api这个上下文

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;

    proxy_pass http://api/;
}

使用rewrite

proxy_pass http://api 的 api后面不带/,通过rewrite移除api这个上下文

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;
}

保留上下文路径

proxy_pass 结尾不带/

location ^~ /api/  {
    proxy_pass http://api;
}

location ^~ /api {
    proxy_pass http://api;
}