【发布时间】:2020-09-16 05:12:38
【问题描述】:
我确信这是一个非常简单的答案,但我似乎无法破解它。我对 nginx 也很陌生,但对 apache2 非常了解。我在 Ubuntu 上运行 nginx 并尝试满足这两个请求:
- 当 URI 调用
filename时,让 nginx 为filename.php提供服务- 调用 http://example.org/bob 将服务于 http://example.org/bob.php,但仍会在浏览器 URL 中显示 http://example.org/bob
- 请求文件夹时,URL 中不显示“索引”
我目前的配置是:
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
【问题讨论】:
标签: nginx redirect mod-rewrite