NGINX index.php 重写规则不起作用

NGINX index.php 重写规则不起作用

最近我将我的网络服务器从 cpanel 和 CENTOS 7 更改为使用带有名为 nDeploy 的脚本的 nginx,该脚本可以很好地与 wordpress 和托管在我服务器上的网站配合使用。

我有这个脚本,但是它不能正常工作。由于 nginx 配置,我的网站使用主 index.php 来访问大多数页面,并使用了一些 php 模板解决方案,但我只收到此错误:"500 too many redirects"

这是我的 .htaccess(当我使用 apache 时,这个技巧就奏效了):

    Options +FollowSymLinks

RewriteEngine On 
RewriteRule ^([^/]*)\.php$ index.php?page=$1 [L,QSA]

这是我的 nginx conf 文件:

# Downloads
rewrite ^/downloads/([0-9]+)/([^/]*)$ /./downloads.php?action=displaycat&catid=$1 last;
rewrite ^/downloads$ /./downloads.php last;

#Knowledgebase
rewrite ^/knowledgebase/([0-9]+)/[a-zA-Z0-9-]+\.html$ /./knowledgebase.php?action=displayarticle&id=$1 last;
rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last;
rewrite ^/knowledgebase$ /./knowledgebase.php last;


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

location /NaxsiRequestDenied {
       return 418;
   }

location ~ ^/pingphpfpm$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/opt/remi/php56/root/var/run/user.sock;
}

location ~* /\.(?!well-known\/) { deny all; access_log off; log_not_found off; }
autoindex on;

location ~ \.php$ {
    include /etc/nginx/conf.d/naxsi_learn.rules;
    include /etc/nginx/sites-enabled/mysite.com.nxapi.wl;

    try_files $uri =404;
        fastcgi_pass unix:/opt/remi/php56/root/var/run/extranet.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
}

include /etc/nginx/conf.d/cpanel_services.conf;
# nginx configuration
location / {
rewrite ^/([^/]*)\.php$ /index.php?page=$1 break;
}

这是我的 index.php 文件:

<?php ob_start();
include "inc/config.php";
    $page = $_GET["page"];  
    if(!isset($page) || $page == "" ){ 
        header("Location:".SITE."index.php");
        exit();
    }
    $access = 1;

include "inc/template.php";
ob_flush();
?>

答案1

您定义了两个location /块,这是一个错误。您应该检查错误日志或使用 测试您的配置nginx -t

任何以 结尾的 URI.php都将在块中处理location ~ \.php$,您应该将rewrite ... break语句放在此处。

例如:

location ~ \.php$ {
    include /etc/nginx/conf.d/naxsi_learn.rules;
    include /etc/nginx/sites-enabled/mysite.com.nxapi.wl;

    rewrite ^/([^/]*)\.php$ /index.php?page=$1 break;

    try_files $uri =404;
    fastcgi_pass unix:/opt/remi/php56/root/var/run/extranet.sock;
    include /etc/nginx/fastcgi_params;
}

相关内容