海运的博客

监控Nginx遇到502 Bad Gateway自动重启lnmp

发布时间:April 15, 2012 // 分类:Nginx,Shell // 1 Comment

此脚本用Curl监控Nginx网站状态,如回应502信息即重启LNMP

#!/bin/bash
website=https://www.haiyun.me #修改为您的网址
if curl -I $website|grep "HTTP/1.1 502"; then
  /root/lnmp restart
fi

添加到crontab计划任务:

crontab -e
*/5 * * * * sh /path/lnmp-502.sh #5分钟执行一次

Nginx/Lnmp错误502 Bad Gateway解决方法

发布时间:April 15, 2012 // 分类:Nginx // No Comments

由于Nginx处理php反代到后端php-cgi处理,如未收到回应会输出502 Bad Gateway,可优化配置防止出现此类情况。
也可以新建脚本监控lnmp出现502 Bad Gateway自动重启lnmp
1.调整nginx与php-cgi通信方式,默认为unix-sock,更改为较稳定的TCP方式。

#https://www.haiyun.me
sed -i 's/\/tmp\/php-cgi.sock/127.0.0.1:9000/' /usr/local/php/etc/php-fpm.conf 
#设置TCP监听127.0.0.1:9000
sed -i 's/unix:\/tmp\/php-cgi.sock/127.0.0.1:9000/' /usr/local/nginx/conf/nginx.conf
sed -i 's/unix:\/tmp\/php-cgi.sock/127.0.0.1:9000/' /usr/local/nginx/conf/vhost/*
#设置nginx反代

2.修改php-cgi进程数量,每个php-cgi进程占用15M左右内存,由于lnmp为小内存优化,默认开启5个php-cgi进程,可根据自己情况适量增加,不要让系统因为内存不足而当掉。

sed -i 's/max_children">.</max_children">8</'  /usr/local/php/etc/php-fpm.conf
#调整为8个
/root/lnmp restart #t重启lnmp

3.修改超时时间,lnmp已优化设置。

Lnmp平滑升级及修改Nginx版本号

发布时间:April 13, 2012 // 分类:Nginx // No Comments

首先下载官方stable稳定版,当前最新stable版是1.0.15。

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz
cd nginx-1.0.15

修改源码,更改nginx版本信息。

vim src/core/nginx.h
#define NGINX_VERSION      "1.0.15" #版本号可自定义修改
#define NGINX_VER          "nginx/" NGINX_VERSION #nginx可自定义修改

例如修改为:

vim src/core/nginx.h
#define NGINX_VERSION      "1.0.0" 
#define NGINX_VER          "ows/" NGINX_VERSION 

修改:

vim src/http/ngx_http_header_filter_module.c
static char ngx_http_server_string[] = "Server: nginx" CRLF;

修改:

vim src/http/ngx_http_special_response.c
"<hr><center>nginx</center>" CRLF  

优化编译,去除DEBUG。

vim auto/cc/gcc
# debug
CFLAGS="$CFLAGS -g" #注释此行或删除

升级编译最好以原来编译的参数编译,查看原先编译的参数:

/usr/local/nginx/sbin/nginx -V

编译安装:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
make #make后先不要make install
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old #备份旧程序
cp objs/nginx /usr/local/nginx/sbin/nginx #复制新程序至程序目录
/usr/local/nginx/sbin/nginx -t #测试是否有问题
make upgrade  #完成升级
/usr/local/nginx/sbin/nginx -v #查看升级后的版本号

Nginx/Apache/Lnmp网站常用日记统计命令

发布时间:April 13, 2012 // 分类:日记分析 // No Comments

Nginx配置日记格式为Apache日志格式,便于分析。
1.访问次数最多的前10个IP。

awk '{print $1}' www.haiyun.me.log|sort|uniq -c|sort -rn|head -n 10

2.访问次数最多的10个页面。

awk '{print $7}' www.haiyun.me.log|sort|uniq -c|sort -rn|head -n 10

3.访问最多的时间,取前十个。

awk '{print $4}' www.haiyun.me.log|cut -c 14-18|sort|uniq -c|sort -rn|head -n10

4.查看下载次数最多的文件,显示前10个。

awk '{print $7}' www.haiyun.me.log|awk -F '/' '{print $NF}'|sort|uniq -c|sort -rn|head -n 10
#如统计请求链接去除awk -F '/' '{print $NF}'|sort|

5.统计网站流量,以M为单位。

awk '{sum+=$10} END {print sum/1024/1024}' www.haiyun.me.log

6.统计IP平均流量、总流量。

awk 'BEGIN {print"ip average total"}{a[$1]+=$10;b[$1]++}END{for(i in a)print i,a[i]/1024/1024/b[i]"MB",\
a[i]/1024/1024"MB"}' www.haiyun.me.log |column -t

7.用sed统计特定时间内日志,配合以上使用awk分析。

sed -n '/10\/Feb\/2012:18:[0-9][0-9]:[0-9][0-9]/,$p' www.haiyun.me.log
#截取二月10号18点后所有日志
sed -n '/10\/Feb\/2012:18:[0-9][0-9]:[0-9][0-9]/,/10\/Feb\/2012:20:[0-9][0-9]:[0-9][0-9]/p' \
www.haiyun.me.log
#截取二月10号18点到20点之间日志

8.统计404或403最多的网址。

awk '$9 ~ /403/ {print $7}' www.haiyun.me.log|sort|uniq -c|sort -rn|head -n 80
awk '$9 ~ /404/ {print $7}' www.haiyun.me.log|sort|uniq -c|sort -rn|head -n 80

LNMP一键包基本使用教程

发布时间:September 12, 2011 // 分类:Nginx // No Comments

Nginx新建虚拟主机:

/root/vhost.sh
Please input domain:
(Default domain: www.lnmp.org):www.haiyun.me #输入新建虚拟机域名
===========================
domain=www.haiyun.me
===========================
Do you want to add more domain name? (y/n) #是否添加其它域名y或n
y
Type domainname,example(www.haiyun.me):
www.haiyun.me
===========================
domain list=www.haiyun.me
===========================
Please input the directory for the domain:www.haiyun.me :
(Default directory: /home/wwwroot/www.haiyun.me): #网站默认目录
Allow Rewrite rule? (y/n) #是否添加伪静态支持y或n
Please input the rewrite of programme :
wordpress,discuz,typecho,sablog,dabr #这些程序有默认伪静态规则
(Default rewrite: other): #输入该虚拟机伪静态文件名称,如discuz
Allow access_log? (y/n) #是否开启日记
Type access_log name(Default access log file:www.haiyun.me.log): #日记文件目录
最后按确认键创建完成。

LNMP管理:

/root/lnmp stop #停止lnmp
/root/lnmp start #启动lnmp
/root/lnmp restart #重启lnmp
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...