增加Apache2和Nginx的header长度限制

nginx默认的header长度上限是4k,如果超过了这个值
nginx会直接返回400错误

[error] 16613#0: *105 upstream sent too big header while reading response header from upstream

可以通过以下2个参数来调整header上限

client_header_buffer_size 16k;
large_client_header_buffers 4 16k;

看起来是,nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取 Continue reading

隐藏 Apache & PHP 的版本号

有朋友问起,如何隐藏 HTTP header 中发送包含在 Server 信息里面的 Apache 和 PHP 版本号(譬如我们可以到 Firefox 的附加工具里面找 Live HTTP Headers;还可以用curl -I IPaddress|http://域名 ) 下面是做法:
Apache:
打开 httpd.conf,在文件最后加入以下代码:

  1. #Hidden I can with apache version number
  2. ServerTokens ProductOnly
  3. ServerSignature Off

PHP:
隐藏 PHP 版本就是隐藏类似于 “X-Powered-By: PHP/5.1.2-1+b1” 这个,开启 php.ini,加入:

  1. expose_php = Off

设置了expose_php=Off后,用phpinfo查看的时候,页面上原来正常显示的图片会消失隐藏的.

相关:http://blog.haohtml.com/archives/10761

apache的日志级别和设置

1.emerg
紧急 – 系统无法使用。
“Child cannot open lock file. Exiting”
2.alert
必须立即采取措施。
“getpwuid: couldn’t determine user name from uid”
3.crit
致命情况。
“socket: Failed to get a socket, exiting child”
4.error
错误情况。
“Premature end of script headers”
5.warn
警告情况。
“child process 1234 did not exit, sending another SIGHUP”
6.notice
一般重要情况。
“httpd: caught SIGBUS, attempting to dump core in …”
7.info
普通信息。
“Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)…”
8.debug
出错级别信息
“Opening config file …”
当指定了特定级别时,任何级别高于他的信息也会同时报告。比如说,当指定了LogLevel info时,任何 notice和warn级别的信息也会被记录。
建议至少要使用crit级别。

解决Apache出现的CPU高占用率的问题

所谓Apache出现CPU高占用率就是指Apache在一段时间内持续占用很高的CPU使用率,甚至达到CPU100%,这个时候造成网站无法访问。解决的方法就是仔细观察Apache的日志文件,查阅错误的信息。

我个人试了一下启用了

EnableSendfile Off

暂时解决了,

下面我们针对几种错误信息进行分析并给出解决的方法:

1. Apache与WinSock v2相冲突
Apache官方提供的手册中提到,在Windows系统下Apache2.x为了提高性能而使用了Microsoft WinSock v2 API,但是一些常见的防火墙软件会破坏他的正确性,从而使得Apache出现死循环操作造成CPU100%。

其错误提示如下所示:

[error] (730038)An operation was attempted on something that is not a socket.: winnt_accept: AcceptEx failed. Attempting to recover.

[error] (OS 10038) : Child 3356: Encountered too many errors accepting client connections. Possible causes: dynamic address renewal, or incompatible VPN or firewall software. Try using the Win32DisableAcceptEx directive.

[warn] (OS 121)信号灯超时时间已到。 : winnt_accept: Asynchronous AcceptEx failed.

[warn] (OS 64)指定的网络名不再可用。 : winnt_accept: Asynchronous AcceptEx failed.

可以依次采用下面的方法来解决上面的问题,如果进行了一步还有问题就继续下一步: Continue reading

Apache防盗链技术实现攻略

国内网站盗链成风,最倒霉的就是咱们这种使用付费主机有流量限制的个人了。没办法,只得加上了一个简单的反盗链(Anti-Leech)措施。我的服务器是 Apache,处理防盗链比较简单,解决之后,于是写下这篇笔记。

一、 使用 rewrite 技术实现 Apache 防盗链

Apache 防盗链的第一种实现方法,可以用 rewrite 实现。首先要确认 Apache 的 rewrite module 可用:能够控制 Apache httpd.conf 文件的,打开 httpd.conf,确保有这么一行配置:

LoadModule rewrite_module modules/mod_rewrite.so

然后在找到自己网站对应的 配置的地方,加入下列代码:

ServerName haohtml.com

# 防盗链配置
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://haohtml.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://haohtml.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.haohtml.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.haohtml.com$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.haohtml.com/about/nolink.png [R,NC]

防盗链配置的说明:

1. 红色部分: 表示自己的信任站点。对我的站点来说,设置为 http://www.haohtml.com 和 http://haohtml.com Continue reading

Apache图片防盗链

最近发现论坛的流量大涨,查后发现很多地方在调用论坛的图片,造成无用的流量,加大了服务器的负载,决定给CU论坛加上了防盗链图片功能

SetEnvIfNoCase Referer “chinaunix.net” local_ref=1

Order Allow,Deny
Allow from env=local_ref

外部网站调用论坛的图片会跳转到另一张图:
ErrorDocument 403 http://www.chinaunix.net/error.gif

下边转贴一篇老外写的文章,有时间再翻译

Preventing Image ‘Theft’ Continue reading

apache自动生成目录文件列表配置

由于种种原因,服务器给卖掉了,因此,我做的源码目录也就没了。这里把该目录的配置属性介绍一下:

  1. <directory /home/sunboyu/www_source.sunboyu.cn>
  2. MaxConnPerIP 1
  3. ReadmeName foot.html        #页面的头
  4. HeaderName top.html          #页面的脚
  5. IndexIgnore top.html foot.html    #列目录的时候,忽略这些文件
  6. ServerSignature Off       #关闭服务器标志
  7. IndexOptions FancyIndexing VersionSort FoldersFirst SuppressLastModified NameWidth=* Charset=UTF-8
  8. Options FollowSymLinks
  9. Options +Indexes
  10. AllowOverride all
  11. Order deny,allow
  12. Allow from all
  13. </directory>

FancyIndexing 打开列表功能

VersionSort 同一文件不同版本分类列出

FoldersFirst 文件夹优先 Continue reading