简体   繁体   English

入口 nginx “502 bad gateway”后无法让 nginx 为 PHP 应用程序提供服务

[英]Can't get nginx to serve PHP application after ingress nginx “502 bad gateway”

I have this old vanilla PHP application I'm playing with, trying to Dockerize it and then put it into a Kubernetes cluster.我有这个旧的香草 PHP 应用程序我正在玩,试图 Dockerize 它然后将它放入 Kubernetes 集群。

I upgraded the app to php7.3-fpm and I'm trying to add nginx to the same image.我将应用程序升级到php7.3-fpm ,并尝试将nginx添加到同一图像中。 Something like how the php7.3-apache is, but using nginx and php-fpm .类似于php7.3-apache的方式,但使用nginxphp-fpm

I came across this answer which offers a solution for building the image.我遇到了这个答案,它提供了构建图像的解决方案。 I've changed it to fit my needs, but I'm having issues getting it to actually serve the application:我已经对其进行了更改以满足我的需求,但是在让它实际为应用程序服务时遇到了问题:

  • It just returns "502 Bad Gateway nginx/1.14.2" if I navigate to /admin/如果我导航到/admin/ ,它只会返回"502 Bad Gateway nginx/1.14.2"
  • It just returns "Welcome to nginx!"它只是返回"Welcome to nginx!" if I navigate to /admin如果我导航到/admin

Seems like ingress-nginx and nginx are at least communicating.似乎ingress-nginxnginx至少在通信。 Just the index.php isn't being served.只是index.php没有被提供。

Not quite sure where I'm going wrong.不太确定我哪里出错了。

Here is my configuration:这是我的配置:

# 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: /$1
    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;
    }
}

What have I screwed up here?我在这里搞砸了什么?

Ok, figured it out...好吧,想通了……

The following I had in the default.conf was wrong:我在default.conf中的以下内容是错误的:

fastcgi_pass 127.0.0.1:4000;

It should have stayed this (which was in the answer I was copying)...它应该保持这个(这是我正在复制的答案)......

fastcgi_pass 127.0.0.1:9000;

Naively didn't know that this was the default for php-fpm .天真地不知道这是php-fpm的默认设置。

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

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