【发布时间】:2021-01-06 20:24:27
【问题描述】:
我正在尝试从 apache 迁移到 nginx,但在 URL 重写方面遇到了一些问题。 这是示例的根结构:
- 根
- dir-1
- dir-2
- index.php
- do.php
- desc.ini
- .隐藏
- dir-1
- file-1.php
- file-2.php
我正在努力实现这些结果:
1. http://url/dir-1 -> 404 error
2. http://url/dir-1/ -> 404 error
3. http://url/dir-1/file-1.php -> 404 error
4. http://url/dir-1/file-2.php -> 404 error
5. http://url/desc.ini -> 404 error
6. http://url/.hidden -> 404 error
7. http://url/ -> 404 error
8. http://url/index.php -> 404 error
9. http://url/do.php -> 404 error
10. http://url/<param1>/<param2> -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
11. http://url/<param1>/<param2>/ -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
12. http://url/<[a-zA-Z0-9]{10}> -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>
13. http://url/<[a-zA-Z0-9]{10}>/ -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>
现在我成功完成了第 10 步到第 13 步,但是当我尝试执行其他操作时,这些文件是可下载的。 显然这个结构只是一个例子,我试图达到一般规则。
我发布了一个我正在尝试的 nginx 配置示例。
谢谢!
server {
listen <port>;
listen [::]:<port>;
server_name <url>;
root <path1>;
index <path1>;
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
location @error {
try_files $uri $uri /error.html;
}
location ~ \.ini {
rewrite /.*\.ini /error.html;
}
location ~ \.[a-zA-Z0-9]* {
rewrite /\.[a-zA-Z0-9]* /error.html;
}
location ~ /(<param1>)/(<param2>)[/$] {
try_files $uri $uri/ /do.php?p1=$1&p2=$2;
}
location ~ /([a-zA-Z0-9]*)[/$] {
try_files $uri $uri /index.php?p3=$1;
}
location ~ \.php {
try_files $uri =404;
include <path2>/fastcgi_params;
include <path2>/fastcgi.conf;
fastcgi_pass unix:<path3>/php7.3-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
【问题讨论】:
-
我是否理解正确,除了
http://url/<param1>/<param2>和http://url/<[a-zA-Z0-9]{10}>带有或不带有斜杠之外的所有请求都生成404 错误? -
是的,是这样的
标签: php nginx url-rewriting