Apache 忽略来自 Lumen 的标头和状态代码

Posted

技术标签:

【中文标题】Apache 忽略来自 Lumen 的标头和状态代码【英文标题】:Apache ignore headers and status codes from Lumen 【发布时间】:2021-03-05 15:24:59 【问题描述】:

我有基于 Docker 和微服务的应用程序。角,mysql,流明 + Apache。不幸的是,在 Docker 上,我的 API 没有发送任何标头或状态代码。任何响应都是 200。我尝试将 HTTP 服务器更改为 nginx,但一无所获。我不知道如何解决这个问题。

Apache 配置

NameVirtualHost *:8080
Listen 8080

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>
ServerName localhost:8080

<VirtualHost *:8080>
  DocumentRoot /var/www/html/public
  <Directory /var/www/html/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

Dockerfile

FROM php:7.4.1-apache

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    zlib1g-dev \
    libonig-dev \
    libxml2-dev \
    curl \
    libzip-dev \
    zip \
    unzip

#COPY .docker/apache.conf /etc/apache2/sites-available/000-default.conf

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-configure gd
RUN docker-php-ext-configure zip && docker-php-ext-install zip
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN chown -R www-data:www-data /var/www/html
RUN a2enmod rewrite headers cache


# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www/html

USER $user

EXPOSE 8080

docker-compose.yml

version: "3.7"
services:
  backend:
    build:
      args:
        user: sammy
        uid: 1000
      context: ./backend
      dockerfile: .docker/Dockerfile
    image: backend
    ports: 
    - 8000:8080
    container_name: backend
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./backend:/var/www/html
      - ./backend/.docker/apache.conf:/etc/apache2/sites-available/000-default.conf
      - ./backend/.docker/ports.conf:/etc/apache2/ports.conf
      
    networks:
      - jpk
  db:
    image: mysql:5.7
    container_name: jpk-db
    restart: always
    ports: 
      - "3306:3306"
    environment:
      MYSQL_DATABASE: 'jpk'
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_ROOT_PASSWORD: ""
    volumes:
      - ./db:/var/lib/mysql
    networks:
      - jpk
  frontend:
    build:
      args:
        env: prod
      context: ./frontend
      dockerfile: Dockerfile
    image: frontend
    container_name: frontend
    ports: 
      - "80:80"
    environment:
      NODE_ENV: production
networks:
  jpk:
    driver: bridge

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
      Options -MultiViews
    </IfModule>

    <IfModule mod_headers.c>
        SetEnvIf Origin "http(s)?://([^.]+\.)?(mysite.com|mysite.local:4200|localhost:4200)$" AccessControlAllowOrigin=$0$1
        Header add Access-Control-Allow-Origin %AccessControlAllowOrigine env=AccessControlAllowOrigin
        Header set Access-Control-Allow-Credentials true
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %REQUEST_FILENAME !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %REQUEST_FILENAME !-d
    RewriteCond %REQUEST_FILENAME !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %HTTP:Authorization .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%HTTP:Authorization]
</IfModule>

【问题讨论】:

我的第一个猜测是,在您的 php 文件中,您有一些以 ?&gt; 结尾的文件以及一些空格和/或行。这些空行强制在 Lumen 有机会设置其他标头之前发送标头 @Nathanael 我没有这样的 php 文件 @Zwolin PHP 应该负责设置状态头,你是如何设置 Lumen 中的状态码的?你确定这没有被任何中间件覆盖吗? @whyguy 例如:return response()->json(['error' => 'Permission denied'], 401);。我敢肯定,没有 docker on artisan serve headers 发送没有任何问题。 【参考方案1】:

好吧,我找到了问题的答案。我需要添加一行

RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

在我的 Dockerfile 中

【讨论】:

以上是关于Apache 忽略来自 Lumen 的标头和状态代码的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的服务器会忽略来自 ajax 请求的身份验证标头?

AngularJS 忽略 HTTP 标头中的 Set-Cookie

使用 Laravel/Lumen 5.2 和 apache 2.4

如何使 HttpClient 忽略 Content-Length 标头

PHP 标头未使用 litespeed 设置(但适用于 apache)

来自 QAbstractListmodel headerData() 的 QML Listview 标头