用express替代nginx作为反向代理

用express替代nginx作为反向代理,express,nginx,reverse-proxy,http-proxy,Express,Nginx,Reverse Proxy,Http Proxy,我想用node js express应用程序取代作为反向代理的nginx,这样我就可以动态设置规则,并有更好的日志记录和测试可能性 我的nginx设置如下所示: http { include mime.types; default_type application/octet-stream; sendfile off; keepalive_timeout 65; gzip on; server {

我想用node js express应用程序取代作为反向代理的nginx,这样我就可以动态设置规则,并有更好的日志记录和测试可能性

我的nginx设置如下所示:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        off;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       8023;
        server_name  localhost;
        sendfile        off;

        location / {
            proxy_pass https://main.app.url.com/;
        }

        location /module1/v1/ {
            client_max_body_size 30000M;
            client_body_buffer_size 200000k;
            # local backend of module 1
            proxy_pass http://localhost:8080/module1/v1/;
        }

        location /module1/v1/web/ {
            # local front end files for module 1
            alias /some/local/path/build/dist;
        }

        location /module2/v1/web/ {
            # local front end files for module 1
            alias /some/other/local/path/build/dist;
        }
    }
}
const express   = require('express');
const app       = express();
const isUrl     = require('is-url');
const proxy     = require('express-http-proxy');

...

/**
 * Function adding new redirects to the current server instance. If the target URL is an URL the request will be
 * handeled with the express-http-proxy middleware. If the target URL is some local directory the content will be serverd using the express-static middleware.
 * @param matchedPath The path (endpoint) for which the redireciton should be configured.
 * @param targetUrl The target URL or directory for the redirection.
 */
const setUpRedirect = (matchedPath, targetUrl, name) => {
    // Use proxy for Urls 
    if (isUrl(targetUrl)) {
        app.use(matchedPath,
            proxy(targetUrl,
                {
                    memoizeHost: false,
                    limit: '50mb',
                    proxyReqPathResolver: function(req) {
                        // I do not have (yet) any clue why I had to do this but it fixed the behavior
                        return req.originalUrl;
                    },
                }),
        )
    }
    else { // When targetUrl is directory serve static files from there
        app.use(matchedPath,
            express.static(targetUrl),
        );
    };  
};

...

setUpRedirect('/module1/v1/web/:major([0-9]+).:minor([0-9]+).:bugfix([0-9]+)', '/some/local/path/build/dist';)
setUpRedirect('/module2/v1/web/:major([0-9]+).:minor([0-9]+).:bugfix([0-9]+)', 'http://some/remote/url/';)
server = app.listen(8080);
我曾尝试使用ExpressHTTP代理中间件,但我很难将上述规则应用于此。首先,我不完全理解proxy_pass和alias指令之间的区别

第二,我尝试了以下几点:

const express   = require('express');
const app       = express();
const proxy     = require('express-http-proxy')

const path = '/some/local/path/build/dist';

app.all('/module1/v1/web/', proxy(path, {
            proxyReqPathResolver: (req) => {
                return '';
            }
        })
    );
};
我有一个错误:

TypeError: Cannot read property 'request' of undefined
    at /Users/user1/Dev/local-dev-runner/node_modules/express-http-proxy/app/steps/sendProxyRequest.js:13:29
    at new Promise (<anonymous>)
    at sendProxyRequest (/Users/user1/Dev/local-dev-runner/node_modules/express-http-proxy/app/steps/sendProxyRequest.js:11:10)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
TypeError:无法读取未定义的属性“request”
at/Users/user1/Dev/local Dev runner/node_modules/express http proxy/app/steps/sendProxyRequest.js:13:29
在新的承诺()
在sendProxyRequest(/Users/user1/Dev/local Dev runner/node_modules/express http proxy/app/steps/sendProxyRequest.js:11:10)
在
在进程中。_tick回调(内部/process/next_tick.js:188:7)

尽管
cont-path=http://www.google.com“
返回了一个有效的响应。

这就是我的答案:

诀窍是服务本地文件和代理web请求。显然,nginx可以自动识别请求URI是本地路径还是远程路径

我决定使用nmp包,因此,工作解决方案如下所示:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        off;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       8023;
        server_name  localhost;
        sendfile        off;

        location / {
            proxy_pass https://main.app.url.com/;
        }

        location /module1/v1/ {
            client_max_body_size 30000M;
            client_body_buffer_size 200000k;
            # local backend of module 1
            proxy_pass http://localhost:8080/module1/v1/;
        }

        location /module1/v1/web/ {
            # local front end files for module 1
            alias /some/local/path/build/dist;
        }

        location /module2/v1/web/ {
            # local front end files for module 1
            alias /some/other/local/path/build/dist;
        }
    }
}
const express   = require('express');
const app       = express();
const isUrl     = require('is-url');
const proxy     = require('express-http-proxy');

...

/**
 * Function adding new redirects to the current server instance. If the target URL is an URL the request will be
 * handeled with the express-http-proxy middleware. If the target URL is some local directory the content will be serverd using the express-static middleware.
 * @param matchedPath The path (endpoint) for which the redireciton should be configured.
 * @param targetUrl The target URL or directory for the redirection.
 */
const setUpRedirect = (matchedPath, targetUrl, name) => {
    // Use proxy for Urls 
    if (isUrl(targetUrl)) {
        app.use(matchedPath,
            proxy(targetUrl,
                {
                    memoizeHost: false,
                    limit: '50mb',
                    proxyReqPathResolver: function(req) {
                        // I do not have (yet) any clue why I had to do this but it fixed the behavior
                        return req.originalUrl;
                    },
                }),
        )
    }
    else { // When targetUrl is directory serve static files from there
        app.use(matchedPath,
            express.static(targetUrl),
        );
    };  
};

...

setUpRedirect('/module1/v1/web/:major([0-9]+).:minor([0-9]+).:bugfix([0-9]+)', '/some/local/path/build/dist';)
setUpRedirect('/module2/v1/web/:major([0-9]+).:minor([0-9]+).:bugfix([0-9]+)', 'http://some/remote/url/';)
server = app.listen(8080);