Nginx入口控制器忽略CSS和JS文件-Google kuberenetes引擎

时间:2019-07-08 10:13:04

标签: kubernetes google-kubernetes-engine kubernetes-helm nginx-ingress

我创建了一个链接到两个服务的nginx入口控制器。该网站运行正常,但js和CSS文件未在HTML页面(404)错误中加载。我使用头盔创建了nginx pod,并在ingress.yaml中包含了nginx配置。当我使用Nginx时会引发错误,我已经在本地运行docker镜像,它工作正常。另外,如果我将服务的类型设为负载均衡器,则应用程序可以正常工作。

![here is the error in the webpage ] 1

enter image description here

这是GKE服务:

enter image description here

ingress.yaml:

$ luajit
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> a1 = require "socket"
error loading module 'socket.core' from file '/usr/lib/lua/5.3/socket/core.so':
    /usr/lib/lua/5.3/socket/core.so: undefined symbol: luaL_setfuncs
stack traceback:
    [C]: at 0x5617fff23fb0
    [C]: in function 'require'
    /usr/share/lua/5.3/socket.lua:12: in main chunk
    [C]: in function 'require'
    stdin:1: in main chunk
    [C]: at 0x5617ffed4c00

twitter.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: {}
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: {}

1 个答案:

答案 0 :(得分:0)

您可能可以通过添加其他入口规则来解决您的问题,这些入口规则会将对静态文件的请求路由到正确的目录。据我在所附的打印屏幕上所看到的,您的静态文件分别放置在cssjs目录中,因此您需要向ingress.yaml添加4条附加规则。该片段的最终版本可能如下所示:

  - 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