W一:HTTP服务?/span>
因tomcat处理静态资源的速度比较慢,所以首先想到的是把所有静态资?JS,CSS,image,swf)
提到单独的服务器Q用更加快速的HTTP服务器,q里选择了nginx了,nginx相比apacheQ更加轻量Q?/p>
配置更加单,而且nginx不仅仅是高性能的HTTP服务器,q是高性能的反向代理服务器?/p>
目前很多大型|站都用了nginx,新浪、网易、QQ{都使用了nginxQ说明nginx的稳定性和性能q是非常不错的?/p>
1. nginx 安装(linux)
http://nginx.org/en/download.html 下蝲最新稳定版?/p>
Ҏ自己需要的功能先下载对应模板,q里下蝲了下面几个模块:
openssl-0.9.8lQzlib-1.2.3Qpcre-8.00
~译安装nginx:
./configure
--without-http_rewrite_module
--with-http_ssl_module
--with-openssl=../../lib/openssl-0.9.8l
--with-zlib=../../lib/zlib-1.2.3
--with-pcre=../../lib/pcre-8.00
--prefix=/usr/local/nginx
make
make install
2、nginx处理静态资源的配置
#启动GZIP压羃CSS和JS
gzip on;
# 压羃U别 1-9,默认?Q别越高压~率大Q当然压~时间也p?/p>
gzip_comp_level 4;
# 压羃cd
gzip_types text/css application/x-javascript;
# 定义静态资源访问的服务Q对应的域名:res.abc.com
server {
listen 80;
server_name res.abc.com;
# 开启服务器d文g的缓存,
open_file_cache max=200 inactive=2h;
open_file_cache_valid 3h;
open_file_cache_errors off;
charset utf-8;
# 判断如果是图片或swfQ客L~存5?/p>
location ~* ^.+.(ico|gif|bmp|jpg|jpeg|png|swf)$ {
root /usr/local/resource/;
access_log off;
index index.html index.htm;
expires 5d;
}
# 因JS,CSS改动比较频繁Q客L~存8时
location ~* ^.+.(js|css)$ {
root /usr/local/resource/;
access_log off;
index index.html index.htm;
expires 8h;
}
# 其他静态资?/p>
location / {
root /usr/local/resource;
access_log off;
expires 8h;
}
}
3、nginx 反向代理讄
# 反向代理服务Q绑定域名www.abc.com
server {
listen 80;
server_name www.abc.com;
charset utf-8;
# BBS使用Discuz!
# 因反向代理ؓ了提高性能Q一部分http头部信息不会转发l后台的服务器,
# 使用proxy_pass_header ?proxy_set_header 把有需要的http头部信息转发l后台服务器
location ^~ /bbs/ {
root html;
access_log off;
index index.php;
# 转发host的信息,如果不设|host,在后C用request.getServerName()取到的域名不是www.abc.comQ而是127.0.0.1
proxy_set_header Host $host;
# 因Discuz! Z安全Q需要获取客LUser-Agent来判断每ơPOST数据是否跟第一ơ请求来自同1个浏览器Q?/p>
# 如果不{发User-Agent,Discuz! 提交数据׃?您的h来\不正,无法提交"的错?/p>
proxy_pass_header User-Agent;
proxy_pass http://127.0.0.1:8081;
}
# 其他h转发ltomcat
location / {
root html;
access_log off;
index index.jsp;
proxy_pass http://127.0.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx详细配置参考:http://wiki.nginx.org/
PSQ如果安装提CGCC not foundQ运行下面命令安装就可以(apt-get install build-essential)Q仅限debian