轉載於:http://blog.csdn.net/Yoara/article/details/41803153
怎麼下載和安裝nginx/tomcat就不說了,談談在剛開始配置時最容易讓人模糊的地方。
1.配置動靜分離和負載均衡,注意,upstram、server都是在http{}下面的。
upstream t1.test.com {
server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
}
其中,紅色標註定義的服務器組的名字,後續通過改名字制定服務器組。
server {
listen 80;
#server
server_name t1.test.com;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://t1.test.com;
}
#配置Nginx動靜分離
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/www;
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.
expires 3d;
}
}
特別注意,藍色標註的是指域名,就是通過訪問http://t1.test.com 將使用該server定義。後面紅色標註,指的是使用upstream中定義的服務器組。這樣,訪問http://t1.test.com域名的所有請求都會被server t1.test.com處理。
2.理解了upstream的使用方法,負載均衡就不是問題了,weight表示權重,max_fails=2 fail_timeout=30s這兩個參數是指,30s內請求失敗2次就不會再訪問該server了。fail_timeout時間過後才繼續訪問,這是容錯的一種處理,服務器如果恢復了,還是能繼續使用。完整的配置如下
[html] view plain copy
upstream t1.test.com {
server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
}
upstream t2.test.com {
server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
upstream all.test.com {
server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
#server
server_name t1.test.com;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://t1.test.com;
}
#配置Nginx動靜分離
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/www;
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.
expires 3d;
}
}
server {
listen 80;
#server
server_name t2.test.com;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://t2.test.com;
}
#配置Nginx動靜分離
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/www;
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.
expires 3d;
}
}
server {
listen 80;
#server
server_name all.test.com;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://all.test.com;
}
#配置Nginx動靜分離
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/www;
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.
expires 3d;
}
}