gpt4 book ai didi

docker - nginx入口将流量发送到pod中的nginx,但不返回 Assets

转载 作者:行者123 更新时间:2023-12-02 12:09:41 25 4
gpt4 key购买 nike

基本上,我很难从位于子路径nginx的CRA Web应用程序获取npm build来提供/new-admin
我确实在package.json中有"homepage": "/new-admin" <Route basename={process.env.PUBLIC_URL}>index.js。在开发中工作正常,但是现在我尝试制作一个暂存/生产副本,但不是很确定,我可以肯定这是由于./nginx/default.conf造成的。在我尝试过的各种配置中,没有一个起作用,我要么得到:

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

  • 这就是我所拥有的:
    # 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
    此版本的 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;
    }
    }
    这是相关的 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"
    这个版本产生 <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;
    }
    }
    考虑到它提供了 index.html,而不是CSS和JS Assets ,这似乎最接近正确。它确实在 index.html中显示,应该正确:
    <script src="/new-admin/static/js/2.c6b4376b.chunk.js"></script>
    <script src="/new-admin/static/js/main.c24347bf.chunk.js"></script>
    我也注意到它在启动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

    最佳答案

    实际上,遇到了这个答案,这几乎是我遇到的确切问题:
    React & nginx routing to subdirectory
    所以我最后的./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;
    }
    }

    关于docker - nginx入口将流量发送到pod中的nginx,但不返回 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64216157/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com