Kubernetes、Nginx、SPA 托管在共享域上

发布于 2025-01-10 19:15:34 字数 1488 浏览 0 评论 0原文

我正在开发一个托管在 kubernetes 上的项目,配置了一个入口,以便访问域根目录(我们称之为 example.com)定向到托管已运行的 spa 的 nginx 服务。某些 api 路径 /v1、/v2 等指向不同的 api 服务。

我们正在添加第二个 SPA,它将托管在 example.com/ui 下,

我可能应该提到这个 SPA 是使用 lit 元素(Web 组件)构建的,但经过预处理(缩小/转换/等)并通过 nginx 托管,而不是使用聚合物服务或类似的东西。处理是通过 rollupjs 和 tailwind-cli(用于 css)完成的。该站点也使用路由,因此其他路由 example.com/ui/someOtherRoute 定义 SPA 中的用户视图,但只会从服务器返回 index.html。

我从用于所有其他 SPA 的 nginx 配置开始,但它被设置为使用根域。

server {
listen   80;
listen   [::]:80 default ipv6only=on;

root /usr/share/nginx/html;
index index.html;

server_name _; # all hostnames

location / {
    try_files $uri /index.html;
}

add_header Access-Control-Allow-Origin *;

所以

我们遇到的情况是,如果uri没有/ui,它就不会路由到我的nginx服务器

当某些内容包含/ui时,我想检查匹配的文件,否则提供index.html

我没有太多经验使用 nginx 但一直在研究潜在的解决方案。

目前我正在尝试利用别名功能:

 location /ui/ {
    alias /usr/share/nginx/html;
    try_files $uri /index.html;
}

我的理解是,使用别名将从搜索路径中删除“/ui”,以便 example.com/ui/somefile.css 的请求将查找文件为如果是 example.com/somefile.css

但是我还发现了一个仍然处于打开状态的旧错误这里< /a>

说明利用try_files $uri 不受别名影响。因此 try_files 命令仍然必须在其文件查找中使用 /ui 。

我还尝试利用索引中的 html 基本标签,但这开始将 /ui/ui 添加到我的所有 css 和 js 脚本请求中。

我的理解可能有些偏差,但任何指导都会很棒。我对 kuberenetes 入口中可以更改的内容的控制有限,但我对 ngix 容器和 SPA 代码库拥有完全控制。

我们有一个后备计划,为新的 ui 应用程序添加一个单独的子域,这将起作用,并且我们之前已经做过很多次了。我真的很纠结这个问题,因为我确信它可以按原样解决,而且我真的很想弄清楚。

I'm working on a project thats hosted on kubernetes, theres an ingress configured so that going to the domain root (lets call it example.com) directs to an nginx service hosting an spa thats already running. certain api paths /v1, /v2 etc point to different api services.

We are adding a second SPA that will be hosted under example.com/ui

I should probably mention this SPA is built using lit element (web components), but gets pre-processed(minified/transpiled/etc) and hosted via nginx rather than using polymer-serve or something similar. The processing is done via rollupjs and tailwind-cli(for the css). Also the site using routing, so other routes example.com/ui/someOtherRoute define user views in the SPA but would just return the index.html from the server.

I started with an nginx config I use for all of my other SPAs however it's set up to use the root domain.

server {
listen   80;
listen   [::]:80 default ipv6only=on;

root /usr/share/nginx/html;
index index.html;

server_name _; # all hostnames

location / {
    try_files $uri /index.html;
}

add_header Access-Control-Allow-Origin *;

}

So we have a situation where if the uri doesn't have /ui it wont get routed to my nginx server

When something does contain /ui I want to check for matching files otherwise serve the index.html

I don't have much experience with nginx but have been researching potential solutions.

currently I'm trying to leverage the alias feature as such:

 location /ui/ {
    alias /usr/share/nginx/html;
    try_files $uri /index.html;
}

My understanding is that using the alias will strip the "/ui" from the search path so that a request of example.com/ui/somefile.css would look up files as if it was example.com/somefile.css

However I also found an old bug thats still open here

Stating that leveraging try_files $uri isn't effected by the alias. so the try_files command must still use the /ui in its file look up.

I also tried leveraging the html base tag in my index but that started adding /ui/ui to all of my css and js script requests.

I may be way off on my understanding here but any guidance would be great. I have limited control over what can be changed in the kuberenetes ingress, but I have complete control over the ngix container and the SPA code base.

We have a fallback plan of adding a separate sub domain for the new ui app, which will work and we've done many times before. I'm just really stuck in my head on this because I'm sure it can be solved as is and I'd really like to figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文