Ubuntu 14.04 nginx 502 bad getaway 处理及优化

42 篇文章 0 订阅
10 篇文章 0 订阅

一、查看错误信息

  1. 出现 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

参考:
https://tjosm.com/4056/fix-nginx-502-bad-getaway-error-ubuntu/
https://tjosm.com/4068/switch-tcpip-from-unix-socket-nginx/

三、最后,不要忘记下面这个来调整的 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

参考:
https://easyengine.io/tutorials/php/fpm-sysctl-tweaking/
https://easyengine.io/tutorials/linux/sysctl-conf/

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值