node.js - 如何配置 --public-host 以在 nginx 反向代理后面运行 angular 通用应用程序?
问题描述
我想用 HMR 运行一个通用应用程序,我将这个问题建立在它实际上可能的假设之上。
我已经设置并运行了一个默认的“ng new”angular 8 应用程序并按预期工作。为了在反向代理后面运行它,我使用修改后的 npm start 运行
e.g. { "start": "ng serve --host 0.0.0.0 --port 3604 --disable-host-check --public-host //domain.com/spa/sockjs-node/" }
我正在使用 nginx 将 domain.com/spa/sockjs-node/ 路由到 domain.com:3604/sockjs-node
这工作正常,我让 HMR 在 nginx 反向代理后面工作。
我希望与 HMR 一起运行一个有角度的通用应用程序,但我不知道如何为通用应用程序设置 --public-host //domain.com/universal/sockjs-node/ 因为它显然不再使用 ng serve 来运行. 如果没有此设置,则在搜索资产和套接字时,角度不会插入路径的 /universal/ 部分。
所以我在这种情况下的问题是:我如何为一个有角度的通用应用程序设置 --public-host?
谢谢
解决方案
对 create-react-app 默认设置和 nginx 进行以下更改。
索引.html
add <base href="/ssr/"> in <head>
nginx.default.conf
server ...
{
location ^~ /ssr/sockjs-node/ {
proxy_pass http://0.0.0.0:3604/sockjs-node/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ^~ /ssr/ {
proxy_pass http://0.0.0.0:3604/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
服务器.ts
const PORT = process.env.PORT || 3604;
推荐阅读
- database - ansible 无法执行 SQL CREATE DATABASE CREATE DATABASE 无法在事务块内运行
- javascript - 如何在开发工具中打印值
- java - 为什么使用 MockMVC MockMvc 时 Spring Security CSRF 检查失败
- javascript - 当 resizeMode 设置为“包含”时,borderRadius 对图像不起作用
- javascript - 从AngularJS中的服务器下载文件时如何更改文件名
- c++ - 当前标准中的“初始化表达式”是什么意思
- string - 对字符串切片进行排序:按字母顺序*在*降序后的字母
- nuxt.js - Nuxt js 中间件身份验证将经过身份验证的用户重定向到登录页面
- javascript - 无法在 Django 中显示表单
- c++ - 仅具有静态方法的模板类使用 .cpp 文件实现给出错误