Nginx配置文件详细说明
最近在研究负载均衡,今天终于把环境搭好了,在此记录下Nginx服务器搭建方法以及 nginx.conf 的配置文件说明, 部分注释收集与网络.
本文主要帮助大家能快速搭建一个可用的负载均衡环境.
首先是需要JBOSS服务器若干,具体搭建方法在此不做描述.
安装nginx,
shell代码
shell>> cd /opt
shell>> wget http://nginx.org/download/nginx-1.0.6.tar.gz
shell>> tar xzvf nginx-0.7.64.tar.gz
shell>> cd nginx-1.0.6
shell>> ./configure
--user=nginx --group=nginx --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module
这里解释一下:
# --user 是指启用程序所属用户
# --group 是指启动程序所属组
# --prefix 是指nginx安装目录(不是源代码目录)
# --sbin-path 是指nginx命令位置
# --conf-path 是指配置文件路径
# --error-log-path 是错误日志路径
# --http-log-path 是访问日志 其他是一些临时文件的路径和做linux service需要用到的文件
# --with-http_stub_status_module 需要监控服务需安装此监控状态模块
然后:
shell>> cd /opt/nginx
shell>> make
shell>> make install
到此为止,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. 网络配置
#!/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
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复制问题
以前用apache做负载均衡的时候,是选择了用 session sticky的模式,这样的话,用户每次进来都会是同一个服务器中的session,不会被转发到其他的服务器上。在这样的情况下,tomcat即使不做session复制也不会影响用户访问。但是nginx并不支持sticky功能。所以必须要做session复制。否则很多地方就根本没法用。比如登录过程,先等到了第一个tomcat上,产生了一个session,在刷新页面,刷到另外一个tomcat的机器上,没有这个session,就会出现问题了。所以程序员在写jsp的时候也要注意这一点
举个简单的例子,比如我们在单机应用情况下修改SESSION中用户的某一个数据,那么通常就是:
User user = (User)request.getSession().getAttribute(“user”);
User.setName(“my name”);
这样我们就是直接存取出来,然后进行修改,虽然在单机情况下没有问题,但是在集群条件下,这样就导致了多台WEB服务器上的SESSION不同步的问题,因为SESSION并没有改变,Tomcat无法监视session中某个数据的值是否发生了变化。因此,我们还需要执行如下操作以保证SESSION的同步:
Request.getSession().setAttribute(“user”, user);
所以,我们在操作SESSION的时候要特别注意!另外的建议就是,我们应该尽可能的不要修改SESSION中的数据。
可能经常会遇到session复制不正常的情况。除了在服务端找原因再也程序上找下原因。都是有可能导致session复制不正常的
2.页面同步
为确保后面tomcat的服务器上的页面程序是一致的,可以采用如下方式
a,rsync同步,或者做成页面按钮,提供给编辑,修改了程序即使点击同步
b,共享区域存储,或者采取drbd网络raid模式
3,确认nginx可以转发成功
在nginx上wget一下后面转发的url(包过端口),如果可以打开,那就可以转发过去。如果不能打开,则无法转发
关于nginx配置文件的详细说明及配置方法,会在后续博文中继续和大家一起探讨.