.htaccess Apache Docker容器-命令“RewriteEngine”无效

nc1teljy  于 2022-11-16  发布在  Apache
关注(0)|答案(3)|浏览(150)

我使用了docker compose。但是,当我运行“docker-compose up”时,我遇到了一个错误:访问:命令“重写引擎”无效。
你能告诉我我哪里失败了吗?
项目架构:

project-name /
             / docker-compose.yml
             / Dockerfile
             / apache.conf
             / php.ini
             / src /
                   / index.php
                   / .htaccess

docker-compose.yml:我是一个很好的编辑器。

web:
    build: .
    ports:
        - "80:80"
    volumes:
        - ./src:/var/www/html
        - php.ini:/usr/local/etc/php/conf.d/30-custom.ini
        - apache.conf:/etc/apache2/sites-enabled
    environment:
        - ALLOW_OVERRIDE=true

停靠文件:

FROM php:7.0-apache

RUN a2enmod rewrite

RUN service apache2 restart

ADD ./src /var/www/html

php.ini文件:

display_errors=1
error_reporting=E_ALL

apache.conf(带有我IP地址):

<VirtualHost *:80>
    ServerName xxx.xxx.xx.xxx
    DocumentRoot /var/www/html
</VirtualHost>

我在命令行中键入:

docker@default:/blabla/project-name$ docker-compose up

它返回我:

AH00558: apache2: Could not reliably determine the server's fully 
qualified domain name, using xxx.xx.x.x. Set the 'ServerName' directive 
globally to suppress this message

/var/www/html/.htaccess: Invalid command 'RewriteEngine', 
perhaps misspelled or defined by a module not included in the server
configuration

以及在浏览器中,在我的IP地址(http://xxx.xxx.xx.xxx/)中:

500 Internal servor error

我的.htaccess:

<files .htaccess>
    Require all denied
</files>
Options +FollowSymlinks -Indexes -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$   /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

我在Windows上使用Oracle VM虚拟机。
提前感谢!
编辑:我应该说,如果我删除重写规则,一切工作。

k4emjkb1

k4emjkb11#

这对我很有效:

# Dockerfile
FROM php:5.6-apache

MAINTAINER Raphael Mäder <me@randm.ch>

RUN a2enmod rewrite

ADD . /var/www/html

如果之前已经构建了映像,请不要忘记使用--build运行docker-compose up命令,否则它将运行可能不包含RUN a2enmod rewrite语句的旧映像。

flmtquvp

flmtquvp2#

将以下内容添加到您的Dockerfile:

# Enable mod_rewrite for images with apache
RUN if command -v a2enmod >/dev/null 2>&1; then \
        a2enmod rewrite headers \
    ;fi
sxpgvts3

sxpgvts33#

我也遇到过同样的问题,但是我在httpd.conf中更改了顺序,具体来说,我在服务器定义和文档定义之间加载了rewrite模块。

ServerRoot "/usr/local/apache2"
Listen 80

LoadModule rewrite_module modules/mod_rewrite.so

<IfModule unixd_module>
    User daemon
    Group daemon
</IfModule>

ServerAdmin you@example.com
ServerName localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/htdocs/m1"
    ServerName www.sub1.domain.tld
    ServerPath "/sub1/"
    RewriteEngine on
    RewriteRule "^(/sub1/.*)" "/usr/local/apache2/htdocs/s1$1"
</VirtualHost>

这允许我使用httpd容器而不是php。

FROM httpd:2.4

我想和大家分享一下,以防对大家有帮助。

相关问题