因?yàn)樽罱捻?xiàng)目需要用到負(fù)載平衡,不消思量,雖然用臺(tái)甫鼎鼎的Nginx啦。至于Nginx的先容,這里就不多說(shuō)了,直接進(jìn)入主題如安在Windows下設(shè)置。
我的系統(tǒng)是win7旗艦版的,到官網(wǎng)下載最新版本 nginx/Windows-1.7.9 解壓到英文目次下(我剛開(kāi)始是放到中文目次下的,啟動(dòng)時(shí)會(huì)有問(wèn)題,下面常見(jiàn)錯(cuò)誤里會(huì)講到)。
一、 Nginx設(shè)置
找到 conf 目次里的 nginx.conf 文件,設(shè)置Nginx
#user nobody;
#指定nginx歷程數(shù)
worker_processes 1;
#全局錯(cuò)誤日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 毗連數(shù)上限
worker_connections 1024;
}
#設(shè)定http處事器,操作它的反向署理成果提供負(fù)載平衡支持
http {
#設(shè)定mime范例,范例由mime.type文件界說(shuō)
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"';
#利用哪種名目標(biāo)日志
#access_log logs/access.log main;
#sendfile 指令指定 nginx 是否挪用 sendfile 函數(shù)(zero copy 方法)來(lái)輸出文件,對(duì)付普通應(yīng)用,
sendfile on;
#tcp_nopush on;
#毗連超時(shí)時(shí)間
#keepalive_timeout 0;
keepalive_timeout 65;
#開(kāi)啟gzip壓縮
#gzip on;
#設(shè)定負(fù)載平衡的處事器列表 支持多組的負(fù)載平衡,可以設(shè)置多個(gè)upstream 來(lái)處事于差異的Server.
#nginx 的 upstream 支持 幾 種方法的分派
#1)、輪詢(xún)(默認(rèn)) 每個(gè)請(qǐng)求定時(shí)間順序逐一分派到差異的后端處事器,假如后端處事器down掉,,能自動(dòng)剔除。
#2)、weight 指定輪詢(xún)幾率,weight和會(huì)見(jiàn)比率成正比,用于后端處事器機(jī)能不均的環(huán)境。 跟上面樣,指定了權(quán)重。
#3)、ip_hash 每個(gè)請(qǐng)求按會(huì)見(jiàn)ip的hash功效分派,這樣每個(gè)訪(fǎng)客牢靠會(huì)見(jiàn)一個(gè)后端處事器,可以辦理session的問(wèn)題。
#4)、fair
#5)、url_hash #Urlhash
upstream mysvr {
#weigth參數(shù)暗示權(quán)值,權(quán)值越高被分派到的幾率越大
#1.down 暗示單前的server臨時(shí)不參加負(fù)載
#2.weight 默認(rèn)為1.weight越大,負(fù)載的權(quán)重就越大。
#3.backup: 其它所有的非backup呆板down可能忙的時(shí)候,請(qǐng)求backup呆板。所以這臺(tái)呆板壓力會(huì)最輕。
#server 192.168.1.116 down;
#server 192.168.1.116 backup;
server 192.168.1.121 weight=1;
server 192.168.1.122 weight=2;
}
#設(shè)置署理處事器的地點(diǎn),即Nginx安裝的處事器地點(diǎn)、監(jiān)聽(tīng)端口、默認(rèn)地點(diǎn)
server {
#1.偵聽(tīng)80端口
listen 80;
#對(duì)付server_name,假如需要將多個(gè)域名的請(qǐng)求舉辦反向署理,可以設(shè)置多個(gè)server_name來(lái)滿(mǎn)意要求
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默認(rèn)主頁(yè)目次在nginx安裝目次的html子目次。
root html;
index index.html index.htm;
proxy_pass http://mysvr; #跟載平衡處事器的upstream對(duì)應(yīng)
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
## 界說(shuō)錯(cuò)誤提示頁(yè)面
#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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
二、 啟動(dòng)Nginx
cmd 進(jìn)入Nginx解壓目次 執(zhí)行 start nginx啟動(dòng)Nginx處事
啟動(dòng)后如何查抄是否啟動(dòng)樂(lè)成呢? 輸入呼吁 tasklist /fi "imagename eq nginx.exe" 看到以下信息說(shuō)明啟動(dòng)樂(lè)成了
一切停當(dāng),會(huì)見(jiàn)一下server 里設(shè)置的 server_name 是不是被重定向到 upstream設(shè)置的處事器上了,是不是很簡(jiǎn)樸!
三、常見(jiàn)錯(cuò)誤
假如啟動(dòng)失敗 可以看下logs目次下 error.log 文件里的錯(cuò)誤信息。
我在第一次安裝的時(shí)碰著兩個(gè)錯(cuò)誤,也是最容易遇到的問(wèn)題,在這里列出來(lái)利便各人遇到溝通的問(wèn)題時(shí)快速辦理。
1. 端口占用問(wèn)題