赞
踩
目录
# 0.首先需要有依赖yum install -y gcc pcre-devel zlib-devel# 1.下载Nginxhttp://nginx.org/en/download.html# 2.将Nginx上传到linux中,并解压缩tar -zxvf nginx-1.11.1.tar.gz# 3.在解压好的nginx目录中执行如下命令:(指定安装位置)./configure --prefix=/usr/nginx# 5.执行上述命令后,执行如下命令:make && make install# 6.编译完成后进入编译安装目录/usr/nginx目录中查看:[root@localhost nginx]# ls -l总用量 4drwxr-xr-x. 2 root root 4096 10月 14 21:17 confdrwxr-xr-x. 2 root root 40 10月 14 21:17 htmldrwxr-xr-x. 2 root root 6 10月 14 21:17 logsdrwxr-xr-x. 2 root root 19 10月 14 21:17 sbin# 7.启动nginx,进入nginx安装目录的sbin目录中执行:./nginx# 8.在windows中浏览器访问,可以看到nginx欢迎页面:http://10.15.0.8:80/ --输入自己的服务器地址:nginx端口号注意:关闭网络防火墙# 9.关闭nginx,进入nginx安装目录的sbin目录中执行:./nginx -s stop# 10.nginx配置文件在nginx安装目录的conf目录中:[root@localhost conf]# ls -l总用量 60-rw-r--r--. 1 root root 2656 10月 14 21:17 nginx.conf.......注意:nginx.conf为nginx的配置文件,可以在nginx.conf修改nginx默认配置
# 1.轮询说明: 默认策略,每个请求会按时间顺序逐一分配到不同的后端服务器# 2.weight 权重说明: weight参数用于指定轮询几率,weight的默认值为1,;weight的数值与访问比率成正比upstream tomcat-servers {server localhost:8080 weight=2;server localhost:8081;server localhost:8082 backup;}注意:1.权重越高分配到需要处理的请求越多。2.此策略可以与least_conn和ip_hash结合使用主要用于后端服务器性能不均# 3.ip_hash 4%3=1说明:指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端的请求一直发送到相同的服务器,以保证session会话。这样每个访客都固定访问一个后端服务器,可以解决session不能跨服务器的问题。upstream tomcat-servers {ip_hash; #保证每个访客固定访问一个后端服务器server localhost:8080;......}# 4.least_conn说明: 把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果。upstream tomcat-servers{least_conn; #把请求转发给连接数较少的后端服务器server localhost:8080;}
# 0.准备多个tomcattar -zxvf apache-tomcat-8.5.46.tar.gz #解压缩一个新的tomcat安装包mv apache-tomcat-8.5.46 tomcat1 #将名称改为tomcat1cp -r tomcat1/ tomcat2 #复制一份cp -r tomcat1/ tomcat3 #复制一份
# 1.此时当前目录中有三个服务器,如下:[root@localhost ~]# ls -l总用量 12248-rwxrwxrwx. 1 root root 11623939 10月 13 12:25 apache-tomcat-8.5.46.tar.gzdrwxr-xr-x. 9 root root 220 10月 14 21:28 tomcat1drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat2drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat3
# 2.修改tomcat1端口号:(伪分布式)vim tomcat1/conf/server.xml,命令修改如下内容:a.<Server port="8001" shutdown="SHUTDOWN"> ---关闭端口b.<Connector port="8888" protocol="HTTP/1.1" ---http协议端口connectionTimeout="20000"redirectPort="8443" />c.<Connector port="10010" protocol="AJP/1.3" redirectPort="8443" /> ---AJP协议端口
# 3.修改tomcat2端口号:(伪分布式)vim tomcat2/conf/server.xml,命令修改如下内容:a.<Server port="8002" shutdown="SHUTDOWN">b.<Connector port="8889" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />c.<Connector port="10011" protocol="AJP/1.3" redirectPort="8443" />
# 4.修改tomcat3端口号:(伪分布式)vim tomcat2/conf/server.xml,命令修改如下内容:a.<Server port="8003" shutdown="SHUTDOWN">b.<Connector port="8890" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />c.<Connector port="10012" protocol="AJP/1.3" redirectPort="8443" />
# 5.将多个tomcat启动:tomcat1/bin/startup.shtomcat2/bin/startup.shtomcat3/bin/startup.sh
# 6.查看tomcat是否启动成功ps -aux|grep tomcat
# 7.在windows中分别访问tomcat,都看到主页代表启动成功:http://10.15.0.8:8888/http://10.15.0.8:8889/http://10.15.0.8:8890/注意:这步一定要关闭网路防火墙
# 8.将多个tomcat配置到nginx的配置文件中:1).进入nginx的sbin目录关掉nginx服务./nginx -s stop2).进入conf目录,然后编辑nginx.conf文件vi nginx.conf3).在server标签上加入如下配置:upstream tomcat-servers {server 192.168.80.130:8090;server 192.168.80.130:8091;server 192.168.80.130:8092;}4).把配置文件里的如下配置注释掉(server配置里)location / {root html;index index.html index.htm;}5).将配置文件中 location /替换为如下配置:location / {proxy_pass http://tomcat-servers;proxy_redirect off;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Host $http_host;proxy_next_upstream http_502 http_504 error timeout invalid_header;}
# 9.进入nginx安装目录sbin目录启动nginx./nginx -c /usr/nginx/conf/nginx.conf
# 10.访问nginx,看到其中一个tomcat画面:http://10.15.0.8/
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name example.com;location / {root /var/www/example.com;index index.html;}location /api/ {proxy_pass http://api.example.com/;}}}
location /resource {alias /var/www/cdn/resource;index index.html;}
location /resource {root /var/www/cdn;index index.html;}
server {listen 80;server_name example.com;location / {root /var/www/example.com;index index.html;}location /files/ {root /var/www/example.com;autoindex on;}}
location /files/ {root /var/www/example.com;autoindex on;autoindex_format html;autoindex_exact_size off;autoindex_localtime on;}
location /files/ {root /var/www/example.com;autoindex on;index index.html index.php;}
location /app/ {proxy_pass http://localhost:8000/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
location /app/ {proxy_pass https://localhost:8000/;...}
location /app/ {proxy_pass http://backend.example.com;...}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。