hailai 发表于 2018-11-8 06:34:54

nginx的502问题

# vim test.com.conf  
   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;
  
       }
  
(如上在虚拟主机里面配置的php解析,需要注意几个地方1fastcgi_pass unix:/tmp/php-fcgi.sock; 这里的路径不要写错,如果写错会出现502问题,
  
# cat /usr/local/php-fpm/etc/php-fpm.conf
  

  
pid = /usr/local/php-fpm/var/run/php-fpm.pid
  
error_log = /usr/local/php-fpm/var/log/php-fpm.log
  

  
listen = /tmp/php-fcgi.sock (这个路径要与nginx虚拟主机的路径完全保持一致)
  
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
  
还有另一种情况就是php监听的不是sock,而是ip加端口的情况时
  
location ~ \.php$
  
            {
  
            include fastcgi_params;
  
            #fastcgi_pass unix:/tmp/php-fcgi.sock;
  
            fastcgi_pass 127.0.0.1:9000
  
            fastcgi_index index.php;
  
            fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com/$fastcgi_script_name;
  
       }
  
这时fastcgi_pass 127.0.0.1:9000 这里就需要这样操作,这的ip要与php配置文件里面监听的地址保持一致)
  
第2个需要注意的地方:fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com/$fastcgi_script_name; 这里的路径要与server 下的
  
{
  
    listen 80;
  
    server_name test.com test2.com lll.com;
  
    index index.html index.htm index.php admin.php;
  
    root /data/wwwroot/test.com; 这个路径保持一致。
  
    if ($host != 'test.com' ) {
  
       rewrite^/(.*)$http://test.com/$1permanent;
  
    }
  
3.php5.4之后的版本有个特性:
  
# vi /usr/local/php-fpm/etc/php-fpm.conf (在配置文件中)
  
listen = /tmp/php-fcgi.sock
  
listen.mode = 666
  
(如果监听的是sock就必须要监听mode)
  
#listen.mode = 666
  
# ls -l /tmp/php-fcgi.sock
  
srw-rw---- 1 root root 0 8月15 03:02 /tmp/php-fcgi.sock
  
(可以看到属主属组都为root,并且没有读的权限)
  
# curl -x127.0.0.1:80 test.com/admin.php -I
  
HTTP/1.1 502 Bad Gateway
  
Server: nginx/1.12.1
  
Date: Mon, 14 Aug 2017 19:03:48 GMT
  
Content-Type: text/html
  
Content-Length: 173
  
Connection: keep-alive
  
(当在curl时会发现报502)
  
# tail -n3 /usr/local/nginx/logs/nginx_error.log
  
2017/08/14 22:24:06 3684#0: *7 connect() to unix:/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/php-fcgi.sock:", host: "test.com"
  
2017/08/15 03:03:48 3682#0: *5 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
  
2017/08/15 03:06:03 3682#0: *7 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
  
(查看错误日志,Permission denied发现拒绝,没有权限。是因为它的属主,属组为nobody ,此时如果将/tmp/php-fcgi.sock 的权限设置为nobody就没有任何问题了)
  
# ps aux |grep nginx
  
root       34850.00.1212721652 ?      Ss   02:28   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  
nobody   36820.00.2231523796 ?      S    03:01   0:00 nginx: worker process
  
nobody   36830.00.2231523300 ?      S    03:01   0:00 nginx: worker process
  
root       37910.00.0 112664   972 pts/0    S+   03:11   0:00 grep --color=auto nginx
  
(还有一种情况也是502就是nginx的资源耗尽))


页: [1]
查看完整版本: nginx的502问题