从nginx切换到apache后,我的Routing.php无法正常工作



我正在本地机器上(在nginxdocker容器中(做一个小项目,今天我第一次将它上传到我的Web服务器,发现它是apache服务器。

一切都很好,除了我的router.php.

当我访问example.com/admin时,我的apache服务器上出现了一个错误404,但在我的nginx docker容器中,我得到了正确的页面

我的error_log_apache中有AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

<?php
//Router.php
namespace blog;
class Router 
public function __construct(private AdminPage           $adminPage,
private AdminContentPage    $adminContentPage,
private ArticlePage         $articlePage,
private VariablesWrapper    $variablesWrapper,
private SessionManager      $sessionManager){}
public function getPageForUrl() : Page
{
switch ((string)$this->variablesWrapper->getRequestUri()){
case '/admin':
if ($this->sessionManager->isAuthenticated()){
return $this->adminContentPage;
} else {
return $this->adminPage;
}
case preg_match('//([A-Za-z]w+)/?article=[0-9]{1,5}/',(string)$this->variablesWrapper->getRequestUri()): //Hello_world/?article=0
default:
return $this->articlePage;
}
}
}

由于它可能与配置有关,而且我必须知道具体是什么,所以我会给你我的default.conf,它是我的nginx服务器的配置。

server {
listen 80;
index index.php index.twig;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;

location / {
try_files $uri /index.php$is_args$args;
}

location ~ .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

这是我的apache服务器上未经修改的httpd.conf编辑:显然我有权更改httpd配置

# managed by uberspace-generate-httpd-config
<Directory /var/www/virtual/USERNAME>
AllowOverride AuthConfig FileInfo Indexes Limit Options=ExecCGI,Includes,Indexes,MultiViews,SymLinksIfOwnerMatch
Options +Includes
</Directory>
<VirtualHost *>
ServerName USERNAME.uber.space

ServerAlias blog.returnnull.de

ServerAlias www.returnnull.de

ServerAlias USERNAME.uber.space

ServerAlias returnnull.de

SuexecUserGroup USERNAME USERNAME
DocumentRoot /var/www/virtual/USERNAME/html
ServerAdmin USERNAME@uber.space
AllowEncodedSlashes NoDecode

ErrorLog /readonly/USERNAME/logs/error_log_apache
LogLevel notice

RewriteEngine On
# If there is a host-specific pseudo-DocumentRoot, use it instead of the default one
RewriteCond /var/www/virtual/USERNAME/%{HTTP_HOST} -d
RewriteRule (.*) /var/www/virtual/USERNAME/%{HTTP_HOST}$1
<FilesMatch ".php$">
SetHandler  "proxy:unix:/run/php-fpm-USERNAME.sock|fcgi://php-fpm-USERNAME"
</FilesMatch>
<Proxy "fcgi://php-fpm-USERNAME" max=10>
</Proxy>
</VirtualHost>

您必须在项目的根上添加一个.htaccess文件。

RewriteBase /
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

为此,您需要为apache启用重写模块。您可以通过运行sudo a2enmod rewrite来实现这一点。之后,您需要重新启动apache服务器sudo service apache2 restart

最新更新