RedHat Linux AS 5下Nginx的安装及Nginx+Tomcat负载均衡配置
一、软件准备
zlib-1.2.5.tar.gz[支持gzip],下载地址:http://www.zlib.net/
pcre-8.10.tar.gz[支持rewrite module],下载地址:http://sourceforge.net/projects/pcre/files/
openssl-1.0.0a.tar.tar[支持ssl],下载地址:http://www.openssl.org/source/
下载LATEST版本
nginx-0.8.53.tar.gz,下载地址:http://nginx.org/en/download.html
二、安装过程
由于ssl暂时没有用到,所以没有安装
1、解压缩相关软件
[root@localhost tools]# tar zxvf zlib-1.2.5.tar.gz
[root@localhost tools]# tar zxvf pcre-8.10.tar.gz
[root@localhost tools]# tar zxvf nginx-0.8.53.tar.gz
2、具体安装
[root@localhost tools]# cd nginx-0.8.53
[root@localhost nginx-0.8.53]# ./configure --prefix=/opt/nginx
--with-http_realip_module --with-http_sub_module --with-http_flv_module
--with-http_dav_module with-http_gzip_static_module --with-http_stub_status_module
--with-http_addition_module --with-pcre=/opt/tools/pcre-8.10 --with-zlib=/opt/tools/zlib-1.2.5
[root@localhost nginx-0.8.53]#make
[root@localhost nginx-0.8.53]#make install
三、Nginx+Tomcat负载均衡配置
? 1、架构描述:
???? 前端一台Nginx服务器做负载均衡,后端放两台[当然可以多台]tomcat服务器,通过
Nginx转发到后面tomcat服务器,并且做动静分离
Nginx服务器IP:192.168.11.197
Tomcat01服务器IP:192.168.11.191
Tomcat02服务器IP:192.168.11.192
?
2、修改Nginx配置文件
[root@localhost conf]# vi nginx.conf
#运行nginx所在的用户名和用户组
user? nobody nobody;?
#运行CPU个数,可以按照实际服务器来计算
worker_processes? 1;
#设定错误日志
error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
?
pid??????? logs/nginx.pid;
?
?
events {
??? #设定连接数
??? worker_connections? 1024;
}
?
?
http {
??? include?????? mime.types;
??? default_type? application/octet-stream;
??? include /opt/nginx/conf/proxy.conf;
??? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
????????????????????? '$status $body_bytes_sent "$http_referer" '
????????????????????? '"$http_user_agent" "$http_x_forwarded_for"';
?? #设定请求缓冲
?? client_header_buffer_size 1k;
?? large_client_header_buffers 4 4k;
?? #设定可以使用gzip相关参数
?? gzip on;
gzip_comp_level 7;
需要压缩的最小长度
?? gzip_min_length 1100;
?? gzip_buffers 4 8k;
?? #指定需要压缩的文件类型
?? gzip_types? text/plain application/javascript text/css text/xml
?? gzip_types text/plain;
?? output_buffers 1 32k;
?? postpone_output 1460;
? ?#设定访问日志
?? access_log? logs/access.log? main;
?? client_header_timeout 3m;
?? client_body_timeout 3m;
?? send_timeout 3m;
?? sendfile on;
?? tcp_nopush on;
?? tcp_nodelay on;
?
?
?? # sendfile??????? on;
?? #tcp_nopush???? on;
?
?? #keepalive_timeout? 0;
?? #keepalive_timeout? 65;
?
?? #gzip? on;
?? upstream tomcat_server{
???? #设定转向Server,weight代表优先级,优先级高的先访问
???? server? 192.168.11.191:8080 weight=1;
???? server? 192.168.11.192:8080 weight=2;
??? }
??? server {
??????? listen?????? 80;
??????? server_name? localhost;
??????? charset gbk;
??????? access_log? logs/host.access.log? main;
??????? location / {
??????????? root /opt/www/root
??????????? index? index.html index.htm index.jsp;
??????????? proxy_pass http://tomcat_server;
??????? }
#设定查看Nginx状态的地址(非默认安装模块,需要在编译时加上
--with-http_stub_status_module)??
location /status {??
??? stub_status??????????? on;??
??? access_log??????????? on;??
??? auth_basic??????????? "status";??
??? auth_basic_user_file?? ??conf/passwd;??
}
#访问http://192.168.11.197/status会提示输入账号
#htpasswd用法
#首先在conf/目录下建立passwd文件,具体命令:touch passwd
#htpasswd cb passwd user password
?
??????? #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav这些都是静态文件,
#但应分辨,js、css可能经常会变,过期时间应小一些,图片、
html基本不变,过期时间可以设长一些??
location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {??
??? root???????? /opt/www/root;??
??? access_log?? off;??
??? expires????? 30d;??
}??
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {??
??? root???????? /opt/www/root;? ?
??? access_log?? off;??
??? expires????? 24h;??
}??
#注:location不包括?后面带的参数,所以以上正则可以
#匹配http://192.168.11.197/image/sxxx.jpg?a=xxx
??????? #error_page? 404????????????? /404.html;
?
??????? # redirect server error pages to the static page /50x.html
???? ???#
??????? error_page?? 500 502 503 504? /50x.html;
??????? location = /50x.html {
??????????? root?? html;
??????? }
?
??????? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
??????? #
??????? #location ~ \.php$ {
??????? #??? proxy_pass?? http://127.0.0.1;
??????? #}
?
??????? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
??????? #
??????? #location ~ \.php$ {
??????? #??? root?????????? html;
??????? #??? fastcgi_pass?? 127.0.0.1:9000;
??????? #??? fastcgi_index? index.php;
??????? #??? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
??????? #??? include??????? fastcgi_params;
??????? #}
?
??????? # deny access to .htaccess files, if Apache's document root
??????? # concurs with nginx's one
??????? #
?????? ?#location ~ /\.ht {
??????? #??? deny? all;
??????? #}
??? }
?
?
??? # another virtual host using mix of IP-, name-, and port-based configuration
??? #
??? #server {
??? #??? listen?????? 8000;
??? #??? listen?????? somename:8080;
??? #??? server_name? somename? alias? another.alias;
?
??? #??? location / {
??? #??????? root?? html;
??? #??????? index? index.html index.htm;
??? #??? }
??? #}
?
?
??? # HTTPS server
??? #
??? #server {
??? #??? listen?????? 443;
??? #??? server_name? localhost;
?
??? #??? ssl????????????????? on;
??? #??? ssl_certificate????? cert.pem;
??? #??? ssl_certificate_key? cert.key;
?
??? #??? ssl_session_timeout? 5m;
?
??? #??? ssl_protocols? SSLv2 SSLv3 TLSv1;
???????? # ssl_ciphers?
ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
??? #??? ssl_prefer_server_ciphers?? on;
?
??? #??? location / {
??? #??????? root?? html;
??? #??????? index? index.html index.htm;
??? #??? }
??? #}
?
}
在conf目录下创建proxy.conf文件,文件内容为
proxy_redirect off;
proxy_set_header Host $host;
#获取真实IP
proxy_set_header X-Real-IP $remote_addr;
#获取代理者的真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
?
3、测试Nginx
测试nginx.conf文件是否正确
[root@localhost sbin]# ./nginx -t -c conf/nginx.conf
the configuration file /opt/nginx/conf/nginx.conf syntax is ok
configuration file /opt/nginx/conf/nginx.conf test is successful
Nginx启动
[root@localhost sbin]# ./nginx -c /opt/nginx/conf/nginx.conf
?
查看是否启动成功
[root@localhost sbin]# ps aux|grep nginx |grep -v grep
root????? 4612? 0.0? 0.0?? 3792?? 472 ? Ss?? Nov01?? 0:00 nginx: master process ./nginx
nobody??? 4613? 0.0? 0.1?? 4144? 1284 ???? S??? Nov01?? 0:05 nginx: worker process
?
出现上述两行代表启动成功
输入http://192.168.11.197/测试下效果吧
?
?
?
4、Nginx启动、关闭脚本
#!/bin/sh??
#??
# description: Starts, stops nginx??
#??
#chkconfig: 2345 20 80?
#dscription: Startup script for nginx webserver on CentOS. Place in /etc/init.d???
#??
# Author: Touya??
set -e??
?
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/nginx??
DESC="nginx daemon"?
NAME=nginx??
DAEMON=/opt/nginx/sbin/$NAME??
CONFIGFILE=/opt/nginx/conf/nginx.conf??
PIDFILE=/opt/nginx/log/$NAME.pid??
SCRIPTNAME=/etc/init.d/$NAME
?
# Gracefully exit if the package has been removed.??
test -x $DAEMON || exit 0?
?
d_start() {??
echo "Starting $DESC: $NAME"?
$DAEMON -c $CONFIGFILE || echo "already running"?
}??
?
d_stop() {??
echo "Stopping $DESC: $NAME"?
test -f $PIDFILE && kill -QUIT `cat $PIDFILE`??
}??
?
d_reload() {??
echo "Reloading $DESC configuration…"?
kill -HUP `cat $PIDFILE` || echo "can’t reload"?
}??
case "$1" in??
'start')??
??? d_start??
??? echo "started."?
;;??
'stop')??
??? d_stop??
??? echo "stoped."?
;;??
'reload')??
??? d_reload??
??? echo "reloaded."?
;;??
'restart')??
??? echo "Restarting $DESC: $NAME ..."?
??? d_stop??
??? # One second might not be time enough for a daemon to stop,??
??? # if this happens, d_start will fail (and dpkg will break if??
??? # the package is being upgraded). Change the timeout if needed??
??? # be, or change d_stop to have start-stop-daemon use --retry.??
??? # Notice that using --retry slows down the shutdown process somewhat.??
??? sleep 3?
??? d_start??
??? echo "done."?
;;??
'list')??
??? ps auxf | egrep '(PID|nginx)' | grep -v grep??
;;??
'test')??
??? $DAEMON -t -c $CONFIGFILE??
;;??
*)??
echo "Usage: $SCRIPTNAME {reload|list|test|start|stop|restart}" >&2?
exit 3?
;;??
esac??
exit 0?
保存文件nginx ,拷贝到/etc/init.d下,并chmod? +x? /etc/init.d/nginx
[root@localhost init.d]# chkconfig --list nginx
service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
增加后台服务
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]#
接下可以用service nginx start|restart|stop来操作你的nginx服务器(restart时重新读入config)
?