Kubernetes Ingress Nginx 加载资源 404

Kubernetes Ingress Nginx loading resources 404

我们正在尝试让我们的网站在 kubernetes 上运行(运行 在使用 nginx 的容器中)。我们使用入口路由到站点,这是我们的配置:

nginx-conf:

server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

Kubernetes 部署:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mywebsite
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: mywebsite
    spec:
      containers:
      - name: mywebsite
        image: containerurl/xxx/mywebsite:latest
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  ports:
  - port: 82
    targetPort: 80

  selector:
    app: mywebsite

入口配置:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myIngress
  annotations:
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - xxx.com
    secretName: tls-secret
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /mywebsite
        backend:
          serviceName: mywebsite
          servicePort: 82

当我去xxx.com/mywebiste时,index.html正在加载,但是css和js没有加载(404):

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My Website</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link href="/styles.30457fcf45ea2da7cf6a.css" rel="stylesheet"></head>
  <body>
  <div id="react-root"></div>
  <script type="text/javascript" src="/app.171d6a95cf24d8abab14.js"></script></body>
</html>

...因为它试图在这里获取资源,例如 css:

xxx.com/styles.30457fcf45ea2da7cf6a.css

...而不是:

xxx.com/mywebsite/styles.30457fcf45ea2da7cf6a.css

如果尝试不同的事情,例如:

nginx.ingress.kubernetes.io/add-base-url
nginx.ingress.kubernetes.io/app-root

...等,但似乎没有任何效果。

有什么想法吗?感谢您的帮助。

此致, 彼得

这不是 nginx 部分的路由问题,而是浏览器试图从您域的根访问绝对 URI。使用相对 URI(删除前导斜杠):

<link href="styles.30457fcf45ea2da7cf6a.css" rel="stylesheet"></head>
<script type="text/javascript" src="app.171d6a95cf24d8abab14.js"></script></body>