Home >Backend Development >PHP Tutorial >nginx tomcat implements load balancing

nginx tomcat implements load balancing

WBOY
WBOYOriginal
2016-07-29 09:06:511250browse

It is very simple for tomcat to use nginx to achieve load balancing

1. Environment preparation

The IP addresses of the two machines are 192.168.0.166 192.168.0.167

166 of which is installed with nginx

Install tomcat on 166 and 167 respectively

Of course, you can choose another machine to install nginx. Because I don’t have such a machine, I installed nginx on 166

2. Configuration file

As mentioned in the previous article After knowing the basic configuration of nginx, you only need to add the following two configurations to the basic configuration

<ol>
<li>
<code></code>  <code><span>#使用域名 www.test.com(可以自己随便定义,也可以使用IP地址)</span></code>
</li>
<li><code><span>  upstream www</span><span>.</span><span>test</span><span>.</span><span>com </span><span>{</span></code></li>
<li>
<code><span>          </span></code> #server 就是想要实现负载均衡的俩tomcat的端口 默认是8080,ip是166和167</li>
<li><code><span>           server  </span><span>192.168</span><span>.</span><span>0.166</span><span>:</span><span>8080</span><span>;</span></code></li>
<li><code><span>           server  </span><span>192.168</span><span>.</span><span>0.167</span><span>:</span><span>8080</span><span>;</span></code></li>
<li><code><span>           </span></code></li>
<li><code><span>  </span><span>}</span></code></li>
<li>
<code></code><span> server </span><span>{</span>
</li>
<li><code><span>        listen       </span><span>80</span><span>;</span></code></li>
<li><code><span>        server_name  www</span><span>.</span><span>test</span><span>.</span><span>com</span><span>;</span></code></li>
<li><code></code></li>
<li><code><span>        </span><span>#charset koi8-r;</span></code></li>
<li><code></code></li>
<li><code><span>        </span><span>#其中~^/rzdb 是一个正则表达式,意思是只要访问的地址中出现rzdb(也就是我的项目名字)</span></code></li>
<li><code></code></li>
<li><code><span>         location </span><span>~^/</span><span>rzdb </span><span>{</span></code></li>
<li><code><span>                        proxy_pass         http</span><span>://</span><span>www</span><span>.</span><span>test</span><span>.</span><span>com</span><span>;</span></code></li>
<li><code><span>                        proxy_set_header   </span><span>Host</span><span>             $host</span><span>;</span></code></li>
<li><code><span>                        proxy_set_header   X</span><span>-</span><span>Real</span><span>-</span><span>IP        $remote_addr</span><span>;</span></code></li>
<li><code><span>                        proxy_set_header   X</span><span>-</span><span>Forwarded</span><span>-</span><span>For</span><span>  $proxy_add_x_forwarded_for</span><span>;</span></code></li>
<li><code><span>                </span><span>}</span></code></li>
</ol>

After the configuration is completed, start nginx.

Visit http://www.test.com/rzdb/index.html and the access is successful;


The above introduces nginx tomcat to achieve load balancing, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn