举例
比如前端调用的地址为:http://localhost:8080/api/user/login
nginx配置为
location ~* ^/(api) { proxy_connect_timeout 15s; proxy_send_timeout 15s; proxy_read_timeout 15s; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; proxy_pass http://localhost:8080; }
以上的配置会带有/api路径
但我们不想要/api,应该配置为
location ~* ^/(api) { proxy_connect_timeout 15s; proxy_send_timeout 15s; proxy_read_timeout 15s; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; rewrite ^/api/(.*)$ /$1 break; proxy_pass http://localhost:8080; }
以上配置会转发到:http://localhost:8080/user/login