Nginx+Windows負載均衡配置方法
更新時間:2012年11月26日 14:26:10 投稿:mdxy-dxy
Nginx負載均衡如何才能實現(xiàn)呢?這個問題有很多的程序員都希望知道,下面我們就向大家詳細的介紹有關Nginx負載均衡的信息
一、下載Nginx http://nginx.org/download/nginx-1.2.5.zip
解壓到C:\nginx目錄下
二、在兩臺服務器上分別建一個網(wǎng)站:
S1:192.168.16.35:8054
S2:192.168.16.16:8089
二、找到目錄
C:\nginx\conf\nginx.conf
打開nginx.conf
配置如下:
復制代碼 代碼如下:
#使用的用戶和組,window下不指定
#user nobody;
#指定工作衍生進程數(shù)(一般等于CPU總和數(shù)或總和數(shù)的兩倍,例如兩個四核CPU,則總和數(shù)為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;
#工作模式及連接數(shù)上限
events {
#使用網(wǎng)絡I/O模型,Linux系統(tǒng)推薦使用epoll模型,F(xiàn)reeBSD系統(tǒng)推薦使用kqueue;window下不指定
#use epoll;
#允許的連接數(shù)
worker_connections 1024;
}
#設定http服務器,利用他的反向功能提供負載均衡支持
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 {
#根據(jù)ip計算將請求分配各那個后端tomcat,可以解決session問題
ip_hash;
#同一機器在多網(wǎng)情況下,路由切換,ip可能不同
#weigth參數(shù)表示權值,權值越高被分配到的幾率越大
#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;
#允許客戶端請求的最大單個文件字節(jié)數(shù)
client_max_body_size 10m;
client_body_buffer_size 128k;
#跟后端服務器連接超時時間 發(fā)起握手等候響應超時時間
proxy_connect_timeout 12;
#連接成功后 等待后端服務器響應時間 其實已進入后端的排隊之中等候處理
proxy_read_timeout 90;
#后端服務器數(shù)據(jù)回傳時間 就是在規(guī)定時間內(nèi)后端服務器必須傳完所有數(shù)據(jù)
proxy_send_timeout 90;
proxy_buffer_size 4k;
#同上 告訴Nginx保存單個用的幾個Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統(tǒng)很忙的時候可以申請國內(nèi)各大的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上的網(wǎng)站,再刷新瀏覽器訪問;關掉S2上的網(wǎng)站,打開S1的網(wǎng)站,刷新瀏覽器訪問。
核心代碼1:在http{}里面加入
復制代碼 代碼如下:
#設定負載均衡的服務器列表
upstream localhost {
#根據(jù)ip計算將請求分配各那個后端tomcat,可以解決session問題
ip_hash;
#同一機器在多網(wǎng)情況下,路由切換,ip可能不同
#weigth參數(shù)表示權值,權值越高被分配到的幾率越大
#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;
#允許客戶端請求的最大單個文件字節(jié)數(shù)
client_max_body_size 10m;
client_body_buffer_size 128k;
#跟后端服務器連接超時時間 發(fā)起握手等候響應超時時間
proxy_connect_timeout 12;
#連接成功后 等待后端服務器響應時間 其實已進入后端的排隊之中等候處理
proxy_read_timeout 90;
#后端服務器數(shù)據(jù)回傳時間 就是在規(guī)定時間內(nèi)后端服務器必須傳完所有數(shù)據(jù)
proxy_send_timeout 90;
proxy_buffer_size 4k;
#同上 告訴Nginx保存單個用的幾個Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統(tǒng)很忙的時候可以申請國內(nèi)各大的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是什么?
首先是配置十分的簡單,而且功能非常強大。真是相見恨晚。先來看看配置文件怎么寫吧
復制代碼 代碼如下:
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會自動調(diào)整訪問B的概率是A的3倍,真正做到Nginx負載均衡好的,安裝完成了。我在make的時候報了個錯,說HTTP Rewrite 模塊 有問題,我就
./configure –without-http_rewrite_module
然后再make,make install就可以了。
安裝好了之后新建一個配置文件,把上面的配置文件內(nèi)容拷進去,當然要修改你的IP,保存為比如 load_balance.conf然后啟動:
/usr/local/Nginx/sbin/Nginx -c load_balence.conf
Nginx就沒有這個限制,對它來說后面是什么服務器是完全透名的。Nginx就一點不爽,它本身目前還不能在windows下面跑。寫了一大堆,哈哈?!f的不對的大家指出哈
相關文章
PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法,涉及nginx平臺的相關配置技巧,需要的朋友可以參考下2016-05-05讓VIM支持Nginx .conf文件語法高亮顯示功能的方法
這篇文章主要給大家介紹了關于讓VIM支持Nginx .conf文件語法高亮顯示功能的方法,文中分別介紹了手動修改和自動化腳本兩種方法的實現(xiàn),都給出了詳細的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07nginx 代理80端口轉(zhuǎn)443端口的實現(xiàn)
這篇文章主要介紹了nginx 代理80端口轉(zhuǎn)443端口的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09