我正在将我的 Web 应用从 Apache 移至 AZURE 下的 NGINX。文件夹结构如下
- public/
- index.php
- group.php
- inc/
- group/
- router.php
- body.php
- body_inc.php
当我获得 URL 时,https://group?page=2
我的配置就可以正常工作并且可以使用$_GET['page'] = 2
。当我进入https://group/body_inc
流程时,index.php->group.php->body.php->router.php->body_inc.php.
我希望到达https://group/body/body_inc?page=2
可以为我服务的点$_GET['page'] = 2
。
我之前的 .htacces 定义为
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
现在我的 NGINX 配置是
add_header Set-Cookie "Path=/, HttpOnly, Secure";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains, SameSite=strict";
#
# HTTPS SERVER
#
server {
listen 8080;
listen [::]:8080;
#
# DEFAULT DOCUMENT ROOT
#
root /home/site/wwwroot/public;
#
# BASIC CONFIGURATION
#
server_name *.azurewebsites.net www.*.azurewebsites.net;
port_in_redirect off;
server_tokens off;
absolute_redirect off;
#
# GENERATE X-VAR TO HEADER FOR NONCE USE
#
add_header X-var $request_id;
include fastcgi_params;
#
# URL LOCATIONS SETUP
#
location / {
index index.php;
# REWRITE URL RULES
try_files $uri $uri/ /index.php?url=$uri$is_args$args;
#UTF-8 CHARACTERS
charset UTF-8;
# CSP NONCE
fastcgi_param REQUEST_ID $request_id;
# SECURE COOKIE
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
# UPLOAD FILES UP TO 100MB
client_max_body_size 100M;
}
#
# REDIRECT SERVER ERROR PAGES TO STATIC PAGE /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /html/;
}
#
# DISABLE GIT DIRECTORY
#
location ~ /\.git {
deny all;
access_log off;
log_not_found off;
}
#
# LOCATIONS OF PHPMYADMIN
#
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 3600;
fastcgi_read_timeout 3600;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
# HIDE PHP HEADERS
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-CF-Powered-By;
}
}
有人能告诉我故障根源在哪里吗?谢谢
我尝试过摆弄 fastcgi 参数,但似乎我的配置是错误的。定义location /group/
或类似操作不是一个选项,因为有多个文件夹(如 Group)正在使用中。我也试过
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?url=$1 break;
}
但它对我也不起作用。
答案1
问题出在这一行:
try_files $uri $uri/ /index.php?url=$uri$is_args$args;
当请求URL为 时https://group/body/body_inc?page=2
,上面的最后部分变为:
/index.php?url=/body/body_inc?page=2
$is_args
当有参数时为?
,否则为空。
这无法正常工作,因为多个查询参数的分隔符是&
。
有一个解决方法如下:
在http
级别中,定义一个地图以获取适当的分隔符:
map $is_args $extra_args {
? &;
default '';
}
然后,使用它try_files
:
try_files $uri $uri/ /index.php?url=$url$extra_args$args;
那么生成的 URL 将是正确的,并且$_GET
数组也应该被正确填充。
答案2
即使没有地图,我也找到了解决方案。这对我来说非常有效。
try_files $uri $uri/ /index.php?url=$uri$args;
set $args &$args;
感谢 Tero 提供的提示!