Nginx实现负载均衡

释放双眼,带上耳机,听听看~!

在看这个知识点之前,先学会nginx的反向代理

先看nginx的反向代理,就看这个链接:https://blog.csdn.net/weixin_43689480/article/details/95860813

nginx实现负载均衡的过程

想要的结果就是:给 www.sina.com 添加多个tomcat服务器

首先安装两个tomcat服务器,都放到nginx服务器里面,两个端口分别是8081和8082

首先是下载tomcat,使用下面的命令


1
2
3
1wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.94/bin/apache-tomcat-7.0.94.tar.gz
2
3

Nginx实现负载均衡
然后是加载这个压缩包


1
2
3
4
1tar -xvf apache-tomcat-7.0.94.tar.gz
2
3
4

Nginx实现负载均衡
然后把apache-tomcat-7.0.94复制成两份,一份是tomcat8081,一份是tomcat8082,过程如下


1
2
3
4
1cp -r apache-tomcat-7.0.94 tomcat8081
2cp -r apache-tomcat-7.0.94 tomcat8082
3
4

Nginx实现负载均衡
然后通过Editplus远程连接修改tomcat8081里面的server.xml配置里面修改端口号(怎么通过EditPlus连接linux,看着篇文章:https://blog.csdn.net/weixin_43689480/article/details/95867289),注意下面要修改三个端口号


1
2
3
4
5
6
7
1<Server port="8006" shutdown="SHUTDOWN">
2<Connector port="8081" protocol="HTTP/1.1"
3               connectionTimeout="20000"
4               redirectPort="8443" />
5<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
6
7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
1<?xml version='1.0' encoding='utf-8'?>
2<!--
3  Licensed to the Apache Software Foundation (ASF) under one or more
4  contributor license agreements.  See the NOTICE file distributed with
5  this work for additional information regarding copyright ownership.
6  The ASF licenses this file to You under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with
8  the License.  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17-->
18<!-- Note:  A "Server" is not itself a "Container", so you may not
19     define subcomponents such as "Valves" at this level.
20     Documentation at /docs/config/server.html
21 -->
22<Server port="8006" shutdown="SHUTDOWN">
23  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
24  <!-- Security listener. Documentation at /docs/config/listeners.html
25  <Listener className="org.apache.catalina.security.SecurityListener" />
26  -->
27  <!--APR library loader. Documentation at /docs/apr.html -->
28  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
29  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
30  <Listener className="org.apache.catalina.core.JasperListener" />
31  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
32  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
33  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
34  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
35
36  <!-- Global JNDI resources
37       Documentation at /docs/jndi-resources-howto.html
38  -->
39  <GlobalNamingResources>
40    <!-- Editable user database that can also be used by
41         UserDatabaseRealm to authenticate users
42    -->
43    <Resource name="UserDatabase" auth="Container"
44              type="org.apache.catalina.UserDatabase"
45              description="User database that can be updated and saved"
46              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
47              pathname="conf/tomcat-users.xml" />
48  </GlobalNamingResources>
49
50  <!-- A "Service" is a collection of one or more "Connectors" that share
51       a single "Container" Note:  A "Service" is not itself a "Container",
52       so you may not define subcomponents such as "Valves" at this level.
53       Documentation at /docs/config/service.html
54   -->
55  <Service name="Catalina">
56
57    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
58    <!--
59    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
60        maxThreads="150" minSpareThreads="4"/>
61    -->
62
63
64    <!-- A "Connector" represents an endpoint by which requests are received
65         and responses are returned. Documentation at :
66         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
67         Java AJP  Connector: /docs/config/ajp.html
68         APR (HTTP/AJP) Connector: /docs/apr.html
69         Define a non-SSL HTTP/1.1 Connector on port 8080
70    -->
71    <Connector port="8081" protocol="HTTP/1.1"
72               connectionTimeout="20000"
73               redirectPort="8443" />
74    <!-- A "Connector" using the shared thread pool-->
75    <!--
76    <Connector executor="tomcatThreadPool"
77               port="8080" protocol="HTTP/1.1"
78               connectionTimeout="20000"
79               redirectPort="8443" />
80    -->
81    <!-- Define a SSL HTTP/1.1 Connector on port 8443
82         This connector uses the BIO implementation that requires the JSSE
83         style configuration. When using the APR/native implementation, the
84         OpenSSL style configuration is required as described in the APR/native
85         documentation -->
86    <!--
87    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
88               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
89               clientAuth="false" sslProtocol="TLS" />
90    -->
91
92    <!-- Define an AJP 1.3 Connector on port 8009 -->
93    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
94
95
96    <!-- An Engine represents the entry point (within Catalina) that processes
97         every request.  The Engine implementation for Tomcat stand alone
98         analyzes the HTTP headers included with the request, and passes them
99         on to the appropriate Host (virtual host).
100         Documentation at /docs/config/engine.html -->
101
102    <!-- You should set jvmRoute to support load-balancing via AJP ie :
103    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
104    -->
105    <Engine name="Catalina" defaultHost="localhost">
106
107      <!--For clustering, please take a look at documentation at:
108          /docs/cluster-howto.html  (simple how to)
109          /docs/config/cluster.html (reference documentation) -->
110      <!--
111      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
112      -->
113
114      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
115           via a brute-force attack -->
116      <Realm className="org.apache.catalina.realm.LockOutRealm">
117        <!-- This Realm uses the UserDatabase configured in the global JNDI
118             resources under the key "UserDatabase".  Any edits
119             that are performed against this UserDatabase are immediately
120             available for use by the Realm.  -->
121        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
122               resourceName="UserDatabase"/>
123      </Realm>
124
125      <Host name="localhost"  appBase="webapps"
126            unpackWARs="true" autoDeploy="true">
127
128        <!-- SingleSignOn valve, share authentication between web applications
129             Documentation at: /docs/config/valve.html -->
130        <!--
131        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
132        -->
133
134        <!-- Access log processes all example.
135             Documentation at: /docs/config/valve.html
136             Note: The pattern used is equivalent to using pattern="common" -->
137        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
138               prefix="localhost_access_log." suffix=".txt"
139               pattern="%h %l %u %t "%r" %s %b" />
140
141      </Host>
142    </Engine>
143  </Service>
144</Server>
145
146
147

然后通过Editplus远程连接修改tomcat8082里面的server.xml配置里面修改端口号(怎么通过EditPlus连接linux,看着篇文章:https://blog.csdn.net/weixin_43689480/article/details/95867289),注意下面要修改三个端口号


1
2
3
4
5
6
7
1<Server port="8007" shutdown="SHUTDOWN">
2<Connector port="8082" protocol="HTTP/1.1"
3               connectionTimeout="20000"
4               redirectPort="8443" />
5<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
6
7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
1<?xml version='1.0' encoding='utf-8'?>
2<!--
3  Licensed to the Apache Software Foundation (ASF) under one or more
4  contributor license agreements.  See the NOTICE file distributed with
5  this work for additional information regarding copyright ownership.
6  The ASF licenses this file to You under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with
8  the License.  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17-->
18<!-- Note:  A "Server" is not itself a "Container", so you may not
19     define subcomponents such as "Valves" at this level.
20     Documentation at /docs/config/server.html
21 -->
22<Server port="8007" shutdown="SHUTDOWN">
23  <!-- Security listener. Documentation at /docs/config/listeners.html
24  <Listener className="org.apache.catalina.security.SecurityListener" />
25  -->
26  <!--APR library loader. Documentation at /docs/apr.html -->
27  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
28  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
29  <Listener className="org.apache.catalina.core.JasperListener" />
30  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
31  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
32  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
33  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
34
35  <!-- Global JNDI resources
36       Documentation at /docs/jndi-resources-howto.html
37  -->
38  <GlobalNamingResources>
39    <!-- Editable user database that can also be used by
40         UserDatabaseRealm to authenticate users
41    -->
42    <Resource name="UserDatabase" auth="Container"
43              type="org.apache.catalina.UserDatabase"
44              description="User database that can be updated and saved"
45              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
46              pathname="conf/tomcat-users.xml" />
47  </GlobalNamingResources>
48
49  <!-- A "Service" is a collection of one or more "Connectors" that share
50       a single "Container" Note:  A "Service" is not itself a "Container",
51       so you may not define subcomponents such as "Valves" at this level.
52       Documentation at /docs/config/service.html
53   -->
54  <Service name="Catalina">
55
56    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
57    <!--
58    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
59        maxThreads="150" minSpareThreads="4"/>
60    -->
61
62
63    <!-- A "Connector" represents an endpoint by which requests are received
64         and responses are returned. Documentation at :
65         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
66         Java AJP  Connector: /docs/config/ajp.html
67         APR (HTTP/AJP) Connector: /docs/apr.html
68         Define a non-SSL HTTP/1.1 Connector on port 8080
69    -->
70    <Connector port="8082" protocol="HTTP/1.1"
71               connectionTimeout="20000"
72               redirectPort="8443" />
73    <!-- A "Connector" using the shared thread pool-->
74    <!--
75    <Connector executor="tomcatThreadPool"
76               port="8080" protocol="HTTP/1.1"
77               connectionTimeout="20000"
78               redirectPort="8443" />
79    -->
80    <!-- Define a SSL HTTP/1.1 Connector on port 8443
81         This connector uses the JSSE configuration, when using APR, the
82         connector should be using the OpenSSL style configuration
83         described in the APR documentation -->
84    <!--
85    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
86               maxThreads="150" scheme="https" secure="true"
87               clientAuth="false" sslProtocol="TLS" />
88    -->
89
90    <!-- Define an AJP 1.3 Connector on port 8009 -->
91    <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
92
93
94    <!-- An Engine represents the entry point (within Catalina) that processes
95         every request.  The Engine implementation for Tomcat stand alone
96         analyzes the HTTP headers included with the request, and passes them
97         on to the appropriate Host (virtual host).
98         Documentation at /docs/config/engine.html -->
99
100    <!-- You should set jvmRoute to support load-balancing via AJP ie :
101    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
102    -->
103    <Engine name="Catalina" defaultHost="localhost">
104
105      <!--For clustering, please take a look at documentation at:
106          /docs/cluster-howto.html  (simple how to)
107          /docs/config/cluster.html (reference documentation) -->
108      <!--
109      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
110      -->
111
112      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
113           via a brute-force attack -->
114      <Realm className="org.apache.catalina.realm.LockOutRealm">
115        <!-- This Realm uses the UserDatabase configured in the global JNDI
116             resources under the key "UserDatabase".  Any edits
117             that are performed against this UserDatabase are immediately
118             available for use by the Realm.  -->
119        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
120               resourceName="UserDatabase"/>
121      </Realm>
122
123      <Host name="localhost"  appBase="webapps"
124            unpackWARs="true" autoDeploy="true">
125
126        <!-- SingleSignOn valve, share authentication between web applications
127             Documentation at: /docs/config/valve.html -->
128        <!--
129        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
130        -->
131
132        <!-- Access log processes all example.
133             Documentation at: /docs/config/valve.html
134             Note: The pattern used is equivalent to using pattern="common" -->
135        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
136               prefix="localhost_access_log." suffix=".txt"
137               pattern="%h %l %u %t "%r" %s %b" />
138
139      </Host>
140    </Engine>
141  </Service>
142</Server>
143
144
145

然后把tomact8081里面的webapps文件夹里面的ROOT文件夹里面的index.jsp变成下面这样,此时可以看到下面输入的内容是8081


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1<%--
2Licensed to the Apache Software Foundation (ASF) under one or more
3contributor license agreements.  See the NOTICE file distributed with
4this work for additional information regarding copyright ownership.
5The ASF licenses this file to You under the Apache License, Version 2.0
6(the "License"); you may not use this file except in compliance with
7the License.  You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16--%>
17<!DOCTYPE html>
18<%@ page session="false" %>
19<%
20java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
21request.setAttribute("year", sdf.format(new java.util.Date()));
22request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
23request.setAttribute("tomcat7DocUrl", "/docs/");
24request.setAttribute("tomcat7ExamplesUrl", "/examples/");
25%>
26<html lang="en">
27    <head>
28        <title><%=request.getServletContext().getServerInfo() %></title>
29        <link href="favicon.ico" rel="icon" type="image/x-icon" />
30        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
31        <link href="tomcat.css" rel="stylesheet" type="text/css" />
32    </head>
33
34    <body>
35      <h1>tomcat8081index.jsp<h1>
36    </body>
37
38</html>
39
40

然后把tomact8082里面的webapps文件夹里面的ROOT文件夹里面的index.jsp变成下面这样,此时可以看到下面输入的内容是8082


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1<%--
2Licensed to the Apache Software Foundation (ASF) under one or more
3contributor license agreements.  See the NOTICE file distributed with
4this work for additional information regarding copyright ownership.
5The ASF licenses this file to You under the Apache License, Version 2.0
6(the "License"); you may not use this file except in compliance with
7the License.  You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16--%>
17<!DOCTYPE html>
18<%@ page session="false" %>
19<%
20java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
21request.setAttribute("year", sdf.format(new java.util.Date()));
22request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
23request.setAttribute("tomcat7DocUrl", "/docs/");
24request.setAttribute("tomcat7ExamplesUrl", "/examples/");
25%>
26<html lang="en">
27    <head>
28        <title><%=request.getServletContext().getServerInfo() %></title>
29        <link href="favicon.ico" rel="icon" type="image/x-icon" />
30        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
31        <link href="tomcat.css" rel="stylesheet" type="text/css" />
32    </head>
33
34    <body>
35      <h1>tomcat8082index.jsp<h1>
36    </body>
37
38</html>
39
40

然后就是启动tomcat8081和tomcat8082,启动如下所示


1
2
3
4
1/root/tomcat8081/bin/startup.sh
2/root/tomcat8082/bin/startup.sh
3
4

然后访问http://47.91.248.236:8081/ 路径结果如下,成功了

Nginx实现负载均衡
然后访问http://47.91.248.236:8082/ 路径结果如下,成功了
Nginx实现负载均衡

然后我们配置本地电脑里面的host文件变成下面这样


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1# Copyright (c) 1993-2009 Microsoft Corp.
2#
3# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
4#
5# This file contains the mappings of IP addresses to host names. Each
6# entry should be kept on an individual line. The IP address should
7# be placed in the first column followed by the corresponding host name.
8# The IP address and the host name should be separated by at least one
9# space.
10#
11# Additionally, comments (such as these) may be inserted on individual
12# lines or following the machine name denoted by a '#' symbol.
13#
14# For example:
15#
16#      102.54.94.97     rhino.acme.com          # source server
17#       38.25.63.10     x.acme.com              # x client host
18
19# localhost name resolution is handled within DNS itself.
20# 127.0.0.1       localhost
21# ::1             localhost
22
2347.91.248.236 www.sina.com
24
25

Nginx实现负载均衡

然后通过EditPlus配置nginx服务器里面的conf文件夹里面的nginx.conf配置文件,配置完之后记得要重启nginx服务器

怎么通过EditPlus操作nginx服务器里面的nginx.conf配置文件,看这个链接:https://blog.csdn.net/weixin_43689480/article/details/95867289

然后讲解下面的配置:就比如下面的
# tomcat1注释,当访问www.sina.com 的时候,就会访问host文件,然后就会去找47.91.248.236 这个ip对应的linux服务器,然后www.sina.com 默认的端口就是80,所以访问www.sina.com 的时候,就会被下面的蓝色代码给捕获到,然后下面的蓝色代码里面的server就会找到蓝色代码里面的upstream tomcat1里面的server 47.91.248.236:8081和server 47.91.248.236:8082,然后呢因为我们配置配置权重,所以当第一次访问www.sina.com 这个域名的时候,就会先找到8081端口的tomcat服务器,然后当第二次访问www.sina.com 这个域名的时候,就会先找到8082端口的tomcat服务器,然后当第三次访问www.sina.com 这个域名的时候,就会先找到8081端口的tomcat服务器,然后当第四次访问www.sina.com 这个域名的时候,就会先找到8082端口的tomcat服务器,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1
2user  root;
3worker_processes  1;
4
5#error_log  logs/error.log;
6#error_log  logs/error.log  notice;
7#error_log  logs/error.log  info;
8
9#pid        logs/nginx.pid;
10
11
12events {
13    worker_connections  1024;
14}
15
16
17http {
18    include       mime.types;
19    default_type  application/octet-stream;
20
21    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
22    #                  '$status $body_bytes_sent "$http_referer" '
23    #                  '"$http_user_agent" "$http_x_forwarded_for"';
24
25    #access_log  logs/access.log  main;
26
27    sendfile        on;
28    #tcp_nopush     on;
29
30    #keepalive_timeout  0;
31    keepalive_timeout  65;
32
33    #gzip  on;
34    # tomcat1注释开始
35
36    upstream tomcat1 {
37  server 47.91.248.236:8081;
38  server 47.91.248.236:8082;
39    }
40    server {
41        listen       80;
42        server_name  www.sina.com;
43
44        #charset koi8-r;
45
46        #access_log  logs/host.access.log  main;
47
48        location / {
49            proxy_pass   http://tomcat1;
50            index  index.html index.htm;
51        }
52    }
53    # tomcat1注释结束
54
55}
56
57
58

然后我们访问 www.sina.com

第一次访问
Nginx实现负载均衡
第二次访问
Nginx实现负载均衡
从上面的结果可以看出来nginx的负载均衡搞定了

Nginx的负载均衡怎么实现不同服务器的访问次数不同

就是用下面的weight属性就好了,默认的每一个服务器的weight都是1,然后weigth的值越大,表示访问该服务器的次数越高,所以如果好一点的服务器,那么我么就把这个服务器的weight的值变大一些,如果服务器差一些,那么就把这个服务器的weight的值变小一些


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1
2#user  nobody;
3worker_processes  1;
4
5#error_log  logs/error.log;
6#error_log  logs/error.log  notice;
7#error_log  logs/error.log  info;
8
9#pid        logs/nginx.pid;
10
11
12events {
13    worker_connections  1024;
14}
15
16
17http {
18    include       mime.types;
19    default_type  application/octet-stream;
20
21    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
22    #                  '$status $body_bytes_sent "$http_referer" '
23    #                  '"$http_user_agent" "$http_x_forwarded_for"';
24
25    #access_log  logs/access.log  main;
26
27    sendfile        on;
28    #tcp_nopush     on;
29
30    #keepalive_timeout  0;
31    keepalive_timeout  65;
32
33    #gzip  on;
34
35    upstream tomcat1 {
36  server 47.91.248.236:8081;
37  server 47.91.248.236:8082 weight 2;
38    }
39    server {
40        listen       80;
41        server_name  www.sina.com.cn;
42
43        #charset koi8-r;
44
45        #access_log  logs/host.access.log  main;
46
47        location / {
48            proxy_pass   http://tomcat1;
49            index  index.html index.htm;
50        }
51    }
52
53}
54
55

能看到这里的同学,就帮忙点个赞吧,Thanks♪(・ω・)ノ

给TA打赏
共{{data.count}}人
人已打赏
安全经验

人们为何痛恨Google Adsense

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索