【问题标题】:My Routing.php doesnt work properly after switch from nginx to apache从 nginx 切换到 apache 后,我的 Routing.php 无法正常工作
【发布时间】:2021-12-19 07:10:42
【问题描述】:

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

一切正常,除了我的 router.php。

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

我的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>

【问题讨论】:

  • 您尝试过什么解决问题的方法?是导致重定向的 PHP 脚本,还是 Apache 本身?

标签: php apache nginx


【解决方案1】:

您必须在项目的根目录中添加一个.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

【讨论】:

  • 由于 OP 在 nginx 配置中使用 try_files $uri /index.php$is_args$args; 而不是 try_files $uri $uri/ /index.php$is_args$args;,因此不需要 RewriteCond %{REQUEST_FILENAME} !-d,只需 RewriteCond %{REQUEST_FILENAME} !-f 就足够了。
  • 当我访问 /admin 时,这似乎给了我一个错误 500,但我的 default 页面有效。当我删除 RewriteCond %{REQUEST_FILENAME} !-d 时,我的 default 页面也会出现错误 500。
  • @tolik518 请检查一次apache错误日志/var/log/apache2/error.log
  • @PrinceKumarBarnwal 刚刚检查了日志,我收到了.htaccess: AllowOverride not allowed here
  • @tolik518 伙计,您似乎不允许在您的主机中覆盖,请查看此答案希望它对您有所帮助stackoverflow.com/questions/35508635/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
相关资源
最近更新 更多