从Angular 5 Universal获取域始终返回127.0.0.1 [英] Get domain from Angular 5 Universal always return 127.0.0.1

查看:59
本文介绍了从Angular 5 Universal获取域始终返回127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Angular 5应用程序的服务器端渲染有问题.我需要域来创建正确的链接(应用程序在不同的环境中使用,并且为每个端点创建几个程序包是个坏主意).

I have a problem with Server Side Rendering of Angular 5 apps. I need domain to create proper link (app is used in different environments and it's bad idea to create few packages to each endpoint).

因此,我尝试使用堆栈中的选项,但始终将127.0.0.1:4000作为域/主机.我认为最好的选择应该是:

So I try to use option from stack, but always i get 127.0.0.1:4000 as a domain/host. I think that the best option should be:

server.ts

server.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中:

And in app.component.ts:

   constructor(@Inject(PLATFORM_ID) private platformId,
                private injector: Injector) {

        if (isPlatformServer(this.platformId)) {
            let req = this.injector.get('request');
            console.log("host: " + req.get('host'));
        } else {
            console.log('we\'re rendering from the browser, there is no request object.');
        }
    }

我还检查了NodeJS的对象option->它没有有关域的任何信息.此处将host定义为127.0.0.1:4000.我不知道如何从NodeJS中获取它.您可以帮忙还是给一些小提示?

I check also object option from NodeJS -> it doesn't has any information about a domain. host is defined there as 127.0.0.1:4000. I doesn't has any idea how can I get it from NodeJS. Can you help or give small tips?

也许我的错是我的Nginx配置错误?

Maybe my fault is that I has wrong Nginx configuration?

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name mock.test.com;

    ssl_certificate /home/xxx/Projects/xsw/cert/certificate.chained.crt;
    ssl_certificate_key /home/xxx/Projects/xsw/cert/key.prv;

    root /home/xxx/Projects/xsw/;
    index index.html;

    location / {
        proxy_pass http://127.0.0.1:4202;
    }

}

推荐答案

我今天失去了大约10个小时,但终于找到了解决方案.我的问题与nginx配置有关.

I lose today about 10 hours but finally I found a solution. My problem was connected with nginx configuration.

当我使用时:

location / {
    proxy_pass http://127.0.0.1:4000;
}

Nginx将请求中的主机更改为127.0.0.1:4000.因此,解决方案是将nginx代理编辑为:

Nginx change host in request into 127.0.0.1:4000. So a solution for this is edit nginx proxy into:

location / {
    proxy_pass http://127.0.0.1:4000;
    proxy_set_header Host example.com;
    proxy_set_header HTTPS on;
}

我在Express Express的Node.js服务器中什么都没有更改:

I doesn't change anything in my express node.js server:

server.ts

server.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中,我使用以下配置:

And in my app.component.ts I use this config:

   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

To create this answer I use another answer from StackOverflow: https://stackoverflow.com/a/47434417/2972087 And part of Nginx Docs: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

这篇关于从Angular 5 Universal获取域始终返回127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆