Nginx+Windows負(fù)載均衡配置方法
一、下載Nginx http://nginx.org/download/nginx-1.2.5.zip
解壓到C:\nginx目錄下
二、在兩臺(tái)服務(wù)器上分別建一個(gè)網(wǎng)站:
S1:192.168.16.35:8054
S2:192.168.16.16:8089
二、找到目錄
C:\nginx\conf\nginx.conf
打開(kāi)nginx.conf
配置如下:
#使用的用戶和組,window下不指定
#user nobody;
#指定工作衍生進(jìn)程數(shù)(一般等于CPU總和數(shù)或總和數(shù)的兩倍,例如兩個(gè)四核CPU,則總和數(shù)為8)
worker_processes 1;
#指定錯(cuò)誤日志文件存放路徑,錯(cuò)誤日志級(jí)別可選項(xiàng)為【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)絡(luò)I/O模型,Linux系統(tǒng)推薦使用epoll模型,F(xiàn)reeBSD系統(tǒng)推薦使用kqueue;window下不指定
#use epoll;
#允許的連接數(shù)
worker_connections 1024;
}
#設(shè)定http服務(wù)器,利用他的反向功能提供負(fù)載均衡支持
http {
#設(shè)定mime類型
include mime.types;
default_type application/octet-stream;
#設(shè)定日志格式
#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"';
#設(shè)定請(qǐng)求緩沖
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#設(shè)定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;
#開(kāi)啟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;
#設(shè)定負(fù)載均衡的服務(wù)器列表
upstream localhost {
#根據(jù)ip計(jì)算將請(qǐng)求分配各那個(gè)后端tomcat,可以解決session問(wèn)題
ip_hash;
#同一機(jī)器在多網(wǎng)情況下,路由切換,ip可能不同
#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
#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;
}
#設(shè)定虛擬主機(jī)
server {
listen 80;
server_name 192.168.16.16;
#charset koi8-r;
charset UTF-8;
#設(shè)定本虛擬主機(jī)的訪問(wèn)日志
access_log logs/host.access.log main;
#假如訪問(wèn) /img/*, /js/*, /css/* 資源,則直接取本地文檔,不通過(guò)squid
#假如這些文檔較多,不推薦這種方式,因?yàn)橥ㄟ^(guò)squid的緩存效果更好
#location ~ ^/(img|js|css)/ {
# root /data3/Html;
# expires 24h;
# }
#對(duì) "/" 啟用負(fù)載均衡
location / {
root html;
index index.html index.htm index.aspx;
proxy_redirect off;
#保留用戶真實(shí)信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允許客戶端請(qǐng)求的最大單個(gè)文件字節(jié)數(shù)
client_max_body_size 10m;
client_body_buffer_size 128k;
#跟后端服務(wù)器連接超時(shí)時(shí)間 發(fā)起握手等候響應(yīng)超時(shí)時(shí)間
proxy_connect_timeout 12;
#連接成功后 等待后端服務(wù)器響應(yīng)時(shí)間 其實(shí)已進(jìn)入后端的排隊(duì)之中等候處理
proxy_read_timeout 90;
#后端服務(wù)器數(shù)據(jù)回傳時(shí)間 就是在規(guī)定時(shí)間內(nèi)后端服務(wù)器必須傳完所有數(shù)據(jù)
proxy_send_timeout 90;
proxy_buffer_size 4k;
#同上 告訴Nginx保存單個(gè)用的幾個(gè)Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)國(guó)內(nèi)各大的proxy_buffers 官方推薦 *2
proxy_busy_buffers_size 64k;
#proxy 緩存臨時(shí)文件的大小
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文件,啟動(dòng)nginx。
五、打開(kāi)瀏覽器:
輸入http://192.168.16.16 進(jìn)行訪問(wèn)
測(cè)試:關(guān)掉S1上的網(wǎng)站,再刷新瀏覽器訪問(wèn);關(guān)掉S2上的網(wǎng)站,打開(kāi)S1的網(wǎng)站,刷新瀏覽器訪問(wèn)。
核心代碼1:在http{}里面加入
#設(shè)定負(fù)載均衡的服務(wù)器列表
upstream localhost {
#根據(jù)ip計(jì)算將請(qǐng)求分配各那個(gè)后端tomcat,可以解決session問(wèn)題
ip_hash;
#同一機(jī)器在多網(wǎng)情況下,路由切換,ip可能不同
#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
#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 {}添加
#對(duì) "/" 啟用負(fù)載均衡
location / {
root html;
index index.html index.htm index.aspx;
proxy_redirect off;
#保留用戶真實(shí)信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允許客戶端請(qǐng)求的最大單個(gè)文件字節(jié)數(shù)
client_max_body_size 10m;
client_body_buffer_size 128k;
#跟后端服務(wù)器連接超時(shí)時(shí)間 發(fā)起握手等候響應(yīng)超時(shí)時(shí)間
proxy_connect_timeout 12;
#連接成功后 等待后端服務(wù)器響應(yīng)時(shí)間 其實(shí)已進(jìn)入后端的排隊(duì)之中等候處理
proxy_read_timeout 90;
#后端服務(wù)器數(shù)據(jù)回傳時(shí)間 就是在規(guī)定時(shí)間內(nèi)后端服務(wù)器必須傳完所有數(shù)據(jù)
proxy_send_timeout 90;
proxy_buffer_size 4k;
#同上 告訴Nginx保存單個(gè)用的幾個(gè)Buffer最大用多大空間
proxy_buffers 4 32k;
#如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)國(guó)內(nèi)各大的proxy_buffers 官方推薦 *2
proxy_busy_buffers_size 64k;
#proxy 緩存臨時(shí)文件的大小
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;
}
以下是一些補(bǔ)充工具:
Nginx負(fù)載均衡是一個(gè)很神奇的技術(shù),很多人都不能很好的掌握這個(gè)技術(shù),今天在這里我們向大家詳細(xì)的介紹下有關(guān)Nginx負(fù)載均衡的問(wèn)題。今天小試了一下Nginx負(fù)載均衡,真是爽?。ginx是什么?
首先是配置十分的簡(jiǎn)單,而且功能非常強(qiáng)大。真是相見(jiàn)恨晚。先來(lái)看看配置文件怎么寫(xiě)吧
worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#這里指定多個(gè)源服務(wù)器,ip:端口,80端口的話可寫(xiě)可不寫(xiě)
server 192.168.43.158:80;
server 192.168.41.167;
}
server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}
Nginx負(fù)載均衡有哪些功能呢?
如果后面的服務(wù)器其中一臺(tái)壞了,它能自動(dòng)識(shí)別,更牛的是它好了之后Nginx可以馬上識(shí)別服務(wù)器A和B,如果A的響應(yīng)時(shí)間為3,B的響應(yīng)時(shí)間為1,那么Nginx會(huì)自動(dòng)調(diào)整訪問(wèn)B的概率是A的3倍,真正做到Nginx負(fù)載均衡好的,安裝完成了。我在make的時(shí)候報(bào)了個(gè)錯(cuò),說(shuō)HTTP Rewrite 模塊 有問(wèn)題,我就
./configure –without-http_rewrite_module
然后再make,make install就可以了。
安裝好了之后新建一個(gè)配置文件,把上面的配置文件內(nèi)容拷進(jìn)去,當(dāng)然要修改你的IP,保存為比如 load_balance.conf然后啟動(dòng):
/usr/local/Nginx/sbin/Nginx -c load_balence.conf
Nginx就沒(méi)有這個(gè)限制,對(duì)它來(lái)說(shuō)后面是什么服務(wù)器是完全透名的。Nginx就一點(diǎn)不爽,它本身目前還不能在windows下面跑。寫(xiě)了一大堆,哈哈?!f(shuō)的不對(duì)的大家指出哈
相關(guān)文章
PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯(cuò)誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯(cuò)誤的解決方法,涉及nginx平臺(tái)的相關(guān)配置技巧,需要的朋友可以參考下2016-05-05詳解nginx 代理多個(gè)服務(wù)器(多個(gè)server方式)
本篇文章主要介紹了詳解nginx 代理多個(gè)服務(wù)器(多個(gè)server方式),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10詳解Nginx如何根據(jù)swagger關(guān)鍵字屏蔽頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Nginx 如何根據(jù)swagger關(guān)鍵字屏蔽頁(yè)面的實(shí)現(xiàn)方案,文中有詳細(xì)的解決方案,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08Windows中將Nginx添加為服務(wù)的問(wèn)題
這篇文章主要介紹了Windows中將Nginx添加為服務(wù)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02讓VIM支持Nginx .conf文件語(yǔ)法高亮顯示功能的方法
這篇文章主要給大家介紹了關(guān)于讓VIM支持Nginx .conf文件語(yǔ)法高亮顯示功能的方法,文中分別介紹了手動(dòng)修改和自動(dòng)化腳本兩種方法的實(shí)現(xiàn),都給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07nginx 代理80端口轉(zhuǎn)443端口的實(shí)現(xiàn)
這篇文章主要介紹了nginx 代理80端口轉(zhuǎn)443端口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09