1.nginx下載地址:nginx官網
2.下載後解壓到指定目錄(本次測試是G:\nginx-1.9.12),然後使用cmd cd命令指定目錄 G:\nginx-1.9.12下運行 nginx.exe ; (啓動關閉命令可以使用大神製作的bat文件,分享Nginx在Windows下的管理命令(bat文件) )需要將下圖中標紅的地方改爲自己的nginx文件所在的目錄和盤符
啓動後在瀏覽器中輸入loclahost出現如下welcome to nginx 說明啓動成功!
3.運行兩個tomcat 跑測試項目,tomcat測試端口分別設置爲8080和8081
4.配置 nginx.conf文件 (在G:\nginx-1.9.12\conf文件夾下)
在http下面增加
(weight是權重,值越大優先訪問率越大,fail_timeout是超時時間)
修改server 中的servername和location
http完整修改文件(去除註釋後)
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#配置負載均衡服務器
upstream site {
ip_hash;
server 192.168.0.124:8080 weight=1 fail_timeout=30s;
server 192.168.0.124:8081 weight=1 fail_timeout=30s;
}
server {
listen 80;
server_name 192.168.0.124;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
#proxy_pass http://192.168.0.124:8080; 端口轉發
proxy_pass http://site;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
}
}
}
配置好重啓nginx 訪問 192.168.0.124 會自動分配項目