listen 80;
server_name localhost;
# 访问"localhost"的全部请求会被转发到"localhost:81"
# localhost => localhost:81
# localhost/a/ => localhost:81/a/
# localhost/b/ => localhost:81/b/
location / {
proxy_pass http://localhost:81;
}
# 访问"localhost"的全部请求会被转发到"localhost:81/a/"
# localhost => localhost:81/a/
# localhost/a/ => localhost:81/a/a/
# localhost/b/ => localhost:81/a/b/
location / {
proxy_pass http://localhost:81/a/;
}
# 访问"localhost/c/"的全部请求会被转发到"localhost:81/a/"
# localhost => 不会转发
# localhost/c/ => localhost:81/a/
# localhost/c/aa/ => localhost:81/a/aa/
location /c/ {
proxy_pass http://localhost:81/a/;
}
# 访问"localhost/api/"的全部请求会被转发到"localhost:81"
# localhost => 不会转发
# localhost/api/ => localhost:81
# localhost/api/a/ => localhost:81/a/
# localhost/api/b/ => localhost:81/b/
location /api/ {
# rewrite 的作用是修改URI
rewrite ^/api(/.*)$ $1 break;
proxy_pass http://localhost:81;
}
# http://localhost/api/ => http://127.0.0.1:81/
# http://localhost/api/a/ => http://127.0.0.1:81/a/
# http://localhost/api/b/ => http://127.0.0.1:81/b/
# http://localhost/api/?path=/a => http://127.0.0.1:81/a
# http://localhost/api/?path=/b => http://127.0.0.1:81/b
location /api/ {
if ($arg_path = '') {
rewrite ^/api(/.*) $1 break;
set $arg_path $1;
}
proxy_pass http://127.0.0.1:81$arg_path;
}