在进入 nginx“502 错误网关”后,无法让 nginx 为 PHP 应用程序提供服务

Can't get nginx to serve PHP application after ingress nginx "502 bad gateway"

我有一个我正在玩的旧香草 PHP 应用程序,试图将其 Dockerize,然后将其放入 Kubernetes 集群。

我已将应用程序升级到 php7.3-fpm,并且正在尝试将 nginx 添加到同一图像。类似于 php7.3-apache,但使用 nginxphp-fpm

我遇到了 this answer,它提供了构建图像的解决方案。我已更改它以满足我的需要,但我在实际为应用程序提供服务时遇到了问题:

似乎 ingress-nginxnginx 至少在沟通。只是 index.php 没有被提供。

不太确定哪里出错了。

这是我的配置:

# project structure

root/
  /conf
    app.conf
    default.conf
    entrypoint.sh
    file_size.ini
  /src
    index.php
    all other.php 
  Dockerfile.dev
  Dockerfile
# ingress-nginx.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "0"
    nginx.org/client-max-body-size: "500m"
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-service-dev
  namespace: default
spec:
  rules:
    - http:
        paths:
          - path: /admin/?(.*)
            backend:
              serviceName: admin-cluster-ip-service-dev
              servicePort: 4000

# admin.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: admin-deployment-dev
spec:
  replicas: 1
  selector:
    matchLabels:
      component: admin
  template:
    metadata:
      labels:
        component: admin
    spec:
      containers:
        - name: admin
          image: testappacr.azurecr.io/test-app-admin
          ports:
            - containerPort: 4000
---
apiVersion: v1
kind: Service
metadata:
  name: admin-cluster-ip-service-dev
spec:
  type: ClusterIP
  selector:
    component: admin
  ports:
    - port: 4000
      targetPort: 4000
# Dockerfile
FROM php:7.3-fpm

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"

RUN apt-get update \
    && apt-get install -y nginx \ 
    && apt-get install -y libpq-dev zlib1g-dev libzip-dev \
    && docker-php-ext-install pgsql zip mbstring opcache 
RUN { \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/php-opocache-cfg.ini

COPY . /usr/share/nginx/html
COPY ./conf/default.conf /etc/nginx/conf.d/default.conf
COPY ./conf/entrypoint.sh /etc/entrypoint.sh
# COPY --chown=www-data:www-data . /app/src

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN mv "/usr/share/nginx/html/conf/file_size.ini" "$PHP_INI_DIR/conf.d/"

WORKDIR /usr/share/nginx/html/src

EXPOSE 4000

ENTRYPOINT ["sh", "/etc/entrypoint.sh"]
# default.conf

server {
    listen 4000;
    root   /usr/share/nginx/html/src;

    include /etc/nginx/default.d/*.conf;

    index app.php index.php index.html index.htm;

    client_max_body_size 500m;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ [^/]\.php(/|$) {
      fastcgi_split_path_info ^(.+?\.php)(/.*)$;
      # Mitigate https://httpoxy.org/ vulnerabilities
      fastcgi_param HTTP_PROXY "";
      fastcgi_pass 127.0.0.1:4000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
}

我搞砸了什么?

好的,明白了...

我在 default.conf 中的以下内容是错误的:

fastcgi_pass 127.0.0.1:4000;

它应该保留这个(我正在复制的答案中)...

fastcgi_pass 127.0.0.1:9000;

天真地不知道这是 php-fpm 的默认设置。