Nginx+Windows負載平衡配置方法_nginx

來源:互聯網
上載者:User
一、下載Nginx
http://nginx.org/download/nginx-1.2.5.zip
解壓到C:\nginx目錄下
二、在兩台伺服器上分別建一個網站:
S1:192.168.16.35:8054
S2:192.168.16.16:8089
二、找到目錄
C:\nginx\conf\nginx.conf
開啟nginx.conf
配置如下:

複製代碼 代碼如下:

#使用的使用者和組,window下不指定
#user nobody;
#指定工作繁衍的處理序數(一般等於CPU總和數或總和數的兩倍,例如兩個四核CPU,則總和數為8)
worker_processes 1;
#指定錯誤記錄檔檔案存放路徑,錯誤記錄檔層級可選項為【debug|info|notice|warn|error|crit】
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放路徑
#pid logs/nginx.pid;

#工作模式及串連數上限
events {
#使用網路I/O模型,Linux系統推薦使用epoll模型,FreeBSD系統推薦使用kqueue;window下不指定
#use epoll;
#允許的串連數
worker_connections 1024;
}

#設定http伺服器,利用他的反向 Proxy功能提供負載平衡支援
http {
#設定mime類型
include mime.types;
default_type application/octet-stream;
#設定日誌格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;
log_format main '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"'
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent"'
'"$http_range" "$sent_http_content_range"';

#設定請求緩衝
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

#設定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;

#開啟gzip模組
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css application/xml;

output_buffers 1 32k;
postpone_output 1460;

server_names_hash_bucket_size 128;
client_max_body_size 8m;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on;

#設定負載平衡的伺服器列表
upstream localhost {
#根據ip計算將請求分配各那個後端tomcat,可以解決session問題
ip_hash;
#同一機器在多網情況下,路由切換,ip可能不同
#weigth參數表示權值,權值越高被分配到的幾率越大
#server localhost:8080 weight=1;
#server localhost:9080 weight=1;
server 192.168.16.35:8054 max_fails=2 fail_timeout=600s;
server 192.168.16.16:8089 max_fails=2 fail_timeout=600s;
}

#設定虛擬機器主機
server {
listen 80;
server_name 192.168.16.16;

#charset koi8-r;
charset UTF-8;
#設定本虛擬機器主機的訪問日誌
access_log logs/host.access.log main;
#假如訪問 /img/*, /js/*, /css/* 資源,則直接取本地文檔,不通過squid
#假如這些文檔較多,不推薦這種方式,因為通過squid的緩衝效果更好
#location ~ ^/(img|js|css)/ {
# root /data3/Html;
# expires 24h;
# }
#對 "/" 啟用負載平衡
location / {
root html;
index index.html index.htm index.aspx;

proxy_redirect off;
#保留使用者真實資訊
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允許用戶端請求的最大單個檔案位元組數
client_max_body_size 10m;
#緩衝區代理緩衝使用者端請求的最大位元組數,可以理解為先儲存到本地再傳給使用者
client_body_buffer_size 128k;
#跟後端伺服器連線逾時時間 發起握手等候響應逾時時間
proxy_connect_timeout 12;
#串連成功後 等待後端伺服器回應時間 其實已進入後端的排隊之中等候處理
proxy_read_timeout 90;
#後端伺服器資料回傳時間 就是在規定時間內後端伺服器必須傳完所有資料
proxy_send_timeout 90;
#代理請求緩衝區 這個緩衝區間會儲存使用者的頭資訊一共Nginx進行規則處理 一般只要能儲存下頭資訊即可
proxy_buffer_size 4k;
#同上 告訴Nginx儲存單個用的幾個Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統很忙的時候可以申請國內各大的proxy_buffers 官方推薦 *2
proxy_busy_buffers_size 64k;
#proxy 緩衝臨時檔案的大小
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;

proxy_pass http://localhost;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

四、雙擊C:\nginx\nginx.exe檔案,啟動nginx。
五、開啟瀏覽器:
輸入http://192.168.16.16 進行訪問
測試:關掉S1上的網站,再重新整理瀏覽器訪問;關掉S2上的網站,開啟S1的網站,重新整理瀏覽器訪問。

核心代碼1:在http{}裡面加入
複製代碼 代碼如下:

#設定負載平衡的伺服器列表
upstream localhost {
#根據ip計算將請求分配各那個後端tomcat,可以解決session問題
ip_hash;
#同一機器在多網情況下,路由切換,ip可能不同
#weigth參數表示權值,權值越高被分配到的幾率越大
#server localhost:8080 weight=1;
#server localhost:9080 weight=1;
server 192.168.1.98:8081 max_fails=2 fail_timeout=600s;
server 192.168.1.98:8082 max_fails=2 fail_timeout=600s;


核心代碼2:在server {}添加
複製代碼 代碼如下:

#對 "/" 啟用負載平衡
location / {
root html;
index index.html index.htm index.aspx;

proxy_redirect off;
#保留使用者真實資訊
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允許用戶端請求的最大單個檔案位元組數
client_max_body_size 10m;
#緩衝區代理緩衝使用者端請求的最大位元組數,可以理解為先儲存到本地再傳給使用者
client_body_buffer_size 128k;
#跟後端伺服器連線逾時時間 發起握手等候響應逾時時間
proxy_connect_timeout 12;
#串連成功後 等待後端伺服器回應時間 其實已進入後端的排隊之中等候處理
proxy_read_timeout 90;
#後端伺服器資料回傳時間 就是在規定時間內後端伺服器必須傳完所有資料
proxy_send_timeout 90;
#代理請求緩衝區 這個緩衝區間會儲存使用者的頭資訊一共Nginx進行規則處理 一般只要能儲存下頭資訊即可
proxy_buffer_size 4k;
#同上 告訴Nginx儲存單個用的幾個Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統很忙的時候可以申請國內各大的proxy_buffers 官方推薦 *2
proxy_busy_buffers_size 64k;
#proxy 緩衝臨時檔案的大小
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass http://localhost;
}


以下是一些補充工具:

Nginx負載平衡是一個很神奇的技術,很多人都不能很好的掌握這個技術,今天在這裡我們向大家詳細的介紹下有關Nginx負載平衡的問題。今天小試了一下Nginx負載平衡,真是爽啊!Nginx是什嗎?

Nginx (”engine x”) 是一個高效能的 HTTP 和 反向 Proxy 伺服器,也是一個 IMAP/POP3/SMTP Proxy 伺服器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 網站開發的,它已經在該網站運行超過兩年半了。Igor 將原始碼以類BSD許可證的形式發布。儘管還是測試版,但是,Nginx 已經因為它的穩定性、豐富的功能集、樣本設定檔和低系統資源的消耗而聞名了。

首先是配置十分的簡單,而且功能非常強大。真是相見恨晚。先來看看設定檔怎麼寫吧

複製代碼 代碼如下:

worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#這裡指定多個原始伺服器,ip:連接埠,80連接埠的話可寫可不寫
server 192.168.43.158:80;
server 192.168.41.167;
}
server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}


Nginx負載平衡有哪些功能呢?

如果後面的伺服器其中一台壞了,它能自動識別,更牛的是它好了之後Nginx可以馬上識別伺服器A和B,如果A的回應時間為3,B的回應時間為1,那麼Nginx會自動調整訪問B的機率是A的3倍,真正做到Nginx負載平衡好的,安裝完成了。我在make的時候報了個錯,說HTTP Rewrite 模組 有問題,我就

./configure –without-http_rewrite_module
然後再make,make install就可以了。

安裝好了之後建立一個設定檔,把上面的設定檔內容拷進去,當然要修改你的IP,儲存為比如 load_balance.conf然後啟動:

/usr/local/Nginx/sbin/Nginx -c load_balence.conf

由於Nginx的作者是俄國人,所以英文的文檔也不是那麼完善,對於我來說Nginx的最大優點還是配置簡單,功能強大。我曾經配過 apache-jk,那真的不是一般人能配的。太複雜了,而且只能用來做tomcat的Nginx負載平衡。

Nginx就沒有這個限制,對它來說後面是什麼伺服器是完全透名的。Nginx就一點不爽,它本身目前還不能在windows下面跑。寫了一大堆,哈哈。~~說的不對的大家指出哈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.