node.js - NginX : 为 Node API 提供静态内容和代理传递
问题描述
我正在运行一个网站,该网站由端口 8080 上的 docker 中的 API 和包含前端反应应用程序的静态文件夹组成。
api位于/api
我正在尝试配置 NginX 以正确地提供此配置,但我无法弄清楚如何让两者都工作。
如果我default
这样设置:
server {
root /var/www/html;
server_name DOMAIN; # managed by Certbot
location @nodeapp {
# Redirect to the api
proxy_pass http://localhost:8080;
}
location / {
# Map all the routes to the index.html
try_files $uri @nodeapp;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/DOMAIN/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/DOMAIN/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
然后一切都被重定向到我的 API,如果没有明确指定(例如转到https://host/index.html) ,则不会传递静态内容
如果我添加try_files $uri $uri/ @nodeapp;
以将根文件夹作为目录提供服务,则索引将被提供,但我无法再访问 API,它始终为反应应用程序提供服务
我也尝试添加
location /api/ {
proxy_pass http://localhost:8080
}
但是没有区别
我究竟做错了什么 ?
解决方案
我找到了答案,我使用的 React 应用程序是使用服务人员设置的,这会弄乱路由。我删除了它,它现在就像一个魅力!
推荐阅读
- python - 如何修复 python GUI 中的无响应错误
- xml - Powershell 生成 XML 文件
- sonos - 仅允许某些设备连接 Sonos 扬声器(Unifi 系统)
- java - SQL - 打印用户在字符串变量中给出的记录
- python - Powershell下运行pip和virtualenv(记事本窗口)
- ios - 如何在 swift 中使用 PageController 创建自定义的水平 UICollectionView
- android - 我们可以在我们的基础应用程序代码中访问按需动态模块的类/功能吗?
- vue.js - 如何在 VueJS 中为 Chromium 扩展定制和自动化构建应用程序
- python - 如何在`os.path.join`中保存`to csv`并服务到另一条路线以在flask python中下载回来
- flutter - 在 Flutter/Dart 中:在 AppBar 中显示两行文本?