Nginx在php fpm上作为后端的反向代理始终返回状态代码200

Nginx在php fpm上作为后端的反向代理始终返回状态代码200,php,nginx,php-7,alpine,nginx-reverse-proxy,Php,Nginx,Php 7,Alpine,Nginx Reverse Proxy,每当php服务器返回带有body的响应时,即使应用程序没有返回状态代码200,nginx也会使用状态代码200和应用程序的正确body进行响应。如果应用程序未设置和响应主体,则nginx返回与应用程序返回的状态代码相同的状态代码 我正在使用这个Dockerfile FROM php:7.2-fpm-alpine3.9 RUN apk update && apk add nginx WORKDIR /var/www RUN mkdir -p /run/nginx # ngi

每当php服务器返回带有body的响应时,即使应用程序没有返回状态代码200,nginx也会使用状态代码200和应用程序的正确body进行响应。如果应用程序未设置和响应主体,则nginx返回与应用程序返回的状态代码相同的状态代码

我正在使用这个Dockerfile

FROM php:7.2-fpm-alpine3.9

RUN apk update && apk add nginx

WORKDIR /var/www

RUN mkdir -p /run/nginx

# nginx conf
RUN echo '\
server {                                                                \
    listen 80 default_server;                                           \
    listen [::]:80 default_server;                                      \
                                                                        \
    location / {                                                        \
      # Remove trailing slashes before checking for matching locations  \
      rewrite ^(.+)\/$ $1 permanent;                                    \
                                                                        \
      dav_methods PUT DELETE MKCOL COPY MOVE;                           \
      dav_access all:r;                                                 \
      index index.php index.html index.htm;                             \
      try_files $uri $uri/ /index.php?$args;                            \
    }                                                                   \
                                                                        \
    location ~ index\.php$ {                                            \
      include /etc/nginx/fastcgi_params;                                \
      fastcgi_split_path_info ^(.+\.php)(/.+)$;                         \
      fastcgi_pass 127.0.0.1:9000;                                      \
      fastcgi_index index.php;                                          \
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;  \
    }                                                                   \
}                                                                       \
' > /etc/nginx/conf.d/default.conf;

# php file
RUN echo '\
<?php                             \
echo "Hello World";               \
http_response_code(400);          \
?>                                \
'> /var/www/html/index.php

ENTRYPOINT [ "ash" ]
注意:如果删除回显“Hello World”nginx从index.php返回正确的状态代码

编辑:

但这是意料之中的事

FROM debian:10-slim

ARG VERSION=7.2

# replace shell with bash so we can source files
SHELL ["/bin/bash", "-c"]

ENV DEBIAN_FRONTEND=noninteractive NODE_VERSION=12

ADD https://packages.sury.org/php/apt.gpg /opt/apt-php.gpg

RUN set -ex; \
    apt-get update -y; \
    apt-get install git gnupg -y; \
    apt-key add - < /opt/apt-php.gpg; \
    apt-get install -y apt-transport-https lsb-release ca-certificates; \
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list; \
    apt-get update -y; \
    apt-get upgrade -y; \
    apt-get install -y \
    php${VERSION} \
    php${VERSION}-fpm \
    nginx \
    curl;

# nginx conf
RUN echo '\
server {                                                                \
    listen 80 default_server;                                           \
    listen [::]:80 default_server;                                      \
                                                                        \
    location / {                                                        \
      # Remove trailing slashes before checking for matching locations  \
      rewrite ^(.+)\/$ $1 permanent;                                    \
                                                                        \
      dav_methods PUT DELETE MKCOL COPY MOVE;                           \
      dav_access all:r;                                                 \
      index index.php index.html index.htm;                             \
      try_files $uri $uri/ /index.php?$args;                            \
    }                                                                   \
                                                                        \
    location ~ index\.php$ {                                            \
      include /etc/nginx/fastcgi_params;                                \
      fastcgi_split_path_info ^(.+\.php)(/.+)$;                         \
      fastcgi_pass 127.0.0.1:9000;                                      \
      fastcgi_index index.php;                                          \
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;  \
    }                                                                   \
}                                                                       \
' > /etc/nginx/sites-available/default;

# php file
RUN echo '\
<?php                             \
echo "Hello World";               \
http_response_code(400);          \
?>                                \
'> /var/www/html/index.php

RUN sed -i "s/listen\ =\ \/run\/php\/php7.2-fpm.sock/listen\ =\ 127.0.0.1:9000/"  /etc/php/7.2/fpm/pool.d/www.conf

RUN mkdir -p /run/php

RUN ln -s /usr/sbin/php-fpm7.2 /usr/sbin/php-fpm

ENTRYPOINT [ "bash" ]
来自debian的
:10
ARG版本=7.2
#用bash替换shell,这样我们就可以创建源文件了
SHELL[“/bin/bash”,“-c”]
ENV DEBIAN_前端=非交互节点_版本=12
添加https://packages.sury.org/php/apt.gpg /opt/apt-php.gpg
运行set-ex\
apt-get-update-y\
apt-get-install-git-gnupg-y\
apt键添加-/etc/apt/sources.list.d/php.list\
apt-get-update-y\
易于升级-y\
apt-get-install-y\
php${VERSION}\
php${VERSION}-fpm\
nginx\
卷曲
#nginx形态
运行echo'\
服务器{\
监听80个默认_服务器\
侦听[:]:80默认_服务器\
\
位置/{\
#在检查匹配位置之前,请删除尾部斜杠\
重写^(+)\/$$1永久\
\
dav_方法放入删除MKCOL复制移动\
dav_访问全部:r\
index.php index.html index.htm\
try_files$uri$uri//index.php?$args\
}                                                                   \
\
位置~index\.php${\
包括/etc/nginx/fastcgi_参数\
fastcgi\u split\u path\u info^(.+\.php)(/.+)$\
fastcgi_pass 127.0.0.1:9000\
fastcgi_index.php\
fastcgi_参数SCRIPT_FILENAME/var/www/html$fastcgi_SCRIPT_name\
}                                                                   \
}                                                                       \
'>/etc/nginx/sites available/default;
#php文件
运行echo'\
\
'>/var/www/html/index.php
运行sed-i“s/listen\=\/RUN\/php\/php7.2-fpm.sock/listen\=\127.0.0.1:9000/”/etc/php/7.2/fpm/pool.d/www.conf
运行mkdir-p/RUN/php
运行ln-s/usr/sbin/php-fpm7.2/usr/sbin/php-fpm
入口点[“bash”]

这是一种预期的行为,从这里可以看到-

具体地

请记住,在发送任何实际输出之前,必须通过>普通HTML标记、文件中的空行或PHP调用header()。这是一个非常常见的错误 通过include或require函数或其他文件访问来读取代码 函数,并在输入header()之前输出空格或空行 打电话。使用单个PHP/HTML文件时也存在同样的问题


通过在php ini设置中将输出缓冲设置设置为On,或使用
php-S localhost:5000-d输出缓冲=4098等选项运行php cli,可以避免这种情况。这是一种预期行为,如图所示-

具体地

请记住,在发送任何实际输出之前,必须通过>普通HTML标记、文件中的空行或PHP调用header()。这是一个非常常见的错误 通过include或require函数或其他文件访问来读取代码 函数,并在输入header()之前输出空格或空行 打电话。使用单个PHP/HTML文件时也存在同样的问题


通过在PHPINI设置中将输出缓冲设置设置为On,或者使用
php-S localhost:5000-d输出缓冲=4098等选项运行PHPCLI,可以避免这种情况的发生在设置头信息之前,实际上会发送没有头和状态代码的响应,因此nginx将返回代码200。但有没有办法避免这种情况?是的,有一种方法,使用和感谢。这确实奏效了。但我仍然不明白为什么基于alpine的映像(在问题中提到)没有遵守ini文件中的输出缓冲=4098设置。我知道在设置头信息之前,echo(ing)会发送没有头和状态代码的响应,因此nginx将返回代码200。但有没有办法避免这种情况?是的,有一种方法,使用和感谢。这确实奏效了。但我仍然感到困惑,为什么基于阿尔卑斯山的图像(在问题中提到)不尊重ini文件中的输出缓冲=4098设置。
FROM debian:10-slim

ARG VERSION=7.2

# replace shell with bash so we can source files
SHELL ["/bin/bash", "-c"]

ENV DEBIAN_FRONTEND=noninteractive NODE_VERSION=12

ADD https://packages.sury.org/php/apt.gpg /opt/apt-php.gpg

RUN set -ex; \
    apt-get update -y; \
    apt-get install git gnupg -y; \
    apt-key add - < /opt/apt-php.gpg; \
    apt-get install -y apt-transport-https lsb-release ca-certificates; \
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list; \
    apt-get update -y; \
    apt-get upgrade -y; \
    apt-get install -y \
    php${VERSION} \
    php${VERSION}-fpm \
    nginx \
    curl;

# nginx conf
RUN echo '\
server {                                                                \
    listen 80 default_server;                                           \
    listen [::]:80 default_server;                                      \
                                                                        \
    location / {                                                        \
      # Remove trailing slashes before checking for matching locations  \
      rewrite ^(.+)\/$ $1 permanent;                                    \
                                                                        \
      dav_methods PUT DELETE MKCOL COPY MOVE;                           \
      dav_access all:r;                                                 \
      index index.php index.html index.htm;                             \
      try_files $uri $uri/ /index.php?$args;                            \
    }                                                                   \
                                                                        \
    location ~ index\.php$ {                                            \
      include /etc/nginx/fastcgi_params;                                \
      fastcgi_split_path_info ^(.+\.php)(/.+)$;                         \
      fastcgi_pass 127.0.0.1:9000;                                      \
      fastcgi_index index.php;                                          \
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;  \
    }                                                                   \
}                                                                       \
' > /etc/nginx/sites-available/default;

# php file
RUN echo '\
<?php                             \
echo "Hello World";               \
http_response_code(400);          \
?>                                \
'> /var/www/html/index.php

RUN sed -i "s/listen\ =\ \/run\/php\/php7.2-fpm.sock/listen\ =\ 127.0.0.1:9000/"  /etc/php/7.2/fpm/pool.d/www.conf

RUN mkdir -p /run/php

RUN ln -s /usr/sbin/php-fpm7.2 /usr/sbin/php-fpm

ENTRYPOINT [ "bash" ]