海运的博客

Nginx编译安装more_set_headers模块自定义head头信息

发布时间:May 9, 2012 // 分类:Nginx // No Comments

通过服务器的head头可以得到服务器的很多信息,这给服务器安全带来很大隐患,如:

curl -I https://www.haiyun.me
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Fri, 10 Feb 2012 10:43:42 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
location: forum.php

Nginx可以编译添加第三方模块more_set_headers来自定义或清除相关head信息。

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
wget -O header.zip --no-check-certificate https://github.com/agentzh/headers-more-nginx-module/zipball/v0.17rc1
unzip header.zip 
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-\
http_ssl_module --with-http_gzip_static_module --add-module=./agentzh-headers-more-nginx-module-3580526/
make && make install

应用示例,清除服务器及php信息,在配置文件http段添加以下:

more_clear_headers "X-Powered-By:";
more_clear_headers "Server:";

重新加载配置文件:

/etc/init.d/nginx reload

查看当前head头信息:

curl -I https://www.haiyun.me
HTTP/1.1 301 Moved Permanently
Date: Fri, 10 Feb 2012 10:58:38 GMT
Content-Type: text/html
Connection: keep-alive
location: forum.php

现在nginx及php信息都没了,当然也可自定义为其它信息。

Nginx安装字符替换模块substitutions_filter

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

substitutions_filter是Nginx的一个扩展模块,用来搜索并替换应答内容中的文本,配合nginx的反向代理有妙用哦。
添加此模块需重新编译Nginx,LNMP编译升级请参考:LNMP平滑升级
首先获取substitutions_filter:

yum -y install subversion
svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ substitutions4nginx-read-only

编译Nginx时添加:

./configure --add-module=/path/substitutions4nginx-read-only

使用语法:

subs_filter 源字符 目标字符 [gior] 
#g:替换全部
#o:替换首个
#i:不区分大小写
#r:使用正则替换

使用范例:

location / {
    subs_filter_types text/html text/css text/xml; #替换的文件类型
    subs_filter st(\d*).onovps $1.www.haiyun.me ir; #
    subs_filter www.haiyun.me www.haiyun.me;
}

注意:开启GZIP后不能替换,需关闭:

proxy_set_header Accept-Encoding "";

Nginx配置透明代理缓存服务器

发布时间:April 22, 2012 // 分类:Nginx // 5 Comments

之前有介绍过Squid构建透明代理缓存服务器Nginx就比较全能了,做为透明代理也不错。
修改Nginx配置文件,在http段添加:

proxy_cache_path /path/proxy_cache levels=1:2 keys_zone=cache_one:30m inactive=10d max_size=10g;
#定义缓存名称cache_one,2级目录,内存缓存30M,硬盘10G,10未访问内容删除
proxy_temp_path /path/proxy_tmp;

server段配置:

server {
    listen 3128;
    resolver 8.8.8.8;
    proxy_cache cache_one;
    proxy_max_temp_file_size 10m;
 
    location / {
        proxy_pass http://$host$request_uri;
        proxy_connect_timeout   60;
        proxy_send_timeout     60;
        proxy_read_timeout     60;
    }
        
         location ~ .*\.(php|jsp|cgi|asp)?$ {
                 proxy_pass http://$host$request_uri;
               }
}

Iptables配置:

iptables -t nat -A PREROUTING -i br-lan -p tcp  --dport 80 -j REDIRECT --to-ports 3128

监控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已优化设置。

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • 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 论坛没找到好方法,博...