无法使Apache重写规则在Docker容器中工作

f8rj6qna  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(151)

我已经按照这个论坛上的一些说明、教程和问题进行了操作,但是我仍然无法实现。我已经使用PHP Apache Docker容器设置了一个REST API,并且需要在apache配置中创建一个重写规则来将API请求重新路由到index.php(REST控制器)。目前,根据下面所写的内容,我得到了一个404:
本地机器上的文件结构(列出了除php源代码之外的所有内容;此处不需要):

php
  conf.d
    - error_reporting.ini
    - xdebug.ini
  Dockerfile
  index.php

apache
  - apache2.conf

docker-compose.yml

Dockerfile的内容为:

FROM php:8.1-apache

WORKDIR /var/www/html/

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && a2enmod rewrite

COPY . .

EXPOSE 80

docker-compose.yml的内容为:

services:

  php:
    build: ./php
    depends_on:
      - db
    container_name: php-apache
    ports:
      - 80:80
    volumes:
      - ./php:/var/www/html
      - ./php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      - ./apache/apache2.conf:/etc/apache2/apache2.conf
    environment:
      MARIADB_HOST: localhost
      MARIADB_USER: root
      MARIADB_PASSWORD: top_very_secret
      MARIADB_DB: apidb

  adminer:
    image: adminer
    depends_on:
      - db
    restart: always
    ports:
      - 8080:8080

  db:
    image: mariadb
    container_name: db
    volumes:
      - maria-db-storage:/var/lib/mysql
    environment:
      MARIADB_ROOT_PASSWORD: top_very_secret
      MARIADB_DATABASE: apidb

volumes:
  maria-db-storage:

关于apache2.conf的内容;为了在本地计算机上创建它,我执行了以下操作:
1.使用docker exec -t -i <container_name> /bin/bash进入容器的虚拟文件系统。
1.徘徊到/etc/apache2
1.已通过cat apache2.conf打印文件内容
1.将内容复制粘贴到本地/apache/apache2.conf文件中
1.在该本地文件的末尾添加了以下指令行:

# Custom directives start here

# Set Server's name
ServerName 'localhost'

# Rewrite for routing of all requests through REST controller
RewriteEngine On

# If requested resource is index.php, do nothing
RewriteRule ^index\.php$ - [L]

# If requested resource is a file or directory that does not exist, reroute to REST 
# controller index.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule . /index.php [L]

在构建映像并运行容器之后,我再次通过CLI检查了容器的虚拟文件系统。/etc/apache2/apache2.conf成功地保存了我的本地文件的内容,我还在容器中执行了一个apachectl -M,可以看到rewrite_module (shared)正在打印。
同样,我只是得到一个404;例如,如果我搜索http://localhost/xyz。如果我不忽略端口(并搜索http://localhost:80/xyz),则情况相同。搜索http://localhosthttp://localhost:80都有效;因此看起来我的重写规则根本没有被应用。
当在Docker容器中运行apachectl configtest时,我也会得到Syntax OK
只是猜测;这是否与xdebugs从容器的端口9003传出的通信有关?
我错过了什么?
显然,serverfault是这些问题的预期位置;所以我把它贴在那里(不知道如何迁移):https://serverfault.com/questions/1115336/cant-get-apache-rewrite-to-work-on-docker-php-apache-container

bhmjp9jg

bhmjp9jg1#

实际上我已经弄明白了。问题似乎是,当使用docker时,你必须在你指定的端口的虚拟主机上下文中指定重写规则。至少我已经做了上面写的,加上下面的步骤:
1.在容器中,我已迁移(在CLI中)到文件夹etc/apache2/sites-enabled
1.文件000-default.conf保存了apache单元上当前启用的站点的默认配置。就这个应用程序而言,修改默认配置是很好的,因为容器将专门用于这个API。至少我是这么想的。
1.所以我又做了同样的;通过cat 000-default.conf复制文件内容,因为它们是随初始化的Docker容器一起提供的。
1.复制并粘贴本地apache/sites-enabled/000-default.conf文件(新创建的)中的内容
1.现在,您可以使用以前位于本地apache2.conf文件中的以下部分(略有改进):

# Rewrite for routing of all requests through REST controller
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ /index.php [L]

并粘贴到本地的000-default.conf文件中;然后从apache/apache2.conf文件中删除重写规则,但将其留给添加的ServerName指令。
1.然后将路径Map添加到PHP容器的卷中,以将自定义虚拟主机默认配置文件加载到容器中,这样就可以开始了;因此您docker-compose.yml变为:

services:

  php:
    ....
    volumes:
      - ./php:/var/www/html
      - ./php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      - ./apache/apache2.conf:/etc/apache2/apache2.conf
      - ./apache/sites-enabled/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
     ....

重写功能现已完全启用!

相关问题