优化Http header信息

curl -I yourdomain.com 能看到什么? Server: Apache xxx PHP xxx XXX xxx ,明码实价,一字排开。这是干嘛,卖菜呢?

我们不妨看看 curl -I www.google.com 结果如何:

HTTP/1.1 302 Found
Cache-Control: private
Location: http://sorry.google.com/sorry/?continue=http://www.google.com/
Date: Mon, 12 Jan 2009 06:57:41 GMT
Content-Type: text/html; charset=UTF-8
Server: GFE/1.3
Content-Length: 259

请注意这里 Google 的前端 Web Server 是 GFE/1.3 (Google Front Edge 1.3),至于它具体对应 Apache 1.3.x 还是 Windows 1.3,我们并不知晓。这样就起到了很好的信息隐藏作用,一旦网上发现 Apache 1.3.x 或者 Windows 1.3 的最新漏洞,黑客们并不会直接联想到 GFE/1.3,自然也就不会来多作尝试了。

所以,我们应该把这些不可告人的秘密都隐藏起来,哪怕放一段文字广告(如: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]),也比卖菜似的一一详细罗列版本要好。

参考解决方案:

1. Lighttpd 1.4.20

src/response.c:108 改为:

buffer_append_string_len(b, CONST_STR_LEN(“\r\nServer: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]”));

输出 Header:
HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 345
Date: Mon, 12 Jan 2009 13:54:02 GMT
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]

2. Nginx 0.7.30

src/http/ngx_http_header_filter_module.c:48-49 改为:

static char ngx_http_server_string[] = “Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]” CRLF;
static char ngx_http_server_full_string[] = “Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]” CRLF;

输出 Header:
HTTP/1.1 200 OK
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
Date: Mon, 12 Jan 2009 14:01:10 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Mon, 12 Jan 2009 14:00:56 GMT
Connection: keep-alive
Accept-Ranges: bytes

3. Cherokee 0.11.6

cherokee/version.c:93 添加:

ret = cherokee_buffer_add_str (buf, “[AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]”);
return ret;

输出 Header:
HTTP/1.1 200 OK
Connection: Keep-Alive
Keep-Alive: timeout=15
Date: Mon, 12 Jan 2009 14:54:39 GMT
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
ETag: 496b54af=703
Last-Modified: Mon, 12 Jan 2009 14:33:19 GMT
Content-Type: text/html
Content-Length: 1795

4. Apache 2.2.11

server/core.c:2784 添加:

ap_add_version_component(pconf, “[AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]”);
return;

输出 Header:
HTTP/1.1 200 OK
Date: Mon, 12 Jan 2009 14:28:10 GMT
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: “1920edd-2c-3e9564c23b600”
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html

5. Squid 3.0 STABLE 11

src/globals.cc:58 改为:

const char *const full_appname_string = “[AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]”;

输出 Header:
HTTP/1.0 400 Bad Request
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
Mime-Version: 1.0
Date: Mon, 12 Jan 2009 15:25:15 GMT
Content-Type: text/html
Content-Length: 1553
Expires: Mon, 12 Jan 2009 15:25:15 GMT
X-Squid-Error: ERR_INVALID_URL 0
X-Cache: MISS from ‘cache.hutuworm.org’
Via: 1.0 ‘cache.hutuworm.org’ ([AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ])
Proxy-Connection: close

6. Tomcat 6.0.18

java/org/apache/coyote/http11/Constants.java:56 和 java/org/apache/coyote/ajp/Constants.java:236 均改为:

ByteChunk.convertToBytes(“Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]” + CRLF);

输出 Header:
HTTP/1.1 200 OK
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
ETag: W/”7857-1216684872000″
Last-Modified: Tue, 22 Jul 2008 00:01:12 GMT
Content-Type: text/html
Content-Length: 7857
Date: Mon, 12 Jan 2009 16:30:44 GMT

7. JBoss 5.0.0 GA

a. tomcat/src/resources/web.xml:40 改为

[AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]

b. 下载 JBoss Web Server 2.1.1.GA srctar (http://www.jboss.org/jbossweb/downloads/jboss-web/)

java/org/apache/coyote/http11/Constants.java:56 和 java/org/apache/coyote/ajp/Constants.java:236 均改为:

ByteChunk.convertToBytes(“Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]” + CRLF);

将编译所得 jbossweb.jar 覆盖 JBoss 编译输出文件:

JBOSS_SRC/build/output/jboss-5.0.0.GA/server/all/deploy/jbossweb.sar/jbossweb.jar
JBOSS_SRC/build/output/jboss-5.0.0.GA/server/standard/deploy/jbossweb.sar/jbossweb.jar
JBOSS_SRC/build/output/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jbossweb.jar
JBOSS_SRC/build/output/jboss-5.0.0.GA/server/web/deploy/jbossweb.sar/jbossweb.jar

输出 Header:
HTTP/1.1 200 OK
Server: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
X-Powered-By: [AD: DangDang http://tinyurl.com/2dangdang ][ Your AD Here ][AD: Joyo http://tinyurl.com/2amazon ]
Accept-Ranges: bytes
ETag: W/”1581-1231842222000″
Last-Modified: Tue, 13 Jan 2009 10:23:42 GMT
Content-Type: text/html
Content-Length: 1581
Date: Tue, 13 Jan 2009 10:30:42 GM

Tags: ,

星期四, 11 3 月, 2010 服务器 没有评论

使用 Nginx 提升网站访问速度

本文主要介绍如何在 Linux 系统上安装高性能的 HTTP 服务器 —— Nginx、并在不改变原有网站结构的条件下用 Nginx 来提升网站的访问速度。
Nginx 简介
Nginx(“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。 Igor 将源代码以类BSD 许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
根据最新一期(08 年 6 月份)的 NetCraft 调查报告显示,已经有超过两百万的主机使用了 Nginx,这个数字超过了另外一个轻量级的 HTTP 服务器 lighttpd, 排名第四,并且发展迅速。下面是这份报告的前几名的报表:
以下为引用的内容:
产品         网站数
Apache  84,309,103
IIS  60,987,087
Google GFE  10,465,178
Unknown  4,903,174
nginx  2,125,160
Oversee  1,953,848
lighttpd  1,532,952

关于这期调查报告的更详细信息请看下面链接:
http://survey.netcraft.com/Reports/200806/
下图是最近几个月使用 Nginx 和 lighttpd 的网站数比较

图 1. 最近几个月使用 Nginx 和 lighttpd 的网站数比较

使用 Nginx 前必须了解的事项

目前官方 Nginx 并不支持 Windows,您只能在包括 Linux、UNIX、BSD 系统下安装和使用;
Nginx 本身只是一个 HTTP 和反向代理服务器,它无法像 Apache 一样通过安装各种模块来支持不同的页面脚本,例如 PHP、CGI 等;
Nginx 支持简单的负载均衡和容错;
支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有。
在 Linux 下安装 Nginx
为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包。您可以到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
# wget ftp://ftp.csx.cam.ac.uk/pub/soft … cre/pcre-7.7.tar.gz
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7
# ./configure
# make
# make install

接下来安装 Nginx,Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /opt/nginx 目录下的详细步骤:
# wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
# ./configure –with-http_stub_status_module –prefix=/opt/nginx
# make
# make install

其中参数 –with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。
安装成功后 /opt/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome tonginx! 则表示 Nginx 已经安装并运行成功。
常用的 Nginx 参数和控制
程序运行参数
Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:
-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令
sbin/nginx – t – c conf/nginx2.conf
通过信号对 Nginx 进行控制
Nginx 支持下表中的信号:
信号名                                             作用描述
TERM, INT  快速关闭程序,中止当前正在处理的请求
QUIT  处理完当前请求后,关闭程序
HUP  重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1  重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2  平滑升级可执行程序
WINCH  从容关闭工作进程
有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。
配置 Nginx
先来看一个实际的配置文件:

以下为引用的内容:
user  nobody;# 工作进程的属主
worker_processes  4;# 工作进程数,一般与 CPU 核数等同 #error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
use epoll;#Linux 下性能最好的 event 模式
worker_connections  2048;# 每个工作进程允许最大的同时连接数
}
http {
include       mime.types;
default_type  application/octet-stream;
#log_format  main  ‘$remote_addr – $remote_user [$time_local] $request ‘
#                  ‘”$status” $body_bytes_sent “$http_referer” ‘
#                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#access_log  off;
access_log  logs/access.log;# 日志文件名
sendfile        on;
#tcp_nopush     on;
tcp_nodelay     on;
keepalive_timeout  65;
include          gzip.conf;

# 集群中的所有后台服务器的配置信息
upstream tomcats {
server 192.168.0.11:8080 weight=10;
server 192.168.0.11:8081 weight=10;
server 192.168.0.12:8080 weight=10;
server 192.168.0.12:8081 weight=10;
server 192.168.0.13:8080 weight=10;
server 192.168.0.13:8081 weight=10;
}
server {
listen       80;#HTTP 的端口
server_name  localhost;
charset utf-8;
#access_log  logs/host.access.log  main;
location ~ ^/NginxStatus/ {
stub_status on; #Nginx 状态监控配置
access_log off;
}
location ~ ^/(WEB-INF)/ {
deny all;
}

location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|
zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
root /opt/webapp;
expires 24h;
}
location / {
proxy_pass http://tomcats;# 反向代理
include proxy.conf;
}
error_page 404 /html/404.html;
# redirect server error pages to the static page /50x.html
#
error_page 502 503 /html/502.html;
error_page 500 504 /50x.html;
location = /50x.html {
root   html;
}
}
}

Nginx 监控
上面是一个实际网站的配置实例,其中灰色文字为配置说明。上述配置中,首先我们定义了一个 location ~^/NginxStatus/,这样通过 http://localhost/NginxStatus/ 就可以监控到 Nginx的运行信息,显示的内容如下:
以下为引用的内容:
Active connections: 70
server accepts handled requests
14553819 14553819 19239266
Reading: 0 Writing: 3 Waiting: 67

NginxStatus 显示的内容意思如下:

active connections – 当前 Nginx 正处理的活动连接数。
erveraccepts handled requests — 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 (证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。
reading — nginx 读取到客户端的 Header 信息数。
writing — nginx 返回给客户端的 Header 信息数。
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
静态文件处理
通过正则表达式,我们可让 Nginx 识别出各种静态文件,例如 images 路径下的所有请求可以写为:
以下为引用的内容:
location ~ ^/images/ {
root /opt/webapp/images;
}
而下面的配置则定义了几种文件类型的请求处理方式。
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /opt/webapp;
expires 24h;
}

对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires:
以下为引用的内容:
expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;

动态页面请求处理
Nginx本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,例如Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx直接处理的一些静态文件请求后,其他所有的请求通过 proxy_pass 指令传送给后端的服务器(在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:
以下为引用的内容:
location / {
proxy_pass        http://localhost:8080;
proxy_set_header  X-Real-IP  $remote_addr;
}

这里我们没有使用到集群,而是将请求直接送到运行在 8080 端口的 Tomcat 服务上来完成类似 JSP 和 Servlet 的请求处理。
当页面的访问量非常大的时候,往往需要多个应用服务器来共同承担动态页面的执行操作,这时我们就需要使用集群的架构。 Nginx 通过 upstream 指令来定义一个服务器的集群,最前面那个完整的例子中我们定义了一个名为 tomcats 的集群,这个集群中包括了三台服务器共 6 个 Tomcat 服务。而 proxy_pass 指令的写法变成了:
以下为引用的内容:
location / {
proxy_pass        http://tomcats;
proxy_set_header  X-Real-IP  $remote_addr;
}

在 Nginx 的集群配置中,Nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时,或者重新起效时,Nginx 都会非常及时的处理状态的变化,以保证不会影响到用户的访问。
总结
尽管整个程序包只有五百多 K,但麻雀虽小、五脏俱全。 Nginx官方提供的各种功能模块应有尽有,结合这些模块可以完整各种各样的配置要求,例如:压缩、防盗链、集群、FastCGI、流媒体服务器、 Memcached 支持、URL 重写等等,更关键的是 Nginx 拥有 Apache 和其他 HTTP服务器无法比拟的高性能。您甚至可以在不改变原有网站的架构上,通过在前端引入 Nginx 来提升网站的访问速度。
本文只是简单介绍了 Nginx 的安装以及常见的基本的配置和使用,更多关于 Nginx 的信息请阅读文章后面的参考资源。在这里要非常感谢我的朋友——陈磊(chanix@msn.com),他一直在做 Nginx 的中文 WIKI(http://wiki.codemongers.com/NginxChs),同时也是他介绍给我这么好的一款软件。
如果您的网站是运行在 Linux 下,如果您并没有使用一些非常复杂的而且确定 Nginx 无法完成的功能,那您应该试试 Nginx 。

Tags: ,

星期四, 11 3 月, 2010 服务器 没有评论

Linux解决Too many open files的方法

Issue:
How do I set the maximum number of files allowed to be open on a system
Resolution:
The current setting for maximum number of open files can be viewed with the command:

ulimit -n

This number indicates the maximum number of files normal users (i.e. non-root) can have open in a single session. Note that for the root user, ulimit -n will sometimes output 1024 even after following the procedure to increase the maximum number of open files. This won’t effect root’s ability to open large numbers of files, as only normal users are bound by this value.

To increase the maximum number of open files beyond the default of 1024, two changes to the system may be necessary. In these examples, we will increase the maximum number of open files to the arbitrary value of 2048. All changes need to be made by the root user and users will need to log out and log back in before the changes will take effect.

1. Configure the system to accept the desired value for maximum number of open files Check the value in /proc/sys/fs/file-max to see if it is larger than the value needed for the maximum number of open files:

# cat /proc/sys/fs/file-max

If the value isn’t large enough, echo an appropriate number into the variable and add the change to /etc/sysctl.conf to make it persistent across reboots. If the number is already larger than the value you wish to use, skip to step 2.

# echo 2048 > /proc/sys/fs/file-max

and edit /etc/sysctl.conf to include the line:

fs.file-max = 2048

2. Set the value for maximum number of open files In the file /etc/security/limits.conf, below the commented line that reads

#<domain>      <type>  <item>         <value>

add this line:

* – nofile 2048

This line sets the default number of open file descriptors for every user on the system to 2048. Note that the “nofile” item has two possible limit values under the <type> header: hard and soft. Both types of limits must be set before the change in the maximum number of open files will take effect. By using the “-” character, both hard and soft limits are set simultaneously.

The hard limit represents the maximum value a soft limit may have and the soft limit represents the limit being actively enforced on the system at that time. Hard limits can be lowered by normal users, but not raised and soft limits cannot be set higher than hard limits. Only root may raise hard limits.

When increasing file limit descriptors, you may want to simply double the value. For example, if you need to increase the default value of 1024, increase the value to 2048 first. If you need to increase it again, try 4096, etc.

==================================================================
1。ulimit -a 查看系统目前资源限制的设定。
[root@test security]# ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited
[root@test security]#
通过以上命令,我们可以看到open files 的最大数为1024
那么我们可以通过一下命令修改该参数的最大值
2. ulimit -n 4096
[root@test security]# ulimit -n 4096
[root@test security]# ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 4096
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited

这样我们就修改了系统在同一时间打开文件资源的最大数,基本解决以上问题。

另外我们可以通过lsof  -p [进程的 pid]来查看系统当前打开的文件资源,可以了解不同时期系统的文件资源的使用情况,可根据情况进行系统资源的配置。

Tags: ,

星期四, 11 3 月, 2010 服务器 没有评论

在linux下如何查看和踢除正在登陆的其它用户

使用who这个命令来查看当前正在登录的用户

[root@localhost http]# who
root     tty1         Apr  9 13:17
http     pts/0        Apr 16 15:13 (192.168.8.235)
http     pts/1        Apr 16 15:13 (192.168.8.235)

上面的消息告诉我们:tty是指在任何一个虚拟控制台登录则产生一个tty,比如你插上显示器登录主机,就会增加一个tty,那pts是什么呢?pts是每一个远程连接都会产生的

要踢出某个用户以及该用户运行的程序(很野蛮),请采用如下方法:

比如:想踢除http这个用户和他的所有开启的程序执行下面命令

pkill -u http

注意:这个命令实际上很危险,要相当小心的执行!!

说他危险的原因是:该用户所有有关的程序都会被关掉,那意味着什么呢?那意味着:如何你使用http这个用户开启的apache服务的 话,apache服务业停掉了,你必须手动开启apache服务!说得更严重点:如果你不小心执行了踢出root的命令,那意味着使用root开启的 ssh服务也停止了,你必须(是的!是必须!)接显示器到服务器上了,或者你必须按一下reset键了~~

那么安全的方法是什么呢?

安全的做法是先查看终端号,然后查看该终端执行的所有进程,根据进程号来停止服务!

示例:

[root@localhost http]# who
root     tty1         Apr  9 13:17
http     pts/0        Apr 16 15:13 (192.168.8.235)
[root@localhost http]# ps -ef|grep pts/0
http     16627 16595  0 15:13 ?        00:00:00 sshd: http@pts/0
http     16628 16627  0 15:13 pts/0    00:00:00 -bash
root     16680 16628  0 15:13 pts/0    00:00:00 su
root     16681 16680  0 15:13 pts/0    00:00:00 bash
root     18089 16681  0 15:41 pts/0    00:00:00 ps -ef
root     18090 16681  0 15:41 pts/0    00:00:00 grep pts/0

如果你要终止某个进程,看准进程号,执行。比如要敲掉http的bash,看准进程号是16628

Tags: ,

星期四, 11 3 月, 2010 服务器 没有评论

Apache缓存系统

Apache 从2.0开始就已经可以使用缓存模块了,不过在2.0的时候还是实验性的,到了2.2已经完全可以放心的使用。Apache 的缓存实现主要依靠 mod_cache、mod_disk_cache、mod_file_cache 及 mod_mem_cache。只需在配置编译的时候加上参数:–enable-cache、 –enable-disk-cache、–enable-file-cache、 –enable-mem-cache 即可。
关于 Apache 的编译安装本文就不再说了,可以参考以前的文章。这里主要介绍一下如何配置使用 Apache 的缓存功能。
具体来说,Apache 的缓存方式有两种,一种是基于硬盘文件的缓存,由 mod_disk_cache 实现,另一种是使用内存缓存,由 mod_mem_cache 实现,不过它们都是依赖 mod_cache 模块的,mod_cache 模块提供了一些缓存配置的指令供它们使用,而 mod_file_cache 模块是搭配 mod_mem_cache 模块使用的,下面分别进行介绍。
1、基于硬盘文件的缓存
基于硬盘文件存储的缓存由 mod_disk_cache 模块实现,先看个简单的配置例子:

CacheDefaultExpire 86400

CacheEnable disk /
CacheRoot /tmp/apacheCache
CacheDirLevels 5
CacheDirLength 5
CacheMaxFileSize 1048576
CacheMinFileSize 10

把上面的配置加到 Apache 的 httpd.conf 文件中,如果缓存相关的模块都已经编译进了 Apache 的核心,则无需加载模块,直接就能使用上面的指令。指令的详细说明如下:
CacheDefaultExpire: 设定缓存过期的时间(秒),默认是1小时,只有当缓存的文档没有设置过期时间或最后修改时间时这个指令才会生效
CacheEnable:启用缓存,第1个参数是缓存类弄,这里当然是 disk了,第2个参数是缓存路径,指的是 url 路径,这里是缓存所有的东西,直接写上“/”即可,如“/docs”则只缓存 /docs 下的所有文件
CacheRoot:缓存文件所在的目录,运行 Apache 的用户(如daemon 或 nobody)要能对其进行读写,如果不清楚的话可以直接设置成 777,请手动建立该目录并设置好访问权限
CacheDirLevels:缓存目录的深度,默认是3,这里设置为5
CacheDirLength:缓存目录名的字符长度,默认是4,这里设置为5
CacheMaxFileSize 和 CacheMaxFileSize:缓存文件的最大值和最小值(byte),当超过这个范围时将不再缓存,这里设置为 1M 和 10bytes
基于硬盘文件存储的文件基本上就这些内容,设置好后重启 Apache 应该就能使用了。一切正常的话,可以在缓存目录下看到 Apache 自动建立的一些目录和缓存的数据文件。
2、基于内存的缓存
基于内存的缓存主要由 mod_mem_cache 模块实现,还是看个简单的配置吧,这样比较直观:-)

CacheEnable mem /
MCacheMaxObjectCount 20000
MCacheMaxObjectSize 1048576
MCacheMaxStreamingBuffer 65536
MCacheMinObjectSize 10
MCacheRemovalAlgorithm GDSF
MCacheSize 131072

简单说一下上面一些指令的意思:
CacheEnable:启用缓存,使用基于内存的方式存储
MCacheMaxObjectCount:在内存中最多能存储缓存对象的个数,默认是1009,这里设置为20000
MCacheMaxObjectSize:单个缓存对象最大为 1M,默认是10000bytes
MCacheMaxStreamingBuffer:在缓冲区最多能够放置多少的将要被缓存对象的尺寸,这里设置为 65536,该值通常小于100000或 MCacheMaxObjectSize 设置的值
MCacheMinObjectSize:单个缓存对象最小为10bytes,默认为1bytes
MCacheRemovalAlgorithm:清除缓存所使用的算法,默认是 GDSF,还有一个是LRU,可以查一下 Apache 的官方文档,上面有些介绍
MCacheSize:缓存数据最多能使用的内存,单位是 kb,默认是100kb,这里设置为128M
保存重启 Apache 基于内存的缓存系统应该就能生效了,根据需要可以使基于内存的存储或硬盘文件的存储方式一起使用,只要指明不同的URL路径即可。
3、注意事项
使用缓存需要注意如下事项:
要使用缓存,必须使用指令 CacheEnable 启用它,目前可用的缓存类型为 disk 或 mem,禁止缓存可以使用 CacheDisable,如 CacheDisable /private
待缓存的 URL 返回的状态值必须为: 200、203、300、301 或 410
URL 的请求方式必须是 GET 方式
发送请求时,头部中包含 “Authorization: ”的字符串时,返回的内容将不会被缓存
URL 包含查询字符串,如问号?后的那些东西,除非返回的内容包含“Expires:”,否则不会被缓存
如果返回的状态值是 200,则返回的头部信息必须包含以下的一种才会被缓存:Etag、Last-Modified、Expires,除非设置了指令 CacheIgnoreNoLastMod On
如果返回内容的头部信息“Cache-Control:”中包含“private”,除非设置了指令 CacheStorePrivate On,否则不会被缓存
如果返回内容的头部信息“Cache-Control:”中包含“no-sotre”,除非设置了指令 CacheStoreNoStore On,否则不会被缓存
如果返回内容的头部信息“Vary:”中包含了“*”,不会被缓存
4、其它一些指令的介绍
如果你的网站有几个文件的访问非常频繁而又不经常变动,则可以在 Apache 启动的时候就把它们的内容缓存到内存中(当然要启用内存缓存系统),使用的是 mod_file_cache 模块,具体如下:
有多个文件可以用空格格开
MMapFile /var/www/html/index.html /var/www/html/articles/index.html
上面是缓存文件的内容到内存中,除此之外,还可以只缓存文件的打开句柄到内存中,当有请求进来的时候,Apache 直接从内存中获取文件的句柄,返回内容,和 MMapFile 指令很像,具体如下:
CacheFile /var/www/html/index.html /var/www/html/articles/index.html
上面两个指令所缓存的文件如果有修改的话,必须重启 Apache 或使用 graceful 之类的方式强制使 Apache 更新缓存数据,否则当用户访问的时候获取的不是最新的数据。
有时候需要根据某些特殊的头部信息来决定是否进行缓存,则可以使用如下指令:
当头部信息中包含 Set-Cookie 时则跳过不进行缓存操作
CacheIgnoreHeaders Set-Cookie
有时候需要缓存的时候跳过 URL 中的查询字符串?使用如下指令:
CacheIgnoreQueryString On
Apache 的缓存系统不仅可以缓存服务器本身的文件,也可以缓存通过代理得到的内容,对了,Apache 可以像 Squid 一样做代理,而且做的还不错,下篇文章就介绍一下 Apache 的代理功能吧。善用 Apache 的缓存功能,可以让你的网站速度提升不少。做为一个网站来说,虽然可用的各种缓存方案很多,但在 Web 服务器层做缓存的效率还是很值得一试的。

嗯 不过常用功能 nginx都有了 而且nginx效率更高
?nginx配合tomcat、resin等java应用服务器提供java支持
首先探讨一下为什么要使用nginx:

1、类似于apache+resin,nginx用于提供静态页面服务,比java服务器要强。虽然这些java服务器的性能都不赖,tomcat新版甚至还支持了epoll,但是用nginx来处理静态文件是一定比这些服务器更稳妥并更快速的。

2、nginx在配合java应用服务器时相比apache耦合度更低,所以它可以用相同的方式连上所有能支持http服务能力的java应用服 务器,无论是tomcat、resin、weblogic、jetty、websphere……,并且可以轻易地将一个应用同时连接不相同的服务器。譬如 一台跑tomcat,一台跑resin,这一点在切换或测试生产环境时非常有用。另外耦合度的降低,将使nginx和这些服务器之间的相互影响降到最低, 这样两者就能各尽其责,apache一般是采用一个模块来和java应用服务器打交道,这个模块是会对apache的运行或多或少产生影响的,nginx 告诉我们这些模块是多么的鸡肋,java服务器要不就往fastcgi协议支持方面发展会更好。

3、nginx的一些特性可以保住脆弱的java服务器。其中可以点名的:1是可细致定制的负载均衡策略;2是超强的故障屏蔽功能可以做到不遗漏任何一个请求;3是客户端连接的异步处理;4是比java服务器强上百倍的负荷能力。

综合这些优点,一般的java应用服务器,接上一个nginx后,在服务稳定性上将可匹敌数万美金的大型java集群服务器,java开发者就不用再为java性能方面发愁了。

Tags: , ,

星期四, 11 3 月, 2010 服务器 没有评论

使用sed命令删除空格和空行

sed s/[[:space:]]//g  filename          删除空格
sed /^$/d         filename        删除空行

Tags: , ,

星期四, 11 3 月, 2010 服务器 没有评论

脚本中date的用法

man date可以看到date的help文件
#date 获取当前时间
#date -d “-1 week” +%Y%m%d 获取上周日期(day,month,year,hour)
#date –date=”-24 hour” +%Y%m%d 同上
date_now=`date +%s` shell脚本里面赋给变量值

%% 输出%符号
%a 当前域的星期缩写 (Sun..Sat)
%A 当前域的星期全写 (Sunday..Saturday)
%b 当前域的月份缩写(Jan..Dec)
%B 当前域的月份全称 (January..December)
%c 当前域的默认时间格式 (Sat Nov 04 12:02:33 EST 1989)
%C n百年 [00-99]
%d 两位的天 (01..31)
%D 短时间格式 (mm/dd/yy)
%e 短格式天 ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%h same as %b
%H 24小时制的小时 (00..23)
%I 12小时制的小时 (01..12)
%j 一年中的第几天 (001..366)
%k 短格式24小时制的小时 ( 0..23)
%l 短格式12小时制的小时 ( 1..12)
%m 双位月份 (01..12)
%M 双位分钟 (00..59)
%n 换行
%N 十亿分之一秒(000000000..999999999)
%p 大写的当前域的上下午指示 (blank in many locales)
%P 小写的当前域的上下午指示 (blank in many locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数(a GNU extension)
%S 双位秒 second (00..60);
%t 横向制表位(tab)
%T 24小时制时间表示(hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)
%U 一年中的第几周星期天为开始 (00..53)
%V 一年中的第几周星期一为开始 (01..53)
%w 一周中的第几天 星期天为开始 (0..6)
%W 一年中的第几周星期一为开始 (00..53)
%x 本地日期格式 (mm/dd/yy)
%X 本地时间格式 (%H:%M:%S)
%y 两位的年(00..99)
%Y 年 (1970…)

例子:编写shell脚本计算离自己生日还有多少天?
read -p “Input your birthday(YYYYmmdd):” date1
m=`date –date=”$date1″ +%m`    #得到生日的月
d=`date –date=”$date1″ +%d`    #得到生日的日
date_now=`date +%s`      #得到当前时间的秒值
y=`date +%Y`            #得到当前时间的年
birth=`date –date=”$y$m$d” +%s`      #得到今年的生日日期的秒值
internal=$(($birth-$date_now))       #计算今日到生日日期的间隔时间
if [ “$internal” -lt “0” ]; then           #判断今天的生日是否已过
birth=`date –date=”$(($y+1))$m$d” +%s`      #得到明天的生日日期秒值
internal=$(($birth-$date_now))        #计算今天到下一个生日的间隔时间
fi
echo “There is :$((einternal/60/60/24)) days.”       #输出结果,秒换算为天

Tags: , ,

星期四, 11 3 月, 2010 服务器 没有评论

linux登录时,/etc/profile、~/.bash_profile等文件的执行过程

在登录Linux时要执行文件的过程如下:

在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件。因为在 ~/.bash_profile文件中一般会有下面的代码:

if [ -f ~/.bashrc ] ; then

. ./bashrc

fi

~/.bashrc中,一般还会有以下代码:

if [ -f /etc/bashrc ] ; then

. /bashrc

fi

所以,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。

执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc ->          /etc/bashrc -> ~/.bash_logout

关于各个文件的作用域

(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。

(2)/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。

(3)~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。

(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。

(5)~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc /profile中的变量,他们是”父子”关系。

(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。

Tags: ,

星期四, 11 3 月, 2010 服务器 没有评论

HTTP codes

This is examples of current HTTP codes

#[Miscellaneous successes]
“2xx”, “[Miscellaneous successes]”,
“200”, “OK”, # HTTP request OK
“201”, “Created”,
“202”, “Request recorded, will be executed later”,
“203”, “Non-authoritative information”,
“204”, “Request executed”,
“205”, “Reset document”,
“206”, “Partial Content”,
#[Miscellaneous redirections]
“3xx”, “[Miscellaneous redirections]”,
“300”, “Multiple documents available”,
“301”, “Moved Permanently”,
“302”, “Found”,
“303”, “See other document”,
“304”, “Not Modified since last retrieval”, # HTTP request OK
“305”, “Use proxy”,
“306”, “Switch proxy”,
“307”, “Document moved temporarily”,
#[Miscellaneous client/user errors]
“4xx”, “[Miscellaneous client/user errors]”,
“400”, “Bad Request”,
“401”, “Unauthorized”,
“402”, “Payment required”,
“403”, “Forbidden”,
“404”, “Document Not Found”,
“405”, “Method not allowed”,
“406”, “ocument not acceptable to client”,
“407”, “Proxy authentication required”,
“408”, “Request Timeout”,
“409”, “Request conflicts with state of resource”,
“410”, “Document gone permanently”,
“411”, “Length required”,
“412”, “Precondition failed”,
“413”, “Request too long”,
“414”, “Requested filename too long”,
“415”, “Unsupported media type”,
“416”, “Requested range not valid”,
“417”, “Failed”,
#[Miscellaneous server errors]
“5xx”, “[Miscellaneous server errors]”,
“500”, “Internal server Error”,
“501”, “Not implemented”,
“502”, “Received bad response from real server”,
“503”, “Server busy”,
“504”, “Gateway timeout”,
“505”, “HTTP version not supported”,
“506”, “Redirection failed”,
#[Unknown]
“xxx” ,”[Unknown]”

Tags:

星期四, 11 3 月, 2010 网络 没有评论

N’java Music Video

Ok so, I’m sure that very little people know who N’java are. Well their a very famous group in Madagascar, Africa and two of the singers(Monika Rasoarnirina & Lala Raharimahala)sang almost all the …

Moving soulful voices of Njava carry you to another level during their Salmon Arm’s 16th Roots & Blues Festival performance.

Njava – Festival Músicas do mundo – Portugal 24.07.2009

http://www.myspace.com/njava

Tags:

星期四, 11 3 月, 2010 音乐 没有评论
1LMooBmUE153Wnd3zDryWvDyXxQudbFxDr