使用 Nginx 提升网站访问速度
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/
配置Nginx
先来看一个实际的配置文件:
usernobody;#工作进程的属主
worker_processes4;#工作进程数,一般与CPU核数等同
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events{
useepoll;#Linux下性能最好的event模式
worker_connections2048;#每个工作进程允许最大的同时连接数
}
http{
includemime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr-$remote_user[$time_local]$request'
#'"$status"$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_logoff;
access_loglogs/access.log;#日志文件名
sendfileon;
#tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
includegzip.conf;
#集群中的所有后台服务器的配置信息
upstreamtomcats{
server192.168.0.11:8080weight=10;
server192.168.0.11:8081weight=10;
server192.168.0.12:8080weight=10;
server192.168.0.12:8081weight=10;
server192.168.0.13:8080weight=10;
server192.168.0.13:8081weight=10;
}
server{
listen80;#HTTP的端口
server_namelocalhost;
charsetutf-8;
#access_loglogs/host.access.logmain;
location~^/NginxStatus/{
stub_statuson;#Nginx状态监控配置
access_logoff;
}
location~^/(WEB-INF)/{
denyall;
}
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;
expires24h;
}
location/{
proxy_passhttp://tomcats;#反向代理
includeproxy.conf;
}
error_page404/html/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page502503/html/502.html;
error_page500504/50x.html;
location=/50x.html{
roothtml;
}
}
}