Nginx代理proxy pass配置去除前缀,VUE发布部署
简介
在我们日常使用中,经常会遇到需要去除前缀得情形,比如我们在做多个服务项目时,在nginx中我们需要把https://moonce.com/user-api/permission?userId=1 转发到 http://localhost:8089/permission?userId=1
完整代理
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; } }
去前缀代理
proxy_pass后面加 /
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; } }
使用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; } }