简体   繁体   English

nginx-ingress 将流量发送到 pod 中的 nginx 但不返回资产

[英]nginx-ingress sending traffic to nginx in pod but not returning assets

Basically, I'm having difficulty getting nginx to serve the npm build from a CRA web app that is at the subpath /new-admin .基本上,我很难让nginx从位于子路径/new-admin的 CRA Web 应用程序提供npm build

I do have in the package.json "homepage": "/new-admin" and <Route basename={process.env.PUBLIC_URL}> in the index.js .我确实在package.json "homepage": "/new-admin"<Route basename={process.env.PUBLIC_URL}>中有index.js Works fine in development, but now that I'm trying to make a staging/production copy it isn't and I'm pretty sure it is due to the ./nginx/default.conf .在开发中运行良好,但现在我正在尝试制作临时/生产副本,但它不是,我很确定这是由于./nginx/default.conf Of the various configurations I've tried, none have worked and I either get:在我尝试过的各种配置中,没有一个有效,我要么得到:

  • 500 Internal Server Error
  • <Uncaught SyntaxError: Unexpected token '<'

This is what I have:这就是我所拥有的:

# ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/proxy-body-size: "0"
    nginx.org/client-max-body-size: "500m"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^(/admin)$ $1/ permanent;
  name: ingress-service-dev-admin
  namespace: default
spec:
  rules:
    - http:
        paths:
          - path: /admin/?(.*)
            backend:
              serviceName: admin-old-cluster-ip-service-dev
              servicePort: 4000  
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^(/new-admin)$ $1/ permanent;
  name: ingress-service-dev
  namespace: default
spec:
  rules:
    - http:
        paths:
          - path: /new-admin/
            backend:
              serviceName: admin-new-cluster-ip-service-dev
              servicePort: 4001
          - path: /api/
            backend:
              serviceName: api-cluster-ip-service-dev
              servicePort: 5000
          - path: /
            backend:
              serviceName: client-cluster-ip-service-dev
              servicePort: 3000
# Dockerfile for /new-admin/

FROM node:13-alpine as builder
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx
EXPOSE 4001
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html

This version of the default.conf results in a 500 Internal Server Error :此版本的default.conf导致500 Internal Server Error

# ./nginx/default.conf

server {
  listen 4001;
  root /usr/share/nginx/html;
  index index.html index.htm;

  location /new-admin {
    try_files $uri $uri/ /new-admin/index.html;
  }
}

This is the associated nginx log:这是关联的nginx日志:

[admin-new] 2020/10/05 20:31:49 [error] 28#28: *9 rewrite or internal redirection cycle while internally redirecting to "/new-admin/index.html", client: 172.17.0.4, server: , request: "GET /new-admin/ HTTP/1.1", host: "192.168.64.5"
[admin-new] 172.17.0.4 - - [05/Oct/2020:20:31:49 +0000] "GET /new-admin/ HTTP/1.1" 500 579 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "192.168.64.1"

This version results in <Uncaught SyntaxError: Unexpected token '<' :此版本导致<Uncaught SyntaxError: Unexpected token '<'

# ./nginx/default.conf

server {
  listen 4001;
  root /usr/share/nginx/html;
  index index.html index.htm;

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

This seems closest to being correct given it serves the index.html , just not the CSS and JS assets.鉴于它提供index.html ,而不是 CSS 和 JS 资产,这似乎最接近正确。 It does show in the index.html , which should be correct:它确实显示在index.html ,这应该是正确的:

<script src="/new-admin/static/js/2.c6b4376b.chunk.js"></script>
<script src="/new-admin/static/js/main.c24347bf.chunk.js"></script>

I've also noticed when it starts the Pod it says, despite the fact it is building the images from scratch.我还注意到当它启动 Pod 时,它说,尽管它是从头开始构建图像。

[admin-new] /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
[admin-new] /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[admin-new] /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
[admin-new] 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
[admin-new] 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packaged version
[admin-new] /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
[admin-new] /docker-entrypoint.sh: Configuration complete; ready for start up

Actually, came across this answer which was pretty much the exact issue I was experiencing:实际上,遇到了这个答案,这几乎是我遇到的确切问题:

React & nginx routing to subdirectory React & nginx 路由到子目录

So my final ./nginx/default.conf looked like this:所以我最终的./nginx/default.conf看起来像这样:

server {
  listen 4001;
  root /usr/share/nginx/html;
  index index.html index.htm;

  location = /new-admin {
    try_files $uri $uri/ /index.html =404;
  }

  location ~ ^/new-admin(.*) {
    try_files $1 $1/ /index.html =404;
  }
}

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

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