简体   繁体   English

nginx 入口 controller 忽略 css 和 js 文件 - 谷歌 kuber.netes 引擎

[英]nginx ingress controller ignoring css and js files - google kuberenetes engine

I've created an nginx ingress controller that is linked to two services.我创建了一个链接到两个服务的 nginx 入口 controller。 The website works fine but the js and css files are not loaded in the HTML page (404) error.网站运行正常但是HTML页面(404)错误中没有加载js和css文件。 I've created nginx pod using helm, and Included the nginx config in the ingress.yaml.我使用 helm 创建了 nginx pod,并在 ingress.yaml 中包含了 nginx 配置。 The error is raised when I use nginx, I've I run the docker image locally, it works fine.当我使用 nginx 时出现错误,我在本地运行 docker 图像,它工作正常。 Also, if I made the services' types as a Load balancer, the applications work fine.此外,如果我将服务的类型设置为负载平衡器,则应用程序可以正常工作。

![here is the error in the webpage ] 1 ![这是网页中的错误] 1

在此处输入图像描述

here is the GKE services:这是 GKE 服务:

在此处输入图像描述

ingress.yaml:入口.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  creationTimestamp: 2019-07-08T08:35:52Z
  generation: 1
  name: www
  namespace: default
  resourceVersion: "171166"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/www
  uid: 659594d6-a15b-11e9-a671-42010a980160
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
  rules:
  - host: twitter.domain.com
    http:
      paths:
      - backend:
          serviceName: twitter
          servicePort: 6020
  - host: events.omarjs.com
    http:
      paths:
      - backend:
          serviceName: events
          servicePort: 6010
  - http:
      paths:
      - backend:
          serviceName: twitter
          servicePort: 6020
        path: /twitter
      - backend:
          serviceName: events
          servicePort: 6010
        path: /events
  tls:
  - secretName: omarjs-ssl
status:
  loadBalancer: {}

twitter.yaml: twitter.yaml:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-07-07T20:43:49Z
  labels:
    run: twitter
  name: twitter
  namespace: default
  resourceVersion: "27299"
  selfLink: /api/v1/namespaces/default/services/twitter
  uid: ec8920ca-a0f7-11e9-ac47-42010a98008f
spec:
  clusterIP: 10.7.253.177
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 31066
    port: 6020
    protocol: TCP
    targetPort: 3020
  selector:
    run: twitter
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-07-07T20:43:49Z
  labels:
    run: twitter
  name: twitter
  namespace: default
  resourceVersion: "27299"
  selfLink: /api/v1/namespaces/default/services/twitter
  uid: ec8920ca-a0f7-11e9-ac47-42010a98008f
spec:
  clusterIP: 10.7.253.177
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 31066
    port: 6020
    protocol: TCP
    targetPort: 3020
  selector:
    run: twitter
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

You can probably solve your problem by adding additional ingress rules which will route the requests for static files to proper directories. 您可能可以通过添加其他入口规则来解决您的问题,这些入口规则会将对静态文件的请求路由到正确的目录。 As far as I can see on the attached print screen, your static files are placed in css and js directories respectively, so you need to add 4 additional rules to your ingress.yaml . 据我在所附的打印屏幕上所看到的,您的静态文件分别位于cssjs目录中,因此您需要向ingress.yaml添加4条附加规则。 The final version of this fragment may look like that: 该片段的最终版本可能如下所示:

  - http:
      paths:
      - backend:
          serviceName: twitter
          servicePort: 6020
        path: /twitter
      - backend:
          serviceName: twitter
          servicePort: 6020
        path: /css
      - backend:
          serviceName: twitter
          servicePort: 6020
        path: /js
      - backend:
          serviceName: events
          servicePort: 6010
        path: /events
      - backend:
          serviceName: events
          servicePort: 6010
        path: /css
      - backend:
          serviceName: events
          servicePort: 6010
        path: /js

The issue for me was the same, however I don't even had my.js/.css/etc files in a separete folder, yet I couldn't get it loaded.我的问题是一样的,但是我什至没有在单独的文件夹中有 my.js/.css/etc 文件,但我无法加载它。

After hours of search, it turned out to be an incorrect Exact pathType in the.yaml file:经过几个小时的搜索,原来是.yaml文件中的一个不正确Exact pathType
I have changed it to Prefix , and it solved the issue.我已将其更改为Prefix ,它解决了问题。

My sample Ingress yaml, with the correct pathType:我的示例 Ingress yaml,具有正确的路径类型:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-webapp-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
  namespace: default
spec:
  rules:
    - host: my.webapp.com
      http:
        paths:
          - path: /
            pathType: Prefix  #<----- the correct pathType for me
            backend:
              service:
                name: my-webapp-service
                port:
                  number: 80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ingress-nginx controller 在 Google Kube.netes Engine (GKE) 上向外部公开 UDP 服务? - How do I expose a UDP service externally on Google Kubernetes Engine (GKE) using the ingress-nginx controller? 如何在使用 ingress-nginx 时在 Google Kube.netes 引擎上启用 SSL? - How To Enable SSL on Google Kubernetes Engine while using ingress-nginx? AKS Ingress-Nginx ingress controller 主机路由失败 - AKS Ingress-Nginx ingress controller failing to route by host Kubernetes HTTPS Google 容器引擎中的入口 - Kubernetes HTTPS Ingress in Google Container Engine 如何在 ingress-nginx GKE 上使用内部 controller - How to use internal controller on ingress-nginx GKE 我如何在 nginx 入口 controller 上使用云装甲? - How can i use cloud armor on nginx ingress controller? Ingress nginx路径路由 - Ingress nginx path routing Nginx 入口未重定向到 https - Nginx ingress not redirecting to https 对于 nginx-ingress controller,当设置 controller loadblancer 白名单 IP 范围(不是入口白名单范围)时,这在 GCP 控制台上可见吗? - For nginx-ingress controller, when setting controller loadblancer whitelist IP ranges (NOT ingress whitelist ranges), is this visible on GCP console? Nginx 400,一个简单的 http 响应被发送到带有 nginx-ingress controller 的 HTTPS 端口 - Nginx 400, A plain http response was sent to HTTPS port with nginx-ingress controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM