1、Ngnix

  Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好。目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。

工作原理图如下:

Linux上安装nginx+tomcat负载均衡

2、Tomcat

tomcat服务器我们可以准备2、3个tomcat服务器进行测试。

安装就不多说了(不会的看这),直接看配置:

依次修改3台tomcat的配置文件vi /software/tomcat-8.5.30/conf/server.xml

... 
 <Server port="18005" shutdown="SHUTDOWN">
...
...
<Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...
... 
 <Server port="28005" shutdown="SHUTDOWN">
...
...
<Connector port="28080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="28009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...
... 
 <Server port="38005" shutdown="SHUTDOWN">
...
...
<Connector port="38080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="38009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...

没有配置全局变量就这样子启动和关闭:

sh /software/tomcat-8.5.30/bin/startup.sh
sh /software/tomcat-8.5.30/bin/shutdown.sh

OK,需要配置的就配置好了,我们再做一件事,区别tomcat的欢迎页,我们修改一下index.jsp页面,tomcat-8.5.30/webapps/ROOT/index.jsp,随意区分一下就好。然后分别启动三个tomcat。Linux上安装nginx+tomcat负载均衡Linux上安装nginx+tomcat负载均衡Linux上安装nginx+tomcat负载均衡Linux上安装nginx+tomcat负载均衡

3、环境要求

1、 jdk 1.8.0_102(不会安装的看这里

2、nginx 1.12.0(在官网上下一个解压就行,官网:http://nginx.org/);

3、2个或多个tomcat 6.x 7.x 8.x 9.x 都可以(比如我准备的是3个一模一样的8.x版本,只是配置文件改了而已,一会会详细说怎么改 ,官网:http://tomcat.apache.org/)

4、Ngnix配置

a、安装依赖:

yum install gcc
yum install gcc-c++ yum install prce pcre
-devel yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

b、安装nginx

也可进入此链接下载下面所需所有包https://download.csdn.net/download/MobiusStrip/12262519

进入:/software/nginx位置

  下载nginx: wget http://nginx.org/download/nginx-1.12.2.tar.gz
  下载openssl : wget http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz
  下载zlib : wget http://zlib.net/zlib-1.2.11.tar.gz
  下载pcre : wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

(如果上面的包找不到,那就在自己电脑上下载然后上传即可,官网:http://nginx.org/en/download.html)

安装perl:   (手动下载网址https://www.cpan.org/src/README.html

[root@localhost ~] wget https://www.cpan.org/src/5.0/perl-5.30.2.tar.gz
[root@localhost ~] tar -xzf perl-5.30.2.tar.gz -C /software
[root@localhost ~] cd perl-5.30.2
[root@localhost ~] ./Configure -des -Dprefix=$HOME/localperl
[root@localhost ~] make
[root@localhost ~] make test
[root@localhost ~] make install

安装openssl:

[root@localhost ~] tar zxvf openssl-fips-2.0.9.tar.gz -C /software
[root@localhost ~] cd /software/openssl-fips-2.0.9
[root@localhost ~] ./config && make && make install

安装pcre:

[root@localhost ~] tar zxvf pcre-8.38.tar.gz -C /software
[root@localhost ~] cd /software/pcre-8.38
[root@localhost ~] ./configure && make && make install

安装zlib:

[root@localhost ~] tar zxvf zlib-1.2.11.tar.gz -C /software
[root@localhost ~] cd /software/zlib-1.2.11
[root@localhost ~] ./configure && make && make install

安装ngnix:

[root@localhost ~] tar zxvf nginx-1.12.2.tar.gz -C /software
[root@localhost ~] cd /software/nginx-1.12.2
[root@localhost ~] ./configure && make && make install

c、配置nginx

配置路径在 /usr/local/nginx/conf  ; 因为安装路径就是在这里。

简易直接配置:

 1 #Nginx所用用户和组,window下不指定
 2 user  root root;
 3 #工作的子进程数量(通常等于CPU数量或者2倍于CPU)
 4 worker_processes  2;
 5 #错误日志存放路径
 6 #error_log  logs/error.log;
 7 #error_log  logs/error.log  notice;
 8 error_log  logs/error.log  info;
 9 #指定pid存放文件
10 #pid        logs/nginx.pid;
11  
12 events {
13 #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
14 #use epoll;
15 #允许最大连接数
16 worker_connections  2048;
17 }
18  
19 http {
20 include       mime.types;
21 default_type  application/octet-stream;
22 #定义日志格式
23 log_format  main  '$remote_addr - $remote_user [$time_local] $request '
24                  '"$status" $body_bytes_sent "$http_referer" '
25                  '"$http_user_agent" "$http_x_forwarded_for"';
26 #access_log  off;
27  
28 access_log  logs/access.log;
29  
30 client_header_timeout  3m;
31 client_body_timeout    3m;
32 send_timeout           3m;
33 #client_header_buffer_size    16k;
34 #large_client_header_buffers  4 16k;
35 sendfile        on;
36 tcp_nopush      on;
37 tcp_nodelay     on;
38 #keepalive_timeout  75 20;
39 #include    gzip.conf;
40  
41 #负载均衡配置
42 #mmzsblog.cn和下文的server_name要一致
43 upstream mmzsblog.cn {
44     #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
45     #同一机器在多网情况下,路由切换,ip可能不同
46     #ip_hash;
47     #server 118.24.19.22:18080;
48     #server 118.24.19.22:28080;
49     #server 118.24.19.22:38080;
50 
51     #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
52     server 118.24.19.22:18081 weight=2;
53     server 118.24.19.22:28080 weight=3;
54     server 118.24.19.22:38080 weight=2;
55 }
56 server {
57 listen       80;
58 server_name  mmzsblog.cn;
59 location / {
60 proxy_connect_timeout   3;
61 proxy_send_timeout      30;
62 proxy_read_timeout      30;
63 
64 add_header X-Static transfer;
65 proxy_redirect off;
66 proxy_set_header Host $host;
67 proxy_set_header X-Real-IP $remote_addr;
68 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
69 
70 #如果是本机就用localhost
71 proxy_pass http://mmzsblog.cn;
72 }
73  
74 #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav这些都是静态文件,但应分辨,js、css可能经常会变,过期时间应小一些,图片、html基本不变,过期时间可以设长一些
75         location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {
76             root         html;
77             access_log   logs/access.log;
78             expires      -1s;
79         }
80  
81 gzip on;
82         gzip_comp_level 7;
83         gzip_min_length  1100; #需要压缩的最小长度
84         gzip_buffers    4 8k;
85         gzip_types      text/plain application/javascript text/css text/xml application/x-httpd-php; #指定需要压缩的文件类型
86         output_buffers  1 32k;
87         postpone_output  1460;
88  
89         #error_page  404              /404.html;
90  
91         # redirect server error pages to the static page /50x.html
92         error_page   500 502 503 504  /50x.html;
93         location = /50x.html {
94             root   html;
95         }
96 }
97 }
/software/nginx-1.12.2/conf/nginx.conf

相关文章: