【发布时间】:2017-02-05 14:55:01
【问题描述】:
将我的 Angular2 应用程序移到 nginx 后面后,我在浏览器控制台中看到了这个:
GET https://localhost:4200/sockjs-node/info?t=1486305706324 net::ERR_CONNECTION_REFUSED
[WDS] Disconnected!
我的 nginx 代理是一个反向代理,它侦听 subdomain.domain.com 端口 443 (ssl) 并将其重定向到 Angular2 应用程序的端口 4200 上的 localhost。 所以在我的客户端应用程序的某个地方,它尝试解析 localhost:4200 而不是 subdomain.domain.com 端口 443 (ssl) 上的资源
我开始我的客户:
ng serve 0.0.0.0
我试过了:
ng serve --host subdomain.domain.com --port 443
没有运气。
我的 package.json 看起来像这样:
...
"name": "app",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\" --project src/tsconfig.json --type-check && tslint \"e2e/**/*.ts\" --project e2e/tsconfig.json --type-check",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
}
...
我还尝试编辑 /node_modules/webpack-dev-server/client/webpack.config.js 以包括: ...
entry: {
'client': "webpack-dev-server/client?https://subdomain.domain.com"
host: "https://subdomain.domain.com",
port: 443,
https: true
},
output: {
publicPath: 'https://subdomain.domain.com'
},
...
使用 nginx 配置更新:
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.com/privkey.pem;
server_name subdomain.domain.com;
access_log /var/log/nginx.access.log;
error_log /var/log/nginx_error.log debug;
location / {
include conf.d/proxy.conf;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://localhost:4200/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
server {
listen 80;
server_name subdomain.domain.com;
return 301 https://$host$request_uri;
}
更新了控制台日志和应用程序结构:
也没有任何运气。 关于如何告诉我在本地高端口 (4200) 上运行的应用程序在另一个端口上解析公共域上的资源的任何建议?
最好的问候。
【问题讨论】:
-
这个 NGINX 是在你的本地服务器上还是在生产服务器上?
-
在也托管 Angular 代码的本地服务器上。
-
您是否有不想使用 Angular CLI 提供的
ng serve的原因? -
不,没有。我只想能够在 nginx 反向代理后面提供我的 angular2 应用程序。