我是 NGINX 新手,正在尝试在目录中设置一个简单的 phpinfo 页面。我使用以下指令/home/codehitman/nginx/phpinfo
设置了该文件:/etc/hosts
127.0.0.1 phpinfo.local
这是配置文件:
server {
listen 80;
server_name phpinfo.local;
root /home/codehitman/nginx/phpinfo;
access_log /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
虚拟主机文件已软链接到 sites-enabled 文件夹,并且服务器已使用 重新启动[OK]
。但每当我转到该页面时,我总是看到默认的 nginx 页面。我没有看到生成的日志文件,但如果我查看默认tail
日志,则会看到以下内容:
127.0.0.1 - - [02/Jan/2016:22:03:20 -0500] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0"
当我在浏览器的开发人员工具中检查标头时,我看到的GET
是 的请求/
,而不是phpinfo.local
。我还将所有权和权限更改为主目录中的 nginx 目录:
sudo chown -R www-data:www-data /home/codehitman/nginx
sudo chmod -R 777 /home/codehitman/nginx
有人能告诉我我做错了什么吗?谢谢!
编辑: 所以我弄清楚了为什么第一次无法正常工作。我错误地软链接了它。现在它已正确链接,但我在日志中收到 502 Bad gateway 错误,其中显示以下内容:
2016/01/02 22:52:36 [error] 18653#0: *16 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: phpinfo.local, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "phpinfo.local"
答案1
解决了!我不得不用.php
这个来代替:
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
还对虚拟主机进行以下更改default
:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}