nginx 在继续重定向到目录后删除 php 扩展,即使具有相同名称的 php 文件
默认服务器配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ /\.ht {
deny all;
}
}
我的根文件夹喜欢的
var/www/html
- service.php
- 服务(目录)
- ㄴindex.php
当我在这里连接 http://example/service
只是重定向到 service/index.php 而不是 service.php
我做错了什么??
我想同时使用它们
http://www.exmaple/service -> service.php
和
http://www.example/service/ -> service/index.php
我正在使用 ubuntu 20.04 nginx 1.18.0 php 8.1
my default server config
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ /\.ht {
deny all;
}
}
my root folder likes
var/www/html
- service.php
- service (diretory)
- ㄴ index.php
when i connect with http://example/service here
just redirect to service/index.php not service.php
what i did wrong ??
i want use both of them
http://www.exmaple/service -> service.php
and
http://www.example/service/ -> service/index.php
i'm using ubuntu 20.04 nginx 1.18.0 php 8.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
try_files $uri $uri/ @extensionless-php;
中,$uri/
项测试service
是否是之前<的目录/em> 您分支到@extensionless-php
将 URI 重写为service.php
。您可以从try_files
语句中删除该术语,只要这不会破坏您网站的其他部分。In
try_files $uri $uri/ @extensionless-php;
, the$uri/
term tests ifservice
is a directory before you branch to@extensionless-php
to rewrite the URI toservice.php
. You could just remove that term from thetry_files
statement, as long as that doesn't break another part of your website.