今天我损失了大约 10 个小时,但最终我找到了解决方案。我的问题与 nginx 配置有关。
当我使用时:
location / {
proxy_pass http://127.0.0.1:4000;
}
Nginx 将请求中的主机更改为 127.0.0.1:4000。因此,解决方案是将 nginx proxy 编辑为:
location / {
proxy_pass http://127.0.0.1:4000;
proxy_set_header Host example.com;
proxy_set_header HTTPS on;
}
我没有更改我的 Express Node.js 服务器中的任何内容:
服务器.ts
app.engine('html', (_, options, callback) => {
const engine = ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
{ provide: 'request', useFactory: () => options.req, deps: [] },
provideModuleMap(LAZY_MODULE_MAP)
]
});
engine(_, options, callback);
});
在我的 app.component.ts 中,我使用以下配置:
private domain: string;
constructor(@Optional() @Inject(REQUEST) private request: any,
@Inject(PLATFORM_ID) private platformId,
private injector: Injector) {
if (isPlatformServer(this.platformId)) {
if (this.request.get('https')) {
this.domain = 'https://' + this.request.get('host');
} else {
this.domain = 'http://' + this.request.get('host');
}
} else {
this.domain = document.location.protocol + '//' + document.location.hostname;
}
}
为了创建这个答案,我使用了 StackOverflow 的另一个答案:https://stackoverflow.com/a/47434417/2972087以及 Nginx 文档的一部分:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header