目標: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>
這樣我們就成功的搭建了一個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"。
這樣說明負載均衡配置成功了!!!!!!
