實現效果:
準備工作
(1)啓動兩臺tomcat服務器,8080和8081
(2)在兩臺 tomcat 裏面 webapps 目錄中,創建名稱是 edu 文件夾,在 edu 文件夾中創建頁面 a.html(內容不同,便於驗證),用於測試
配置
在安裝目錄/conf/nginx.conf配置如下:
在http塊中新增服務器列表
#配置服務器列表
upstream myserver{
server 127.0.0.1:8080;
server 127.0.0.1:8081;#可以在這裏配置分配策略,比如按權重: server 127.0.0.1:8080 weight=1; server 127.0.0.1:8081 weight=2;
}
修改location中的proxy_pass值爲自己新增的服務器列表名字
location / {
root html;
proxy_pass http://myserver; //這一句!!!!
index index.html index.htm;
}
//其中也可以把server中的server_name值改爲自己喜歡的ip,非必要,默認是127.0.0.1
驗證
在瀏覽器輸入http://127.0.0.1/edu/a.html,隨着刷新能看到每次可能訪問不同的服務器。
nginx 分配服務器策略