shallow丿ove
nginx解析php的配置
- 配置 location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }
- fastcgi_pass #用于指定php-fpm监听的地址或socket
[root@localhost ~]# vi /data/wwwroot/test.com/3.php
1 <?php
2 phpinfo();
[root@localhost ~]# curl -x 127.0.0.1:80 test.com/3.php
<?php
phpinfo();
[root@localhost ~]# vi /usr/local/nginx/conf/vhost/test.com.conf
37 location ~ .*(upload|image)/.*\.php$
38 {
39 deny all;
40 }
41
42 if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
43 {
44 return 403;
45 }
46
47 location ~ \.php$
48 {
49 include fastcgi_params;
50 fastcgi_pass unix:/tmp/php-fcgi.sock;
51 fastcgi_index index.php;
52 fastcgi_param SCRIPT_FILEMANE /data/wwwrooot/test.com$fastcgi_script_name;
53 }
54
55 access_log /tmp/test.com.log;
56 }
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
502 Bad的问题
[root@localhost ~]# vi /usr/local/nginx/conf/vhost/test.com.conf
将
50 fastcgi_pass unix:/tmp/php-fcgi.sock;
改错为
50 fastcgi_pass unix:/tmp/php-cgi.sock;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -x 127.0.0.1:80 test.com/test.php
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
[root@localhost ~]# tail /usr/local/nginx/logs/nginx_error.log
2018/03/02 16:07:03 [notice] 7416#0: nginx/1.8.0
2018/03/02 16:07:03 [notice] 7416#0: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
2018/03/02 16:07:03 [notice] 7416#0: OS: Linux 3.10.0-123.el7.x86_64
2018/03/02 16:07:03 [notice] 7416#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2018/03/02 16:07:03 [notice] 7417#0: start worker processes
2018/03/02 16:07:03 [notice] 7417#0: start worker process 7418
2018/03/02 16:07:03 [notice] 7417#0: start worker process 7419
解释
location ~ \.php$
{
include fastcgi_params;
# include语句会获取指定文件中存在的所有文本/代码/标记,并复制到使用 include 语句的文件中。
fastcgi_pass unix:/tmp/php-fcgi.sock;
# fastcgi_pass 127.0.0.1:9000;
# 指定FastCGI服务器监听端口与地址,可以是本机或者其它:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/zlw.com$fastcgi_script_name;
# #脚本文件请求的路径
}
[root@localhost ~]# vi /data/wwwroot/test.com/3.php
502找不到sock
将/usr/local/nginx/conf/vhost/test.com.conf中的fastcgi_pass unix:/tmp/php-fcgi.sock;改为php-cgi.sock
更改级别
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
1 user nobody nobody;
2 worker_processes 2;
3 error_log /usr/local/nginx/logs/nginx_error.log crit;
4 pid /usr/local/nginx/logs/nginx.pid;
5 worker_rlimit_nofile 51200;
6 events
7 {
8 use epoll;
9 worker_connections 6000;
10 }
.
.
.
将crit改为debug为
1 user nobody nobody;
2 worker_processes 2;
3 error_log /usr/local/nginx/logs/nginx_error.log debug;
4 pid /usr/local/nginx/logs/nginx.pid;
5 worker_rlimit_nofile 51200;
6 events
7 {
8 use epoll;
9 worker_connections 6000;
10 }
.
.
.
[root@localhost ~]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ OK ]
[root@localhost ~]# tail /usr/local/nginx/logs/nginx_error.log
访问时提示没有php-cgi.sock的文件
查询定义的sock文件
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024