简体   繁体   English

浏览器上的 Nginx Ingress 404

[英]Nginx Ingress 404 on browser

Created an Ingress for my react application hosted in nginx docker container.为我在 nginx docker 容器中托管的 React 应用程序创建了一个入口。

Ingress config入口配置

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  labels:
    helm.sh/chart: home-service
    app.kubernetes.io/name: home-service
    app.kubernetes.io/instance: xrayed-vulture
    app.kubernetes.io/version: "1.0.0"
    app.kubernetes.io/managed-by: Tiller
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: "home-service.net"
      http:
        paths:
          - path: "/pmr"
            backend:
              serviceName: my-service
              servicePort: 8089

Service服务

apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    helm.sh/chart: home-service
    app.kubernetes.io/name: home-service
    app.kubernetes.io/instance: xrayed-vulture
    app.kubernetes.io/version: "1.0.0"
    app.kubernetes.io/managed-by: Tiller
spec:
  type: ClusterIP
  ports:
    - port: 8089
      targetPort: 8089
      protocol: TCP
      name: http
    - port: 443
      targetPort: https
      protocol: TCP
      name: https
  selector:
    app.kubernetes.io/name: pmr-ui-app

Nginx /etc/nginx/conf.d/default.conf config in my react-app which is hosted in nginx:stable-alpine container.我的 react-app 中的 Nginx /etc/nginx/conf.d/default.conf配置,它托管在 nginx:stable-alpine 容器中。

server {
        listen 8089;
        listen 443 default ssl;
        server_name localhost;

        root   /usr/share/nginx/html;

        ssl_certificate /etc/nginx/certs/nginx.crt;
        ssl_certificate_key /etc/nginx/certs/nginx.key;
        ssl_protocols        SSLv3 TLSv1;
        ssl_ciphers          HIGH:!aNULL:!MD5;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri /index.html;
        }

        error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        access_log /var/log/nginx/access.log main;
        error_log   /var/log/nginx/error.log  warn;
}

Making a curl call to the blow address, the ingress works fine and return the right page对吹地址进行 curl 调用,入口工作正常并返回正确的页面

curl -v http://home-service.net/pmr


message from my nginx controller container
10.251.128.1 - - [02/Sep/2020:16:33:30 +0000] "GET /pmr HTTP/1.1" 200 3009 "-" "curl/7.64.0" 103 0.002 [nc-my-service-8089] [] 10.x.x.26:8089 3009 0.000 200 e2407a01ffcf7607b958522574979b29

message from the react app container itself
10.x.x.27 - - [02/Sep/2020:16:33:30 +0000] "GET /pmr HTTP/1.1" 200 3009 "-" "curl/7.64.0" "10.251.128.1"

But visiting the browser, i see 404 for loading some .js, and css files但是访问浏览器,我看到 404 用于加载一些 .js 和 css 文件

Chrome with http://home-service.net/pmr带有http://home-service.net/pmr Chrome

Nginx controller logs
10.x.x.198 - - [02/Sep/2020:14:45:36 +0000] "GET /pmr HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 611 0.001 [nc-my-service-8089] [] 10.x.x.26:8089 0 0.000 304 f994cdb21f962002e73ce6d967f82550

10.x.x.200 - - [02/Sep/2020:14:51:11 +0000] "GET /wf-bridge/wf-bridge/wf-bridge.esm.js HTTP/1.1" 404 21 "http://home-service.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 495 0.001 [upstream-default-backend] [] 10.x.x.18:8080 21 0.000 404 0ba3519a8f55673cdcbb391d6102609a
10.x.x.1 - - [02/Sep/2020:14:51:11 +0000] "GET /static/js/2.ae30464f.chunk.js HTTP/1.1" 404 21 "http://home-service.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 435 0.001 [upstream-default-backend] [] 10.x.x.18:8080 21 0.000 404 b01b7b33a961df5be413f10ec14909c1
10.x.x.198 - - [02/Sep/2020:14:51:11 +0000] "GET /static/js/main.d76be073.chunk.js HTTP/1.1" 404 21 "http://home-service.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 438 0.000 [upstream-default-backend] [] 10.x.x.18:8080 21 0.000 404 c333c6dca261a75133944c6e308874af
10.210.x.200 - - [02/Sep/2020:14:51:11 +0000] "GET /static/css/main.9d650a52.chunk.css HTTP/1.1" 404 21 "http://home-service.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 455 0.000 [upstream-default-backend] [] 10.x.x.18:8080 21 0.000 404 0d8a5a6aa7d549294fcb1c147dd01294
10.x.x.198 - - [02/Sep/2020:14:51:11 +0000] "GET /static/js/2.ae30464f.chunk.js HTTP/1.1" 404 21 "http://kda9dc8056pmr01.aks.azure.ubsdev.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 435 0.000 [upstream-default-backend] [] 10.x.x.18:8080 21 0.000 404 1d8ee795cfa4e52225cf51e9fc1989d6
10.x.x.198 - - [02/Sep/2020:14:51:11 +0000] "GET /static/js/main.d76be073.chunk.js HTTP/1.1" 404 21 "http://home-service.net/pmr" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" 438 0.001 [upstream-default-backend] [] 10.x.x.18:8080 21 0.004 404 a0952e173bfb58f418f5d600e377f18c

React Nginx container will recieve the request with 200 like this
10.x.x.25 - - [02/Sep/2020:14:25:27 +0000] "GET / HTTP/1.1" 200 3009 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "10.210.62.200"

Why the requests are all not handled by the my-service alone but css and .js files are handled by the upstream-default-backend and resulting in 404 ?为什么请求不是全部由my-service单独处理,而是由upstream-default-backend处理 css 和 .js 文件并导致 404 ?

Nginx ingress controller version: nginx-ingress-controller:0.26.1 Nginx 入口控制器版本: nginx-ingress-controller:0.26.1

Your resources are rendered with the root path /static .您的资源使用根路径/static呈现。 That's a separate HTTP request and will get default backend since it does not satisfy your /pmr path rule.这是一个单独的 HTTP 请求,将获得默认后端,因为它不满足您的/pmr路径规则。 You should use relative paths inside the web app for the reverse proxy to work correctly.您应该在 Web 应用程序内使用相对路径,以便反向代理正常工作。 For example drop the / root and just use static/css/main.css .例如删除/ root 并只使用static/css/main.css Or use hostname rule instead of path rules.或者使用主机名规则而不是路径规则。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM