背景:
最近公司分配一个项目,做一个直播APP的后台,像这种随时都有用户在线的情况,后台一定不能down掉,而且只做一台服务器的话压力肯定很大,所以考虑用nginx做负载均衡
环境:
三台linux服务器,一台反向代理服务器,两台负载均衡服务器
反向代理服务器 10.10.10.30 80
负载均衡服务器 10.10.10.40 80
10.10.10.50 80
首先编译安装nginx,安装编译环境:
yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
yum -y install openssl openssl-devel
安装编译环境可能遇到的问题:
要是这样编译的时候还是找不到openssl库,就需要下载openssl源文件,解压后,将路径指定到解压的路径
./configure --prefix=/usr/local/nginx --with-http_ssl_module--with-openssl=/usr/local/src/openssl-xxxx --with-pcre--with-http_stub_status_module
接下来编译安装nginx
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --with-http_ssl_module
make
make install
配置完成后测试是否正常
/usr/local/nginx/sbin/nginx -t
接着启动nginx
/usr/local/nginx/sbin/nginx
若修改后配置文件或者将配置文件覆盖修改的 ,需要执行:
/usr/local/nginx/sbin/nginx -s>
nginx主目录:/usr/local/nginx
nginx主页目录:/usr/local/nginx/html
nginx主配置文件目录:/usr/local/nginx/conf
启动成功之后,浏览器输入http://10.10.10.30/ 可进入测试页面
负载均衡服务器配置:
upstream mynginx {
server 10.10.10.40:80 weight=10;
server 10.10.10.50:80 weight=10;
}
server {
listen 80;
server_name www.asen0713.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://mynginx;
}
#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;
#}
}
配置好了之后重新加载配置文件:/usr/local/nginx/sbin/nginx -s> 注意事项:
1、修改C:\Windows\System32\drivers\etc下的hosts文件加入 10.10.10.30 www.asen0713.com 一行
2、火狐和IE需要按ctrl+F5强制刷新才能看到效果,而谷歌浏览器刚好相反
这样负载均衡就已经配置完了,如果10.10.10.40 80 10.10.10.50 80其中一台down掉,负载均衡服务器会自动分配到另外一台,服务正常访问
常用命令:
启动:/usr/local/nginx/sbin/nginx
以配置文件启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重新加载配置文件:/usr/local/nginx/sbin/nginx -s> 停止:/usr/local/nginx/sbin/nginx -s stop
遗留问题:
1、如果负载均衡服务器down掉,将无法访问
2、配置负载均衡服务器,session也会随之分配到对应服务器,这样session将不能共享,获取资源出现问题
3、文件上传下载也会被分配到不同服务器 |