Linux环境下使用Nginx做双机热备(glassfish)

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:758

[backcolor=inherit][color=#262626][backcolor=rgb(243, 152, 0)]Linux环境下使用Nginx做双机热备
[backcolor=inherit]  [color=#262626][backcolor=inherit]Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
[backcolor=inherit][color=#262626][backcolor=inherit]  反向代理服务器:反向代理(Reverse Proxy)方式是指以代理服务器来接受internet/内网上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
[backcolor=inherit][color=#262626][backcolor=inherit]测试环境
[backcolor=inherit][table=98%,inherit]
[tr=inherit][backcolor=inherit]服务器用途
[backcolor=inherit]os版本
安装soft[backcolor=inherit]IP地址

[tr=inherit][backcolor=inherit]Nginx前置代理服务器
[backcolor=inherit]Redhat5.7_86_64
[backcolor=inherit]Nginx-1.6.0
[backcolor=inherit]192.168.1.145

[tr=inherit][backcolor=inherit]平台A机
[backcolor=inherit]Redhat5.4_86_64
glassfish\oracle\jdk[backcolor=inherit]192.168.1.88

[tr=inherit][backcolor=inherit]平台B机
[backcolor=inherit]Redhat5.4_86_64
[backcolor=inherit]glassfish\oracle\jdk
[backcolor=inherit]192.168.1.117

[backcolor=rgb(143, 196, 31)]一、Nginx安装
[backcolor=inherit][backcolor=inherit][size=13px]安装PCRE(Perl Compatible Regular Expressions)包[backcolor=rgb(254, 254, 242)][size=13px]为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包。[backcolor=rgb(254, 254, 242)][size=13px]安装GCC(GNU Compiler Collection,GNU编译器套装)[backcolor=rgb(254, 254, 242)][size=13px]系统光盘上面找安装GCC相关的依赖包及GCC包,包名及安装顺序如下:[table=305,inherit]
[tr=inherit][backcolor=inherit][backcolor=rgb(254, 254, 242)][size=13px]1.kernel-headers-2.6.18-274.el5.x86_64 .rpm[backcolor=rgb(254, 254, 242)][size=13px]2.glibc-headers-2.5-65.x86_64 .rpm[backcolor=rgb(254, 254, 242)][size=13px]3.glibc-devel-2.5-65.x86_64 .rpm[backcolor=rgb(254, 254, 242)][size=13px]4.gcc-4.1.2-51.el5.x86_64.rpm

[backcolor=rgb(254, 254, 242)][size=13px]安装命令:[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]$rpm -ivh 包名[backcolor=rgb(254, 254, 242)][size=13px]系统光盘上面找安装GCC-C++相关的依赖包及GCC-C++包,包名及安装顺序如下:[table=304,inherit]
[tr=inherit][backcolor=inherit][backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]1.libstdc++-devel-4.1.2-51.el5.x86_64[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]2.gcc-c++-4.1.2-51.el5.x86_64

[backcolor=rgb(254, 254, 242)][size=13px]解压PCRE[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#tar -xvf pcre-8.35.tar.gz[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#cd pcre-8.35[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]检测安装环境是否达到安装要求[backcolor=rgb(254, 254, 242)][size=13px][color=#262626][backcolor=inherit]#./configure[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]编译安装[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#make[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#make install[backcolor=inherit][size=13px][backcolor=inherit]安装nginx依赖包,包名及安装顺序如下:[table=299,inherit]
[tr=inherit][backcolor=inherit][backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]1.e2fsprogs-devel-1.39-33.el5.x86_64.rpm[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]2.keyutils-libs-devel-1.2-1.el5.x86_64.rpm [backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]3.libsepol-devel-1.15.2-3.el5.x86_64.rpm[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]4.libselinux-devel-1.33.4-5.7.el5.x86_64.rpm [backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]5.krb5-devel-1.6.1-62.el5.x86_64.rpm[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]6.openssl-0.9.8e-20.el5.x86_64.rpm [backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]7.openssl-devel-0.9.8e-20.el5.x86_64.rpm [backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]8.pcre-devel-6.6-6.el5_6.1.x86_64.rpm

编译安装Nginx
[backcolor=rgb(254, 254, 242)][size=13px]#mkdir -p /usr/local/nginx[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]# ./configure --with-http_stub_status_module [color=#e60013][backcolor=inherit]--prefix=/usr/local/nginx/[backcolor=inherit] [color=#e60013][backcolor=inherit]#定义安装路径为/usr/local/nginx[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]编译安装 [backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#make[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]#make install[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]Nginx服务器搭建完毕后,可在浏览器里面输入http://IP验证Nginx是否安装成功[backcolor=inherit]二、nginx配置[backcolor=inherit]Nginx的配置文件在安装目录下的conf文件夹下面的nginx.conf,此处示例地址为/usr/local/nginx/conf,[backcolor=rgb(254, 254, 242)][size=13px][backcolor=inherit]代码示例,红色字体为需要修改或添加的部分:[table=98%,inherit]
[tr=inherit][backcolor=inherit]
#user nobody;
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;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
[color=#e60013][backcolor=inherit]keepalive_timeout 65;

#gzip on;

[color=#e60013][backcolor=inherit]upstream glassfish {
[backcolor=inherit][color=#e60013][backcolor=inherit]server 192.168.1.117:8080 weight=10;
server 192.168.1.88:8080 weight=10;
}
[color=#e60013][backcolor=inherit]
server {
listen 80;
server_name 192.168.1.105;

#charset koi8-r;

#access_log logs/host.access.log main;

[color=#e60013][backcolor=inherit]location / {
root html;
index index.html index.htm;
proxy_pass [color=rgb(230, 0, 19)][backcolor=inherit]http//:[color=rgb(230, 0, 19)][backcolor=inherit]glassfish[color=rgb(230, 0, 19)][backcolor=inherit];
[backcolor=inherit][color=#e60013][backcolor=inherit]proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}

#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 [backcolor=inherit]http://127.0.0.1/]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 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

[backcolor=inherit][color=#262626][backcolor=inherit]keepalive_timeout 65; [color=#8fc41f][backcolor=inherit] #删除该行
[color=#262626][backcolor=inherit]upstream glassfish { [color=#8fc41f][backcolor=inherit] #“[backcolor=inherit]glassfish[backcolor=inherit]”代表Nginx主机名,可自定义
[backcolor=inherit][color=#262626][backcolor=inherit]server 192.168.1.117:8080 weight=10; [color=#8fc41f][backcolor=inherit] #两台短信平台主机的ip地址及访问端口
[color=#262626][backcolor=inherit]server 192.168.1.88:8080 weight=10; [color=#8fc41f][backcolor=inherit]#weight=10表示权重,两台都是10表示两台主机被分配访问的概率是一样的
[backcolor=inherit][color=#262626][backcolor=inherit]server {
listen 80; [color=#8fc41f][backcolor=inherit]#Nginx服务器访问的端口
[color=#262626][backcolor=inherit]server_name 192.168.1.105; [color=#8fc41f][backcolor=inherit]#Nginx服务器主机名或IP地址
[backcolor=inherit][color=#262626][backcolor=inherit]location / {
root html;
index index.html index.htm;
proxy_pass http//:glassfish; [color=#8fc41f][backcolor=inherit] #主机名,与上面的“upstream glassfish {”值相等
[color=#262626][backcolor=inherit]proxy_set_header Host $host; [color=#8fc41f][backcolor=inherit]#[backcolor=inherit][backcolor=inherit]反向代理服务器会向后端真实服务器发送请求,请求头中的host字段应为proxy_pass指令设置的服务器
[color=#262626][backcolor=inherit]proxy_set_header X-Real-IP $remote_addr; [color=#8fc41f][backcolor=inherit] #同上,向后方真实服务器传递真实访问IP地址
[color=#262626][backcolor=inherit]proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;[color=#8fc41f][backcolor=inherit] #[size=13px]Nginx做反向代理和负载均衡时“[size=13px]X-Forwarded-For[size=13px]”信息头的处理
[color=#262626][backcolor=inherit]proxy_connect_timeout 1; [color=#8fc41f][backcolor=inherit] #当其中一台glassfish宕机时,访问超时1秒后会连接到另外一台服务器
[color=#262626][backcolor=inherit]proxy_read_timeout 1;
[color=#262626][backcolor=inherit]proxy_send_timeout 1;
[color=#262626][backcolor=inherit]}
[backcolor=inherit]三、nginx命令

[backcolor=inherit][backcolor=rgb(89, 159, 254)]1、查看nginx进程
[backcolor=inherit][backcolor=inherit]#ps -ef|grep nginx
[backcolor=inherit][backcolor=inherit]说明:nginx运行时会有两个进程,一个是主进程,一个是工作进程,如下图,第一行是主进程,第二行是工作进程;工作线程的数量跟nginx.conf中的配置参数worker_processes有关,而worker_processes的值与服务器CPU数量相等。
[backcolor=inherit][img]file:///C:/Users/Administrator/AppData/Local/YNote/Data/song-yuxiang@foxmail.com/ec913f6536cd4f0092b1579c054ea3f8/clipboard.png[/img]
[backcolor=inherit][color=#262626][backcolor=rgb(89, 159, 254)]2、启动nginx
[backcolor=inherit][color=#262626][backcolor=inherit]nginx的启动文件位于安装目录下的sbin目录下,改目录下只有一个启动文件
[backcolor=inherit][color=#262626][backcolor=inherit]#cd /usr/local/nginx/sbin
[backcolor=inherit][color=#262626][backcolor=inherit]#./nginx
[backcolor=inherit][color=#262626][backcolor=rgb(89, 159, 254)]3、重启、平滑启动nginx
[backcolor=inherit][color=#262626][backcolor=inherit]#kill -HUP `cat /usr/local/logs/nginx.pid`
或者
[color=#262626][backcolor=inherit]#nginx -s reload

[backcolor=inherit]说明:平滑启动的意思是在不停止nginx的情况下,重启nginx,重新加载配置文件,启动新的工作线程,完美停止旧的工作线程。
[backcolor=inherit][color=#262626][backcolor=inherit]安装目录下的nginx.pid存储目前nginx的工作进程,[color=#e6013]上面命令里面的"`"号为esc键下的~键,不是单引号。
[backcolor=inherit][backcolor=rgb(89, 159, 254)][color=#262626][backcolor=inherit]4、完美停止nginx
[backcolor=inherit][color=#262626][backcolor=inherit]#kill -quit `cat /usr/local/nginx/logs/nginx.pid `
[backcolor=inherit][color=#262626][backcolor=inherit]说明:[color=#262626]/usr/local/nginx/logs/nginx.pid文件里面存储nginx的进程ID,[color=#e60013][backcolor=inherit]上面命令里面的"`"号为esc键下的~键,不是单引号。
[backcolor=inherit]四、nginx添加到开机启动、系统服务

[backcolor=inherit]#cd /etc/rc.d/init.d
[backcolor=inherit]#touch nginx
[backcolor=inherit]#vim nginx
[backcolor=inherit]贴入以下代码
[backcolor=inherit][table=98%,inherit]
[tr=inherit][backcolor=inherit]#!/bin/sh[backcolor=inherit]# chkconfig: 2345 90 10[backcolor=inherit]# description: nginx management[backcolor=inherit]# processname: nginx[backcolor=inherit]NGINXPATH=/usr/local/nginx/sbin[backcolor=inherit]NGINXPID=/usr/local/nginx/sbin/nginx.pid[backcolor=inherit]export NGINXPATH[backcolor=inherit]case $1 in[backcolor=inherit]start)[backcolor=inherit]echo 'begin to start nginx...'[backcolor=inherit]cd ${NGINXPATH}[backcolor=inherit]nohup ./nginx > /dev/null &[backcolor=inherit]sleep 2[backcolor=inherit]echo 'Done!'[backcolor=inherit];;[backcolor=inherit]stop)[backcolor=inherit]echo "begin to stop nginx..."[backcolor=inherit]nohup kill -quit `cat /usr/local/nginx/logs/nginx.pid` > /dev/null 2>&1[backcolor=inherit]sleep 2[backcolor=inherit]echo 'Done!'[backcolor=inherit];;[backcolor=inherit]restart)[backcolor=inherit]echo "begin to stop nginx..."[backcolor=inherit]nohup kill -quit `cat /usr/local/nginx/logs/nginx.pid` > /dev/null 2>&1[backcolor=inherit]sleep 2[backcolor=inherit]echo 'begin to start nginx...'[backcolor=inherit]cd ${NGINXPATH}[backcolor=inherit]nohup ./nginx > /dev/null &[backcolor=inherit]sleep 2[backcolor=inherit]echo 'Done!'[backcolor=inherit];;[backcolor=inherit]*)[backcolor=inherit]echo $"Usage:$0{start|stop|restart}"[backcolor=inherit]exit 1[backcolor=inherit]esac

[backcolor=inherit]#chmod u+x nginx
[backcolor=inherit]#chkconfig --add nginx
[backcolor=inherit]#chkconfig --list nginx
[backcolor=inherit]执行以下命令查看添加服务是否成功
[backcolor=inherit]#service nginx start
[backcolor=inherit]#service nginx stop
[backcolor=inherit]#service nginx reatart

本文标签:
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】