在开发期间使用http-proxy-middleware插件代理到后端接口,规则匹配写着比较灵活,但是在使用nginx部署的时候遇到了一些坑,这里记录一下。

模块一后端接口地址:http://127.0.0.1:8080
模块二后端接口地址:http://127.0.0.1:8081
鉴权后端接口地址:http://127.0.0.1:8082/auth

http-proxy-middleware代理设置

    proxyTable:{
        '/apiOne':{
            target: 'http://127.0.0.1:8080',
            pathRewrite: {
                '^/apiOne': '/'
            }
        },
        '/apiTwo':{
            target: 'http://127.0.0.1:8081',
            pathRewrite: {
                '^/apitwo': '/'
            }
        },
        '/auth':{
            target: 'http://127.0.0.1:8082',
        }
    }

nginx代理设置

    location ^~ /apiOne/ {
        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header  Host $host:$server_port;
        proxy_pass http://127.0.0.1:8080/;
    }

    location ^~ /apiTwo/ {
        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header  Host $host:$server_port;
        proxy_pass http://127.0.0.1:8081/;
    }
    location ~ ^/auth {
        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header  Host $host:$server_port;
        proxy_pass http://127.0.0.1:8082;
    }