我相信这是一个非常简单的答案,但我似乎无法破解它。我对nginx也很陌生,但对apache2非常了解。我正在 Ubuntu 上运行 nginx,并试图满足这两个请求:
- 让 nginx 在 URI 调用时提供
filename.php
filename
- 调用 http://example.org/bob 将起到 http://example.org/bob.php 作用,但仍 http://example.org/bob 显示在浏览器 URL 中
- 请求文件夹时,URL 中不会显示"索引"
- 调用 http://example.org/jack/将起到 http://example.org/jack/index.php 但 http://example.org/jack/仍显示在 URL 中(而不是 http://example.org/jack/index(
我目前的配置是:
server {
listen 80;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ @php;
}
location @php {
try_files $uri.php =404;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
rewrite ^/(.*).php$ /$1 redirect;
location ~ /.ht {
deny all;
}
}
我觉得我真的很接近,需求 (1( 完美无缺,但我似乎找不到正确的配置来解决需求 (2(。
如果有帮助,这是在 Apache2 中工作的配置,启用了mod_rewrite模块:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # if a folder isn't called ...
RewriteCond %{REQUEST_FILENAME}.php -f # but a php file is ...
RewriteRule ^(.*)$ $1.php # take the basename, add .php then call that
看到这可以帮助您启用 Apache2 将规则重写为 nginx 规则:
https://www.nginx.com/blog/converting-apache-to-nginx-rewrite-rules/
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /$1.php;
}
}