由於最近的項目須要用到負載均衡,不用考慮,固然用大名鼎鼎的Nginx啦。至於Nginx的介紹,這裏就很少說了,直接進入主題如何在Windows下配置。php
個人系統是win7旗艦版的,到官網下載最新版本 nginx/Windows-1.7.9 解壓到英文目錄下(我剛開始是放到中文目錄下的,啓動時會有問題,下面常見錯誤裏會講到)。html
1、 Nginx配置nginx
找到 conf 目錄裏的 nginx.conf 文件,配置Nginx後端
#user nobody;
#指定nginx進程數
worker_processes 1;
#全局錯誤日誌及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 鏈接數上限
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型,類型由mime.type文件定義
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;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
sendfile on;
#tcp_nopush on;
#鏈接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
#開啓gzip壓縮
#gzip on;
#設定負載均衡的服務器列表 支持多組的負載均衡,能夠配置多個upstream 來服務於不一樣的Server.
#nginx 的 upstream 支持 幾 種方式的分配
#1)、輪詢(默認) 每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
#2)、weight 指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。 跟上面樣,指定了權重。
#3)、ip_hash 每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
#4)、fair
#5)、url_hash #Urlhash
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的概率越大
#1.down 表示單前的server暫時不參與負載
#2.weight 默認爲1.weight越大,負載的權重就越大。
#3.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。
#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;
}
#配置代理服務器的地址,即Nginx安裝的服務器地址、監聽端口、默認地址
server {
#1.偵聽80端口
listen 80;
#對於server_name,若是須要將多個域名的請求進行反向代理,能夠配置多個server_name來知足要求
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默認主頁目錄在nginx安裝目錄的html子目錄。
root html;
index index.html index.htm;
proxy_pass http://mysvr; #跟載均衡服務器的upstream對應
}
#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 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;
# }
#}
}
2、 啓動Nginx
緩存
cmd 進入Nginx解壓目錄 執行 start nginx啓動Nginx服務服務器
啓動後如何檢查是否啓動成功呢? 輸入命令 tasklist /fi "imagename eq nginx.exe" 看到如下信息說明啓動成功了session
一切就緒,訪問一下server 裏配置的 server_name 是否是被重定向到 upstream配置的服務器上了,是否是很簡單!app
3、常見錯誤負載均衡
若是啓動失敗 能夠看下logs目錄下 error.log 文件裏的錯誤信息。dom
我在第一次安裝的時遇到兩個錯誤,也是最容易碰到的問題,在這裏列出來方便你們碰到相同的問題時快速解決。
1. 端口占用問題
個人配置文件裏服務偵聽的是 80 端口,因爲機器上部署了IIS,80端口被默認站點佔用,把站點關閉就能夠了,這個問題在錯誤日誌裏記錄是這樣的。
2015/01/15 10:44:12 [emerg] 8800#5988: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
碰到相似的錯誤,請確認端口是否被佔用或被防火牆屏蔽
2.Nginx所在目錄有中文
錯誤日誌大體輸出一下內容
2015/01/15 11:55:55 [emerg] 5664#8528: CreateFile() "E:\軟件\nginx-1.7.8/conf/nginx.conf" failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)
3. 啓用緩存時報錯
2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A20000
2015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1
我一直沒有找到解決的方法,有人說重啓服務,或者緩存設置大一點就能夠了,我試了一下沒有用的,官網 原文是這樣講的,只能認爲windwos下無解了。
: The cache and other modules which require shared memory support do
: not work in Windows Vista and later due to address space layout
: randomization being enabled in these Windows versions.