Php 动态页面的Nginx重写不起作用
我有一部分我的网站,我希望包含许多文件,由某些类别划分,每个类别作为自己的文件夹。但是,用户不必导航到具有类似Php 动态页面的Nginx重写不起作用,php,nginx,rewrite,Php,Nginx,Rewrite,我有一部分我的网站,我希望包含许多文件,由某些类别划分,每个类别作为自己的文件夹。但是,用户不必导航到具有类似URL的页面http://example.com/subpage/?cat=random&page=stuff我更愿意重写URL以允许使用http://example.com/subpage/random/stuff 通过这种方式,我可以使用一个“框架页面”,其中包含我的站点需要加载的所有其他内容,并且只在主页中包含请求的文件(本例中为subpage.php) 但是,在实现重写规则时,我
URL的页面http://example.com/subpage/?cat=random&page=stuff
我更愿意重写URL以允许使用http://example.com/subpage/random/stuff
通过这种方式,我可以使用一个“框架页面”,其中包含我的站点需要加载的所有其他内容,并且只在主页中包含请求的文件(本例中为subpage.php)
但是,在实现重写规则时,我遇到了一些问题,例如动态加载的文件无法检测主页中包含的php文件(这导致我的服务器被500个错误阻塞),或者显示原始文本,但没有CSS、jquery脚本或主页中包含的其他HTML文件
以下是我的主页中处理URL并请求包含该页面的部分:
if(!empty($_GET['cat']) && !empty($_GET['page'])) {
$folder = $_GET['cat'];
$page = $_GET['page'] . ".php";
$pages = scandir($folder);
unset($pages[0], $pages[1]);
if(file_exists($folder . DIRECTORY_SEPARATOR . $page) && in_array($page, $pages)) {
include("important-file.php");
include($folder . DIRECTORY_SEPARATOR . $page);
} else {
header("HTTP/1.0 404 Not Found");
}
} else {
include("contents.php");
}
服务器抛出错误,因为第二个include语句加载的页面无法调用“important file.php”中公开的函数,如果我将include语句移动到页面本身,则只打印该页面的内容,而不打印我的站点的页眉/页脚、CSS或通常由“important file.php”打印的内容
下面是我的服务器使用的Nginx重写规则:
rewrite ^/([^/]*)/([^/]*)$ /subpage/?cat=$1&page=$2 last;
我已经删除了上面很多无关的代码,比如回显div和其他HTML格式。我主要关注的是在主页的主体中包含我需要的页面
Nginx配置文件:
server {
listen 80 default_server;
listen [::]:80 ipv6only=on;
root /usr/share/nginx/html;
server_name localhost;
error_page 403 /;
error_page 404 /error/404.php;
error_page 500 502 503 504 /error/50X.php;
location / {
try_files $uri $uri/ @no-extension;
index index.html index.htm index.php;
#allow 192.168.0.0/24;
#allow 127.0.0.1;
#deny all;
}
location /subpage {
try_files $uri $uri/ @no-extension;
index index.html index.htm index.php;
rewrite ^/subpage/([^/]*)/([^/]*)$ /subpage/?cat=$1&page=$2 last;
}
# PHP Handler
location ~ \.php$ {
try_files $uri $uri/ =404;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
}
# Deny access to or drop hidden file access from logs
location ~ /\. {access_log off; log_not_found off; deny all;}
location ~ ~$ {access_log off; log_not_found off; deny all;}
location = /robots.txt {access_log off; log_not_found off;}
# Browser caching
location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|woff|ttf|svg)$ {
expires max;
log_not_found off;
}
location @no-extension {
rewrite ^(.*)$ $1.php last;
}
}
但是您在重写时没有考虑文件夹
/subpage
,您的意思是^/subpage/([^/]*)/([^/]*)$
停止错误,但是页面仍然只加载“包含文件”的基本内容,没有站点页眉/页脚,没有CSS,或者脚本。你能分享你的整个nginx配置吗?我已经编辑了上面的问题,将配置包括在内,因为它不适合在评论中。