1、RedHat65 Linux Nginx+Tomcat负载均衡图文详解运维资料(最后修改时间:2015-6-24 15)目录Linux下Nginx+tomcat实现负载均衡 21.1 JDK安装 21.2 Tomcat安装 21.2.1 指定JDK 21.2.2 修改端口号 21.3 Nginx安装 41.3.1 安装prce包 41.3.2 安装Nginx 51.4 Nginx配置修改 61.4.1 proxy.conf 61.4.2 gzip.conf 61.4.3 nginx.conf 61.5 负载均衡测试 10Linux下Nginx+tomcat实现负载均衡Linux下利用Nginx
2、+tomcat实现负载均衡,本次配置如下10.161.200.90:nginx服务器端口:8010.161.200.91:tomcat1服务器端口:808010.161.200.92:tomcat2服务器端口:80801.1 JDK安装Linux系统在不清楚具体部署了那些程序之前最好不要用“.bin”格式的JDK安装文件自动安装。此操作会更改到其他用户默认环境。一般选择解压包方式安装,然后在应用程序上直接指定JDK路径。将jdk-7u51-linux-x64.tar.gz上传到91、92服务器并执行以下语句tarxvzf jdk-7u51-linux-x64.tar.gz为方便管理,将解压出的
3、文件夹移动到/opt下并更名为jdk1.71.2 Tomcat安装将apache-tomcat-7.0.53上传到需要应用服务器(10.161.200.91以及10.160.121.200.92)unzip apache-tomcat-7.0.53为方便管理,将解压出的apache-tomcat-7.0.53文件夹复制到/home文件夹下并更名为tomcat7_v31.2.1 指定JDK进入/home/tomcat7_v3/bin,修改catalina.sh文件如下# OS specific support. $var _must_ be set to either true or false
4、.JAVA_HOME=/opt/jdk1.7 #加入jdk路径1.2.2 修改端口号如果每个Tomcat在不同服务器上则不需要更改端口号,反之则需要修改。进入/home/tomcat7_v3/conf/ 修改server.xml文件,第一处修改(修改时最好按照tomcat顺序,不易出错):第一个tomcat默认全部不修改剩下的Tomcat依次修改为8105、8205、8305第二处修改:第一个tomcat默认全部不修改,剩下的Tomcat依次修改为(8180,8444)、(8280,8445)、(8380,8446)第三处修改:第一个tomcat默认全部不修改,剩下的Tomcat依次修改为(8
5、109,8444)、(8209,8445)、(8309,8446)第四处修改:所有得Tomcat依次修改为tomcat1、tomcat2、tomcat3完成后启动tomcat分别访问http:/localhost:prot以及可以看见tom猫表示tomcat部署成功1.3 Nginx安装1.3.1 安装prce包Prce包为Nginx正则表达式支持包,将pcre-8.35.tar.gz上传到服务器/soft目录下(该包为nginx依赖包)tarxvzfpcre-8.35.tar.gzcd pcre-8.35./configure MakeMake install1.3.2 安装Nginxtar
6、xvzfnginx-1.6.0.tar.gzcd nginx-1.6.0./configure -prefix=/opt/nginxmakemake install安装完成后进入/opt/nginx/sbin目录Cd /opt/nginx/sbinnginx或者opt/nginx/sbin/nginx注:redhat 64位机器上nginx可能会读取pcre文件为/lib64/libpcre.so.1文件,所以需要做一个软连接:lns /usr/local/lib/libpcre.so.1 /lib64/在启动nginx(nginx启动不会有任何反馈信息)在浏览器地址栏输入http:/loca
7、lhost提示its work 或者是nginx的欢迎页面表示成功1.4 Nginx配置修改1.4.1 proxy.conf在conf文件夹下新建proxy.conf文件并加入如下代码proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 10m;client_body_buffer_size 128k;proxy_c
8、onnect_timeout 300;proxy_send_timeout 300;proxy_read_timeout 300;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;1.4.2 gzip.conf在conf文件夹下新建gzip.conf文件并加入如下代码gzip on;gzip_min_length 1000;gzip_types text/plain text/cssapplication.x-javascript;1.4.3 n
9、ginx.conf打开nginx.conf修改如下#user nobody;worker_processes4;#nginx进程数,建议设置为等于CPU总核心数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服务器http includemime.types;default_type
10、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;keepalive_timeout 65;gzip on; #取消注释server
11、listen 80 default;server_namelocalhost; #charset koi8-r; #access_log logs/host.access.log main;location / # root html;index index.html index.htm; #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
12、; # 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_indexindex.php; # fastcgi_param SCRIPT_FILENAME
13、 /scripts$fastcgi_script_name; # include fastcgi_params; # # deny access to .htaccess files, if Apaches document root # concurs with nginxs one # #location /.ht # deny all; # # another virtual host using mix of IP-, name-, and port-based configuration # #server # listen 8000; # listen somename:8080;
14、 # server_namesomename alias another.alias; # location / # root html; # index index.html index.htm; # # # upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大,此处为配置权值,默认为轮巡方式。upstreamlocalhost #ip_hash; server 192.168.56.102:8180; #server ip:prot weight;server 192.168.56.102:8280; server
15、listen 80; #nginx监听端口server_name 192.168.56.102; #nginx服务名location / proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http:/localhost; #nginx监听地址 # HTTPS server # #server # listen 443 ssl; # server_namelocalhost; # ssl_certificatecert.pem; # ssl_certificate_keycert.k
16、ey; # 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; # #完成以上配置后执行以下代码检测配置opt/nginx/sbin/nginx -t若检测通过这杀掉nginx进程重新启动nginx1.5 负载均衡测试最简单的测试为:修改tomcat1以及tomcat2的index.jsp的内容。然后新建多个会话访问10.161.200.90若tomcat首页不同这表示负载均衡配置成功。至此,本次配置结束
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2