Nginx

NGINX index.php 重寫規則不起作用

  • June 27, 2016

最近我將我的網路伺服器從 cpanel 和 CENTOS 7 更改為使用 nginx 和一個名為 nDeploy 的 scipt,它與 wordpress 和託管在我伺服器上的站點配合得很好。

我有這個腳本不起作用。由於 nginx 配置,我的站點使用主 index.php 訪問大多數頁面,並帶有一些 php 模板解決方案,但我只收到此錯誤:"500 too many redirects"

所以這是我的 .htaccess (當我使用 apache 時,這就是訣竅):

   Options +FollowSymLinks

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

這是我的 nginx 配置文件:

# 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();
?>

您已經定義了兩個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;
}

引用自:https://serverfault.com/questions/786446