微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法让 mod_rewrite 在 docker 容器中工作

如何解决无法让 mod_rewrite 在 docker 容器中工作

我正在将一个 PHP 站点移至 docker,我正在使用 docker-compose。我无法让 mod_rewrite 在 docker 中工作,尽管它在我从中移动它的服务器上运行良好。

我想隐藏 URL 中的文件名和参数,它们在 .htaccess 文件中定义。

示例: 我想将 http://mysite/index.PHP?p1=login 转换为 http://mysite/login

我在 Stackoverflow 和其他地方尝试了多篇文章

请在下面找到完整的来源。

docker-compose.yml

'sl-button' is not a kNown element

Dockerfile

version: "3"
services:
    www:
        container_name: www
        build: .
        restart: always
        ports: 
            - "80:80"
            - "443:443"
        volumes:
            - ./public_html/:/var/www/html
            - ./PHP.ini:/usr/local/etc/PHP/conf.d/30-custom.ini
        links:
            - db
        networks:
            - default
    db:
      container_name: db
      image: MysqL:5.7
      restart: always
      environment:
          MysqL_ROOT_PASSWORD: a1234
          MysqL_DATABASE: test
          MysqL_USER: test
          MysqL_PASSWORD: a1234
      ports:
          - "3306:3306"
      volumes:
          - ./dump:/docker-entrypoint-initdb.d
          - persistent:/var/lib/MysqL
      networks:
          - default
    PHPmyadmin:
        container_name: PHPmyadmin
        image: PHPmyadmin/PHPmyadmin
        restart: always
        links: 
            - db:db
        ports:
            - 8080:80
        environment:
            MysqL_USER: user
            MysqL_PASSWORD: test
            MysqL_ROOT_PASSWORD: test
volumes:
    persistent:

n2.nothing.com.conf

FROM PHP:7.4-apache

# Install stuff
RUN apt-get update && apt-get upgrade -y
RUN apt-get install sudo unzip wget -y
RUN docker-PHP-ext-install MysqLi

# Configure stuff
copY ./n2.nothing.com.conf /etc/apache2/sites-available/n2.nothing.com.conf
RUN ln -s /etc/apache2/sites-available/n2.nothing.com.conf /etc/apache2/sites-enabled/n2.nothing.com.conf
#RUN chmod -R 755 /var/www
#RUN chown -R www-data:www-data /var/www
RUN a2enmod rewrite
RUN a2enmod ssl
RUN service apache2 restart

EXPOSE 80

PHP.ini PHP.ini文件中没有内容

  <VirtualHost *:80>
    <Directory /var/www/html>
      Options Indexes FollowSymLinks
        Allow From All
        RewriteEngine On
      
        RewriteRule ^signup\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=signup&p2=$1&p3=$2 [L]
        RewriteRule ^signup\/([^\/]+)\/?  ./index.PHP?p1=signup&p2=$1 [L]
        RewriteRule ^signup/?$ ./index.PHP?p1=signup [L,NC,QSA]
        RewriteRule ^verify\/([^\/]+)\/?  ./index.PHP?p1=verify&p2=$1 [L]
        RewriteRule ^login\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=login&p2=$1&p3=$2 [L]
        RewriteRule ^login\/([^\/]+)\/?  ./index.PHP?p1=login&p2=$1 [L]
        RewriteRule ^login/?$ ./index.PHP?p1=login [L,QSA]
        RewriteRule ^logout/?$ ./app/logout/ [L,QSA]
        RewriteRule ^cart/?$ ./index.PHP?p1=cart [L,QSA]
        RewriteRule ^checkout/?$ ./index.PHP?p1=checkout [L,QSA]
        RewriteRule ^payment/?$ ./index.PHP?p1=payment [L,QSA]
        RewriteRule ^account\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=account&p2=$1&p3=$2 [L]
        RewriteRule ^account\/([^\/]+)\/?  ./index.PHP?p1=account&p2=$1 [L]
        RewriteRule ^account/?$ ./index.PHP?p1=account [L,QSA]
        RewriteRule ^fapi\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=fapi&p2=$1&p3=$2&ui=0 [L]
        RewriteRule ^fapi\/([^\/]+)\/?  ./index.PHP?p1=fapi&p2=$1&ui=0 [L]
        RewriteRule ^fapi/?$ ./index.PHP?p1=fapi&ui=0 [L,QSA]
        RewriteRule ^admin/?$ ./index.PHP?p1=admin [L,QSA]
        RewriteRule ^designer\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=designer&p2=$1&p3=$2 [L]
        RewriteRule ^designer\/([^\/]+)\/?  ./index.PHP?p1=designer&p2=$1 [L]
        RewriteRule ^designer/?$ ./index.PHP?p1=designer [L,QSA]
        RewriteRule ^product\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=product&p2=$1&p3=$2 [L]
        RewriteRule ^product\/([^\/]+)\/?  ./index.PHP?p1=product&p2=$1 [L]
        RewriteRule ^product/?$ ./index.PHP?p1=product [L,QSA]
        RewriteRule ^contact\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=contact&p2=$1&p3=$2 [L]
        RewriteRule ^contact\/([^\/]+)\/?  ./index.PHP?p1=contact&p2=$1 [L]
        RewriteRule ^contact/?$ ./index.PHP?p1=contact [L,QSA]
        RewriteRule ^cron/?$ ./index.PHP?p1=cron [L,QSA]
        RewriteRule ^terms/?$ ./index.PHP?p1=terms [L,QSA]
        RewriteRule ^privacy/?$ ./index.PHP?p1=privacy [L,QSA]
    </Directory>

    ServerAdmin me@nothing.com
    ServerName nothing.com
    ServerAlias n2.nothing.com
    
    DocumentRoot /var/www/html
    ErrorLog /var/www/error.log
    CustomLog /var/www/access.log combined
  </VirtualHost>
  
ServerSignature Off
ServerName n2.nothing.com

自述文件


.htaccess

# 1 - First I build
sudo docker-compose build --no-cache

# 2 - Then I run docker-compose
sudo docker-compose up -d

# 3 - I log into the www container to look for errors in the /var/www/error.log file
docker exec -it www /bin/bash
# 1 - First I build
sudo docker-compose build --no-cache

# 2 - Then I run docker-compose
sudo docker-compose up -d

# 3 - I log into the www container to look for errors in the /var/www/error.log file
docker exec -it www /bin/bash

来源: public_html/index.PHP

RewriteEngine On
RewriteRule ^signup\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=signup&p2=$1&p3=$2 [L]
RewriteRule ^signup\/([^\/]+)\/?  ./index.PHP?p1=signup&p2=$1 [L]
RewriteRule ^signup/?$ ./index.PHP?p1=signup [L,QSA]
RewriteRule ^verify\/([^\/]+)\/?  ./index.PHP?p1=verify&p2=$1 [L]
RewriteRule ^login\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=login&p2=$1&p3=$2 [L]
RewriteRule ^login\/([^\/]+)\/?  ./index.PHP?p1=login&p2=$1 [L]
RewriteRule ^login/?$ ./index.PHP?p1=login [L,QSA]
RewriteRule ^logout/?$ ./app/logout/ [L,QSA]
RewriteRule ^cart/?$ ./index.PHP?p1=cart [L,QSA]
RewriteRule ^checkout/?$ ./index.PHP?p1=checkout [L,QSA]
RewriteRule ^payment/?$ ./index.PHP?p1=payment [L,QSA]
RewriteRule ^account\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=account&p2=$1&p3=$2 [L]
RewriteRule ^account\/([^\/]+)\/?  ./index.PHP?p1=account&p2=$1 [L]
RewriteRule ^account/?$ ./index.PHP?p1=account [L,QSA]
RewriteRule ^fapi\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=fapi&p2=$1&p3=$2&ui=0 [L]
RewriteRule ^fapi\/([^\/]+)\/?  ./index.PHP?p1=fapi&p2=$1&ui=0 [L]
RewriteRule ^fapi/?$ ./index.PHP?p1=fapi&ui=0 [L,QSA]
RewriteRule ^admin/?$ ./index.PHP?p1=admin [L,QSA]
RewriteRule ^designer\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=designer&p2=$1&p3=$2 [L]
RewriteRule ^designer\/([^\/]+)\/?  ./index.PHP?p1=designer&p2=$1 [L]
RewriteRule ^designer/?$ ./index.PHP?p1=designer [L,QSA]
RewriteRule ^product\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=product&p2=$1&p3=$2 [L]
RewriteRule ^product\/([^\/]+)\/?  ./index.PHP?p1=product&p2=$1 [L]
RewriteRule ^product/?$ ./index.PHP?p1=product [L,QSA]
RewriteRule ^contact\/([^\/]+)\/([^\/]+)\/?  ./index.PHP?p1=contact&p2=$1&p3=$2 [L]
RewriteRule ^contact\/([^\/]+)\/?  ./index.PHP?p1=contact&p2=$1 [L]
RewriteRule ^contact/?$ ./index.PHP?p1=contact [L,QSA]
RewriteRule ^cron/?$ ./index.PHP?p1=cron [L,QSA]
RewriteRule ^terms/?$ ./index.PHP?p1=terms [L,QSA]
RewriteRule ^privacy/?$ ./index.PHP?p1=privacy [L,QSA]

解决方法

我找到了解决此问题的方法。

移动 .htaccess 中的所有内容,然后删除 .htaccess 文件。然后将内容移动到 n2.nothing.com.conf 文件中。

示例中的 n2.nothing.com.conf 文件已更新。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。