因為最近的項目需要用到負載平衡,不消思量,雖然用臺甫鼎鼎的Nginx啦。至于Nginx的先容,這里就不多說了,直接進入主題如安在Windows下設置。
我的系統是win7旗艦版的,到官網下載最新版本 nginx/Windows-1.7.9 解壓到英文目次下(我剛開始是放到中文目次下的,啟動時會有問題,下面常見錯誤里會講到)。
一、 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;
# }
#}
}
二、 啟動Nginx
cmd 進入Nginx解壓目次 執行 start nginx啟動Nginx處事
啟動后如何查抄是否啟動樂成呢? 輸入呼吁 tasklist /fi "imagename eq nginx.exe" 看到以下信息說明啟動樂成了
一切停當,會見一下server 里設置的 server_name 是不是被重定向到 upstream設置的處事器上了,是不是很簡樸!
三、常見錯誤
假如啟動失敗 可以看下logs目次下 error.log 文件里的錯誤信息。
我在第一次安裝的時碰著兩個錯誤,也是最容易遇到的問題,在這里列出來利便各人遇到溝通的問題時快速辦理。
1. 端口占用問題