1. 程式人生 > 其它 >nginx 配置負載均衡

nginx 配置負載均衡

windows下載nginx解壓後如下:

配置檔案為conf目錄下的nginx.conf

    在http節點下加入upstream如下:

    upstream tomcats {
		server 127.0.0.1:8081;
		server 127.0.0.1:8082;
		#server 127.0.0.1:8082 weight=2 fail_timeout=20s;
		#server 127.0.0.1:8081 weight=1 fail_timeout=20s;
		#ip_hash;
	}

    在server節點下修改location如下,加入proxy_pass :

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://tomcats; #這裡和upstream的名稱相同
        }

啟動(重啟)nginx.exe,並請求server節點配置的server_name和埠port就可以將請求輪流發到8081和8082去處理。具體upstream的機制請參考:http://blog.csdn.net/shuai825644975/article/details/58140008

其中nginx的啟動停止可以使用一下cmd命令:

    nginx.exe -s stop
    nginx.exe -s reload #重新載入配置檔案
    nginx.exe #啟動

在linux中使用nginx:

    安裝:sudo apt-get install nginx

    啟動: sudo nginx (如果由於log檔案許可權問題啟動失敗,則可以在對應log目錄下執行sudo chmod 777 xxx.log修改檔案的許可權)

配置負載均衡與windows差不多,在/etc/nginx目錄下有配置檔案nginx.conf,裡面預設使用include匯入了conf.d/資料夾下的所有*.conf檔案,所以就可以將server節點以及upstream節點需要配置的東西分別在conf.d/目錄下建立配置檔案寫入。若有許可權問題,可以使用chmod修改許可權。