今天在配置一个纯静态的小项目时,nginx conf文件都配置好了,就是复制之前的项目配置文件的,但运行的时候出现了500错误;配置文件检查了N遍,重启服务后都没用。于是就去网上百度搜了搜,很多答案都不是我想要的。
其实nginx 500错误并不是配置错误,如果配置错误的话,在启动ngin时会直接报错,并且启动不成功;而出现500错误,说明nginx已经启动成功了,可能只是配置文件配置不规范或者其他错误导致的。
而我的这次配置是错误在哪了呢?后来我发现果然是配置不规范导致的!
server
{
listen 80;
#listen [::]:80;
server_name tools.dsboke.com;
index index.html index.htm index.php default.html default.htm default.php;
root D:\Wnmp\html\toolsdsboke;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME D:\Wnmp\html\toolsdsboke\$fastcgi_script_name;
include fastcgi_params;
}
#include rewrite/thinkphp.conf;
#include enable-php-pathinfo.conf;
#include enable-php7.3.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log D:/Wnmp/conf/logs/tools.dsboke.com.log;
}
看到上面的配置,是不是乍一看没啥毛病?我一开始也是这么觉得的,但就是出现问题了,后来想一想,其实之前也出现过,所以这次又出现了,赶紧记录下来,免得下次还犯!
其实问题很简单,就是粗心大意;root 路径配置不规范(我的系统环境是windows):
root D:\Wnmp\html\toolsdsboke; //这个是错误示范,路径斜杠应该是“/”,而不是“\”
root D:/Wnmp/html/toolsdsboke; //这是正确的
fastcgi_param SCRIPT_FILENAME D:\Wnmp\html\toolsdsboke\$fastcgi_script_name; //这个也是如此