使用Nginx帮助Jboss负载

Shell代码 
  1. shell>> cd /opt  
  2. shell>> wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz  
  3. shell>> tar xzvf nginx-0.7.64.tar.gz  
  4. shell>> cd nginx-0.7.64  

    然后开始编译安装,先配置编译变量:
Shell代码 
  1. ./configure \  
  2.     --user=nginx \  
  3.     --group=nginx \  
  4.     --prefix=/opt/nginx \  
  5.     --sbin-path=/usr/sbin/nginx \  
  6.     --conf-path=/etc/nginx/nginx.conf \  
  7.     --error-log-path=/var/log/nginx/error.log \  
  8.     --http-log-path=/var/log/nginx/access.log \  
  9.     --http-client-body-temp-path=/tmp/nginx/client_body \  
  10.     --http-proxy-temp-path=/tmp/nginx/proxy \  
  11.     --http-fastcgi-temp-path=/tmp/nginx/fastcgi \  
  12.     --pid-path=/var/run/nginx.pid \  
  13.     --lock-path=/var/lock/subsys/nginx \  
  14.     --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

    然后make
Shell代码  使用Nginx帮助Jboss负载
  1. shell>> cd /opt/nginx  
  2. shell>> make  
  3. shell>> make install  



    现在已经可以通过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.  网络配置

Shell代码

  1. #!/bin/sh  
  2. #  
  3. # nginx - this script starts and stops the nginx daemon  
  4. #         by haitao.tu at 2009-12-15  
  5. #         email:tuhaitao@foxmail.com  
  6. #  
  7. # chkconfig:   - 85 15  
  8. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \  
  9. #               proxy and IMAP/POP3 proxy server  
  10. # processname: nginx  
  11. # config:      /etc/nginx/nginx.conf  
  12. # config:      /etc/sysconfig/nginx  
  13. # pidfile:     /var/run/nginx.pid  
  14.   
  15. # Source function library.  
  16. . /etc/rc.d/init.d/functions  
  17.   
  18. # Source networking configuration.  
  19. . /etc/sysconfig/network  
  20.   
  21. # Check that networking is up.  
  22. "$NETWORKING" = "no" ] && exit 0  
  23.   
  24. nginx="/usr/sbin/nginx"  
  25. prog=$(basename $nginx)  
  26.   
  27. NGINX_CONF_FILE="/etc/nginx/nginx.conf"  
  28.   
  29. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  30.   
  31. lockfile=/var/lock/subsys/nginx  
  32.   
  33. start() {  
  34.     [ -x $nginx ] || exit 5  
  35.     [ -f $NGINX_CONF_FILE ] || exit 6  
  36.     echo -n $"Starting $prog: "  
  37.     daemon $nginx -c $NGINX_CONF_FILE  
  38.     retval=$?  
  39.     echo  
  40.     [ $retval -eq 0 ] && touch $lockfile  
  41.     return $retval  
  42. }  
  43.   
  44. stop() {  
  45.     echo -n $"Stopping $prog: "  
  46.     killproc $prog  
  47.     retval=$?  
  48.     echo  
  49.     [ $retval -eq 0 ] && rm -f $lockfile  
  50.     return $retval  
  51. }  
  52.   
  53. restart() {  
  54.     configtest_q || configtest || return 6  
  55.     stop  
  56.     start  
  57. }  
  58.   
  59. reload() {  
  60.     configtest_q || configtest || return 6  
  61.     echo -n $"Reloading $prog: "  
  62.     killproc $nginx -HUP  
  63.     echo  
  64. }  
  65.   
  66. configtest() {  
  67.   $nginx -t -c $NGINX_CONF_FILE  
  68. }  
  69.   
  70. configtest_q() {  
  71.     configtest >/dev/null 2>&1  
  72. }  
  73.   
  74. rh_status() {  
  75.     status $prog  
  76. }  
  77.   
  78. rh_status_q() {  
  79.     rh_status >/dev/null 2>&1  
  80. }  
  81.   
  82. # Upgrade the binary with no downtime.  
  83. upgrade() {  
  84.     local pidfile="/var/run/${prog}.pid"  
  85.     local oldbin_pidfile="${pidfile}.oldbin"  
  86.   
  87.     configtest_q || configtest || return 6  
  88.     echo -n $"Staring new master $prog: "  
  89.     killproc $nginx -USR2  
  90.     retval=$?  
  91.     echo   
  92.     sleep 1  
  93.     if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then  
  94.         echo -n $"Graceful shutdown of old $prog: "  
  95.         killproc -p ${oldbin_pidfile} -QUIT  
  96.         retval=$?  
  97.         echo   
  98.         return 0  
  99.     else  
  100.         echo $"Something bad happened, manual intervention required, maybe restart?"  
  101.         return 1  
  102.     fi  
  103. }  
  104.   
  105. case "$1" in  
  106.     start)  
  107.         rh_status_q && exit 0  
  108.         $1  
  109.         ;;  
  110.     stop)  
  111.         rh_status_q || exit 0  
  112.         $1  
  113.         ;;  
  114.     restart|configtest)  
  115.         $1  
  116.         ;;  
  117.     force-reload|upgrade)  
  118.         rh_status_q || exit 7  
  119.         upgrade  
  120.         ;;  
  121.     reload)  
  122.         rh_status_q || exit 7  
  123.         $1  
  124.         ;;  
  125.     status|status_q)  
  126.         rh_$1  
  127.         ;;  
  128.     condrestart|try-restart)  
  129.         rh_status_q || exit 7  
  130.         restart  
  131.             ;;  
  132.     *)  
  133.         echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart}"  
  134.         exit 2  
  135. esac  


    好了,已经做成chkconfig了,下边需要配置一下nginx反向代理,需要修改/etc/nginx/nginx.conf配置文件

Shell代码
  1. user  nginx;  
  2. worker_processes  2;  
  3.   
  4. error_log   /var/log/nginx/error.log;  
  5. #error_log  /var/log/nginx/error.log  notice;  
  6. #error_log  /var/log/nginx/error.log  info;  
  7.   
  8. pid         /var/run/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     use epoll; # 采用epoll,是linux下最快的event  
  13.     worker_connections  2048;  
  14. }  
  15.   
  16.   
  17. http {  
  18.     include       mime.types;  
  19.     default_type  application/octet-stream;  
  20.     sendfile        on;  
  21.     keepalive_timeout  65;  
  22.     #gzip  on;  
  23.   
  24.     #反向代理配置 ,向内网6台jboss转发  
  25.     upstream jboss5 {  
  26.         server 172.16.201.127:8080 weight=10;  
  27.         server 172.16.201.128:8080 weight=10;  
  28.         server 172.16.201.129:8080 weight=10;  
  29.         server 172.16.201.130:8080 weight=10;  
  30.         server 172.16.201.131:8080 weight=10;  
  31.         server 172.16.201.132:8080 weight=10;  
  32.     }  
  33.   
  34.     server {  
  35.         listen       80;  
  36.         server_name  localhost;  
  37.   
  38.         location ~ ^/nginx_status/ {  
  39.             stub_status on;  
  40.             access_log off;  
  41.         }  
  42.   
  43.         location / {  
  44.             proxy_pass http://jboss5;  
  45.         }  
  46.   
  47.         error_page   500 502 503 504  /50x.html;  
  48.         location = /50x.html {  
  49.             root   html;  
  50.         }  
  51.   
  52.     }  
  53.   
  54. }  

相关推荐