模拟均衡负载环境: 一台ubuntu15.04做Nginx分发服务器(192.168.0.101),两台window2008做Apache web服务器(192.168.0.15、192.168.0.26)
1、部署Nginx分发服务器(192.168.0.101)
1)、在ubuntu15.04服务器上安装Nginx服务
numbgui@numbgui:/home/numbgui$ wget http://nginx.org/download/nginx-1.5.9.tar.gz
下载完成后解压缩到目录,执行 ./configure,如果无法完成,按照错误提示安装pcre、zlib
numbgui@numbgui:/home/numbgui/Public/nginx-1.5.9$ sudo apt-get install libpcre3 libpcre3-dev
numbgui@numbgui:/home/numbgui/Public/nginx-1.5.9$ sudo apt-get install zlib1g-dev
执行完成后重新执行 ./configure 来检测你的安装平台的目标特征
numbgui@numbgui:/home/numbgui/Public/nginx-1.5.9$ sudo ./confugure
检测通过没有问题之后执行,make操作
numbgui@numbgui:/home/numbgui/Public/nginx-1.5.9$ sudo make
ps: make是用来编译的,它从Makefile中读取指令,然后编译
编译完成会后执行make install 安装
numbgui@numbgui:/home/numbgui/Public/nginx-1.5.9$ sudo make install
到这里Nginx服务在ubuntu15.04中已经安装完成
2)、启动Nginx服务
启动Nginx服务
numbgui@numbgui:/home/numbgui$/usr/local/nginx/sbin/nginx -s reload
显示如下界面表示启动成功
如果启动Nginx服务提示是 :丢失nginx.pid
执行以下语句可以解决:
numbgui@numbgui:/home/numbgui$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3)、配置Nginx均很负载(简单模拟)
编辑/usr/local/nginx/conf/nginx.conf 配置文件,如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pidlogs/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
#sendfileon;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#这里配置均很负载的服务地址,我这里配置的为两台win2008下的apache web服务,均在80端口下,地址后面的参数:weight--轮询几率
upstream apache{
server 192.168.0.15:80 weight=5 max_fails=3 fail_timeout=10s;
server 192.168.0.26:80 weight=5 max_fails=3 fail_timeout=10s;
}
server {
listen 80;
#这里server_name 配置上面 upstream配置的名字(这里为apache)
server_name apache;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
#http://后面与的参数与server_name相同(这里为apache)
proxy_pass http://apache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP$remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
# includefastcgi_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 服务
numbgui@numbgui:/usr/local/nginx/conf$ sudo ./nginx -s reload
2、部署window2008 Apache web服务(192.168.0.15、192.168.0.26)
关于apache服务的部署,这里不再赘述,如有疑问请自行查询相关资料
即可完成简单均很负载配置;
效果如下:
ps: 访问192.168.0.101 Nginx服务器,通过轮询的方式计算并转发给26或者15服务器的apache web服务;
在Arch上使用Nginx/Apache安装RainLoop Webmail:http://www.linuxdiyf.com/linux/10574.html
Ubuntu 14.04 64bit编译安装Nginx1.7+PHP5.4+MySQL5.6:http://www.linuxdiyf.com/linux/10608.html
CentOS 7用户怎样安装LNMP(Nginx+PHP+MySQL):http://www.linuxdiyf.com/linux/10460.html
CentOS下安装LEMP服务(Nginx、MariaDB/MySQL和PHP):http://www.linuxdiyf.com/linux/10318.html
CentOS+Nginx一步一步开始配置负载均衡:http://www.linuxdiyf.com/linux/10205.html