一、查看错误信息
出现 nginx 502 bad getaway 后,查看 nginx 错误信息
sudo vi /etc/nginx/sites-available/default
2015/01/15 23:36:50 [error] 1924#0: *27095 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: xxx.xxx.xxx.xxx,...
这类错误消息有数百行。因为它说,连接到PHP-fpm.sock是失败的。它说,该资源(PHP-fpm.sock)暂时不可用。这意味着它是越来越高负载。其在高负载情况下的问题是众所周知的。
解决方案:
使用Unix套接字稍微快一些,因为它提供你没有任何的TCP / IP开销直接网络接入。不利的一面,它不是可伸缩如TCP / IP。当 sockets 已经所剩无几的Nginx将引发502错误。在这种情况下,你可以调整操作系统设置,以适应更大的连接池或只需切换到TCP / IP。
二、php5+nginx : 从Unix套接字切换到TCP/IP
1.打开PHP-FPM池的配置文件:
sudo vi /etc/php5/fpm/pool.d/www.conf
2.搜索以下行,并取消注释 ;
listen.backlog = 65536
3.现在搜索以下行
listen = /var/run/php5-fpm.sock
并替换成如下:
listen = 127.0.0.1:9000
4.被更改的php-fpm的现在完成了。但还应该做的Nginx虚拟主机配置。打开虚拟主机文件下列命令
sudo vi /etc/nginx/sites-available/default
寻找下面的行,
fastcgi_pass unix:/var/run/php5-fpm.sock;
并替换成如下:
fastcgi_pass 127.0.0.1:9000;
5.现在重新启动 php5-fpm 和 nginx
sudo service php5-fpm restart
sudo service nginx restart
三、最后,不要忘记下面这个来调整的 Linux 的 sysctl 的 value
1.打开 sysctl.conf 编辑
sudo vi /etc/sysctl.conf
添加如下配置代码,加入到 sysctl.conf 文件底部:
vm.dirty_background_ratio = 2
### GENERAL NETWORK SECURITY OPTIONS ###
# Number of times SYNACKs for passive TCP connection.
net.ipv4.tcp_synack_retries = 2
# Allowed local port range
net.ipv4.ip_local_port_range = 2000 65535
# Protect Against TCP Time-Wait
net.ipv4.tcp_rfc1337 = 1
# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15
# Decrease the time default value for connections to keep alive
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
### TUNING NETWORK PERFORMANCE ###
# Default Socket Receive Buffer
net.core.rmem_default = 31457280
# Maximum Socket Receive Buffer
net.core.rmem_max = 12582912
# Default Socket Send Buffer
net.core.wmem_default = 31457280
# Maximum Socket Send Buffer
net.core.wmem_max = 12582912
# Increase number of incoming connections
net.core.somaxconn = 4096
# Increase number of incoming connections backlog
net.core.netdev_max_backlog = 65536
# Increase the maximum amount of option memory buffers
net.core.optmem_max = 25165824
# Increase the maximum total buffer-space allocatable
# This is measured in units of pages (4096 bytes)
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144
# Increase the read-buffer space allocatable
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384
# Increase the write-buffer-space allocatable
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384
# Increase the tcp-time-wait buckets pool size to prevent simple DOS attacks
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
2.运行下面的命令加载 sysctl 相关变动。
sysctl -p
linux系统下nginx+php-fpm报错:502 Bad Gateway解决方法:http://www.linuxdiyf.com/linux/13927.html
Ubuntu14.04配置nginx开机自启动项:http://www.linuxdiyf.com/linux/16608.html
Debian/Ubuntu下Nginx的安装、反向代理和负载均衡的基本配置:http://www.linuxdiyf.com/linux/11079.html
CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡:http://www.linuxdiyf.com/linux/10141.html
Linux下Nginx反向代理实现负载均衡:http://www.linuxdiyf.com/linux/9880.html