【发布时间】:2018-11-12 08:45:54
【问题描述】:
我想用 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;
}
}
}
我尝试使用 express-http-proxy 中间件,但我很难将上述规则应用于它。首先,我不完全理解 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)
虽然cont path = 'http://www.google.com'返回了一个有效的响应。
【问题讨论】:
标签: express nginx reverse-proxy http-proxy