nginx - 使用 nginx 的 Angular 2 部署
问题描述
以下是我使用 Nginx 在本地部署 Angular2 应用程序所遵循的步骤:
- 在我机器的某个地方下载并解压 Nginx。
- 使用 ng build 命令 ng build --prod 为生产构建我的 Angular 应用程序
-打开../nginx-1.13.8/conf/nginx.conf,添加如下脚本:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 9090;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /device-listing/ {
proxy_pass https://apim.azure-api.net/devicelisting/device-listing;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 导航到 ../nginx-1.13.8/html,并在其中复制所有从 app/dist 文件夹生成的文件。
- 双击 ../nginx-1.13.8/nginx.exe 运行 Nginx 服务器。
- 使用浏览器打开 localhost:9090,我的 Angular 应用程序已启动并正在运行。
但是当我点击导航链接时,它给了我 405 Not Allowed 错误!!!!!!基本上它不会在单击导航链接时重定向到 api 调用
网络详情:
请建议我还需要做什么配置?
解决方案
看起来您错误地POST
访问了无效的 URL,而不是您的预期 IP 地址。
您的帖子请求转到http://localhost:9090/null
,您是否打算改为转到http://localhost:9090/device-listing
?
如果您的 API 仍然无法工作,这可能是原因:
proxy_pass
会将整个路径传递给代理。因此,当有人访问/device-listing/
您的 Web 服务器时,他们的请求将被传递给:
https://apim.azure-api.net/devicelisting/device-listing/device-listing
注意device-listing
重复两次。
如果我不得不猜测,那不是您后端的有效 API 方法,因此您的 API 返回 405 Not Allowed。
proxy_pass
将您的指令更改为https://apim.azure-api.net/devicelisting/
应该使其按预期工作。
推荐阅读
- gtk - WebKitGTK 如何确定每像素的设备像素?
- python - 如何在 Keras Python 框架中使用 VGG16 实现区域建议网络 (RPN) 的增量?
- sql - 如何在 SQL 中读取“2021 年 8 月 2 日”格式的日期
- amazon-web-services - 对 lambda 的 HTTP 不安全请求
- amazon-web-services - 如何将包与 Cmake 和 Make 文件集成到 Buildroot 中
- elasticsearch - Elasticsearch:按子文档嵌套对象的字段排序
- excel - 如何将某些信息从行移动到 VBA excel 中的列中?
- javascript - 在反应中将组件存储在对象中
- security - 没有服务器端渲染的 CSRF 令牌
- apache-kafka - 获取主题元数据时超时 - Kafka