nginx + tomcat配置負載均衡


目標:Nginx做為HttpServer,連接多個tomcat應用實例,進行負載均衡。

注:本例程以一台機器為例子,即同一台機器上裝一個nginx和2個Tomcat且安裝了JDK1.7。

1、安裝Nginx
安裝Nginx教程

2、配置兩個Tomcat
在本機上配置兩個Tomcat,分別為tomcat7-8081、tomcat7-8082。
tomcat7-8081訪問地址:http://localhost:8081,瀏覽顯示內容:this is 8081 port
tomcat7-8082訪問地址:http://localhost:8082,瀏覽顯示內容:this is 8082 port

D:\div\tomcat7-8081\webapps\ROOT\index.jsp文件內容為:

<!DOCTYPE html>

<html lang="en">
    <head>this is 8081 port</head>
</html>

D:\div\tomcat7-8082\webapps\ROOT\index.jsp文件內容為:

<!DOCTYPE html>

<html lang="en">
    <head>this is 8082 port</head>
</html>

在同一台服務器上配置多個Tomcat教程

這樣我們就成功的搭建了一個nginx服務,成功的配置了兩個tomcat應用實例。

3、Nginx+Tomcat負載均衡配置
這里只需要修改Nginx的配置,讓它通過tomcat來轉發。
a、nginx.conf配置文件

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  10;
    include extra/upstream01.conf;
}

 

b、extra/upstream01.conf文件,負載均衡配置信息

upstream mysite {
    server localhost:8081 weight=5;
    server localhost:8082 weight=5;
}
 
server {
    listen 80;
    server_name localhost;
  
    location / {
    proxy_pass http://mysite;
    }
}

當有請求到localhost時,請求會被分發到對應的upstream設置的服務器列表上,每一次請求分發的服務器都是隨機的。

接着在運行一次start nginx,當你不斷刷新http://localhost的時候,瀏覽器上就會來回切換"this is 8081 port"和"this is 8082 port"。

這樣說明負載均衡配置成功了!!!!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM