我正在尝试用nginx建立我的第一个网站,我似乎无法使无扩展的php页面工作。 我希望像/aboutme.php这样的网页显示为/ aboutme 或我想将我的网址更改为/ aboutme而不是/aboutme.php并使其成功。 我设法通过编辑.htaccess使最后一个场景在cpanel托管上工作:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
但是,如果我使用在线工具将htaccess代码转换为nginx,它就无法运行。我也尝试使用try_files和extensionless-php编辑我的nginx.conf但没有成功,我得到404.我没有try_files和extensionless-php的普通nginx.conf看起来像这样:
server {
listen localhost:8081;
server_name mywebsite.com; # I use my website here
root /usr/share/nginx/web/website1;
location / {
root /usr/share/nginx/web/website1;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/web/website1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
编辑:我的问题在其他地方标记为重复,所以我尝试配置它,因为它说没有成功,如果我访问' / aboutme'我找不到404,如果我访问' /aboutme.php' ;,它就打开了' /aboutme.php'与' .php':
server {
listen localhost:8081;
server_name mywebsite.com;
root /usr/share/nginx/web/website1;
location / {
try_files $uri $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
错误日志:2018/03/14 14:39:32 [错误] 7714#7714:* 27 open()" / usr / share / nginx / web / website1 / aboutme"失败(2:没有这样的文件或目录),客户端:127.0.0.1,服务器:mywebsite.com,请求:"获取/关于HTTP / 1.1",主机:" mywebsite.com&# 34;,推荐人:" mywebsite.com /&#34 ;;目录中存在aboutme.php,nginx版本为1.10.3 我尝试多次重启nginx和vps。