|
最近在研究负载均衡,今天终于把环境搭好了,在此记录下Nginx服务器搭建方法以及 nginx.conf 的配置文件说明, 部分注释收集与网络.
本文主要帮助大家能快速搭建一个可用的负载均衡环境. 首先是需要JBOSS服务器若干,具体搭建方法在此不做描述.
到此为止,NGINX已经可以正常启动了,我们可以cd到nginx安装目录执行 ./sbin/nginx 启动nginx. 但是我们如果想要把nginx做成一个服务,必须要写一个shell. 简单说一下, # chkconfig: - 85 15 所有运行级别,启动优先级85, 关闭优先级15 # processname: 进程名称 # config: nginx配置文件位置 # config: 系统会优先找第一个,如果找不到再去找第二个 # pidfile: 进程ID存放文件,用来存放程序启动后的进程ID # Source function library. linux常用的方法库,有兴趣可以去看看service XXX status 就使用了里边的一个方法 # Source networking configuration. 网络配置
[plain] view plaincopyprint?
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# by haitao.tu at 2009-12-15
# email:tuhaitao@foxmail.com
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n {1}quot;Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n {1}quot;Stopping $prog: "
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || configtest || return 6
stop
start
}
reload() {
configtest_q || configtest || return 6
echo -n {1}quot;Reloading $prog: "
killproc $nginx -HUP
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
configtest >/dev/null 2>&1
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
local pidfile="/var/run/${prog}.pid"
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || configtest || return 6
echo -n {1}quot;Staring new master $prog: "
killproc $nginx -USR2
retval=$?
echo
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
echo -n {1}quot;Graceful shutdown of old $prog: "
killproc -p ${oldbin_pidfile} -QUIT
retval=$?
echo
return 0
else
echo {1}quot;Something bad happened, manual intervention required, maybe restart?"
return 1
fi
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo {1}quot;Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart}"
exit 2
esac
好了,已经做成chkconfig了,下边需要配置一下nginx反向代理,需要修改/usr/local/nginx/nginx.conf配置文件
[plain] view plaincopyprint?
user nginx;
worker_processes 2;
error_log /log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /run/nginx.pid;
events {
use epoll; # 采用epoll,是linux下最快的event
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
#反向代理配置 ,向内网6台jboss转发
upstream jboss {
server 192.168.162.35:8080 weight=10;
server 192.168.162.11:8080 weight=8;
server 192.168.162.61:8080 weight=2;
}
server {
listen 2011;
server_name localhost;
location ~ ^/nginx_status/ {
stub_status on;
access_log off;
}
location / {
proxy_pass http://jboss;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
OK, 没什么大问题的话, 你的nginx已经具体负载均衡的功能了,
可能存在的问题 1, session复制问题
2.页面同步
3,确认nginx可以转发成功
关于nginx配置文件的详细说明及配置方法,会在后续博文中继续和大家一起探讨. |
