nginx 502 (Resource temporarily unavailable)

查看连接数和当前的连接数

netstat -ant | grep $ip:80 | wc -l   
netstat -ant | grep $ip:80 | grep EST | wc -l

查看IP访问次数

netstat -nat|grep ":80"|awk '{print $5}' |awk -F: '{print $1}' | sort| uniq -c|sort -n

实时查看连接数

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'  
watch "netstat -n | awk '/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}'"

返回结果示例

LAST_ACK 5
SYN_RECV 30 
ESTABLISHED 1597 
FIN_WAIT1 51 
FIN_WAIT2 504 
TIME_WAIT 1057

说明

  • SYN_RECV 表示正在等待处理的请求数;
  • ESTABLISHED 表示正常数据传输状态;
  • TIME_WAIT 表示处理完毕,等待超时结束的请求数;
  • FIN_WAIT1 表示server端主动要求关闭tcp连接;
  • FIN_WAIT2 表示客户端中断连接;
  • LAST_ACK 关闭一个TCP连接需要从两个方向上分别进行关闭,双方都是通过发送FIN来表示单方向数据的关闭,当通信双方发送了最后一个FIN的时候,发送方此时处于LAST_ACK状态,当发送方收到对方的确认(Fin的Ack确认)后才真正关闭整个TCP连接;

解决办法

/etc/security/limits.conf

www-data   soft    nproc   51200
www-data   hard    nproc   16384
www-data   soft    nofile   51200
www-data   hard    nofile   51200

php-fpm配置及管理(无须重启,即可重新加载配置及LOG日志)

PHP-FPM参数

php-fpm {start|stop|quit|restart|reload|logrotate}
--start 启动php的fastcgi进程
--stop 强制终止php的fastcgi进程
--quit 平滑终止php的fastcgi进程
--restart 重启php的fastcgi进程
--reload 重新平滑加载php的php.ini
--logrotate 重新启用log文件

master进程信号

SIGINT, SIGTERM 立刻终止
SIGQUIT 平滑终止
SIGUSR1 重新打开日志文件
SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:
php-fpm 关闭:
kill -SIGINT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm 重启:
kill -SIGUSR2 `cat /usr/local/php/var/run/php-fpm.pid`

nginx ssl 配置

生成证书

openssl genrsa -des3 -out xxx.key 1024  
openssl req -new -x509 -key xxx.key -out xxx.crt -days 3650  
openssl rsa -in openssl.key -out xxx_nopass.key 

默认配置

ssl                     on;
ssl_certificate         /etc/nginx/certs/xxx.crt;
ssl_certificate_key     /etc/nginx/certs/xxx_nopass.key;
ssl_session_timeout     5m;

ssl_protocols           SSLv2 SSLv3 TLSv1;
ssl_prefer_server_ciphers       on;

gitolite使用

本地生成一个证书,并传到远程服务器

生成证书

cd ~/.ssh/
ssh-kengen -f shining

将证书传送到远程服务器

scp YourName.pub  YourName@HostName:YourName.pub

安装gitolite(git服务器端)

新增一个git用户,不需要密码。
登录到git用户,可以从root用户通过su切换过去。
确认 ~/.ssh/authorized_keys 不存在
将公钥放在 ~/YourName.pub
运行下面的命令:

# 获取版本库
git clone git://github.com/sitaramc/gitolite
# 创建bin目录,用于存放安装后的文件
mkdir -p ~/bin
# 将gitolite安装到bin目录
gitolite/install -to ~/bin
# 使用YourName.pub公钥初始化版本库
gitolite setup -pk YourName.pub

设置本地的.ssh/config

vim ~/.ssh/config

host githost
  user git
  hostname HostName
  port xxx 
  identityfile ~/.ssh/YourName.pub

管理项目、人员、证书

git clone githost:gitolite-admin.git
conf下是项目及人员配置
keydir下是证书

给fastestmirror再提速

fedora和centos安装了fastermirror之后会快一些,但是还是经常选到国外的站点。

方法1:修改源配置文件(推荐,以fedora20为例)

sudo vim fedora.repo
# 找到以下这行
metalink=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch
# 后面添加&country=cn
metalink=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch&country=cn
sudo vim fedora-updates.repo
# 找到以下这行
metalink=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&arch=$basearch
# 后面添加&country=cn
metalink=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&arch=$basearch&country=cn

方法2:修改fastestmirror.conf

sudo vim /etc/yum/pluginconf.d/fastestmirror.conf

修改后清除fastestmirror和metalink缓存

sudo yum clean metadata plugins
sudo yum -y update

gitlab安装及使用

操作系统是ubuntu12.04,先下载gitlab deb,https://www.gitlab.com/downloads/

参考地址:https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

apt-get install postfix
wget https://downloads-packages.s3.amazonaws.com/gitlab_6.7.2-omnibus-1.ubuntu.12.04_amd64.deb
sudo dpkg -i gitlab_6.7.2-omnibus-1.ubuntu.12.04_amd64.deb
sudo gitlab-ctl reconfigure

facebook hack 试用

fedora 20下安装hhvm

添加hhvm源:
vim /etc/yum.repos.d/hhvm.repo

[hhvm]
name=HHVM for Fedora $releasever - $basearch
baseurl=http://dl.hhvm.com/fedora/$releasever/$basearch/

更新

wget http://dl.hhvm.com/conf/hhvm.gpg.key
sudo rpm --import hhvm.gpg.key
sudo yum install hhvm

性能比较

PHP hello world

hhvm -m daemon -p 9910 t.php
ab -n10000 -c100 http://localhost:9910/t.php  # 15687/sec
ab -n10000 -c100 http://localhost/t.php # nginx+fpm: 11016/sec

Yii test

hhvm -m daemon -p 9910 yiitest/index.php 
ab -n10000 -c100 http://localhost:9910/yiitest/index.php # 4039/sec
ab -n10000 -c100 http://localhost/yiitest/index.php # nginx+fpm: 259/sec

vim hack.php

<?hh
echo 'hello';

test hack.php

hhvm -m daemon -p 9910 hack.php 
ab -n10000 -c100 http://localhost:9910/hack.php # 15169/sec 基本无变化,可能因为程序太简单