nginx负载均衡

  • 安装prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。)
yum -y install pcre*

yum -y install openssl*
  • 下载nginx 1.7.8
wget http://nginx.org/download/nginx-1.9.9.tar.gz
  • 解压编译安装
tar -zxvf nginx-1.9.9.tar.gz
  • 然后进入目录编译安装
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx-1.9.9
--with-http_ssl_module --with-http_spdy_module
--with-http_stub_status_module --with-pcre /
make
make install
  • 开启nginx进程
/usr/local/nginx-1.7.8/sbin/nginx
  • 重启或关闭进程:
/usr/local/nginx-1.7.8/sbin/nginx -s reload
/usr/local/nginx-1.7.8/sbin/nginx -s stop
  • 关闭防火墙,或者添加防火墙规则就可以测试了。
service iptables stop

或者编辑配置文件:

vi /etc/sysconfig/iptables

添加这样一条开放80端口的规则后保存:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启服务即可:

service iptables restart
  • 配置负载均衡

只需要修改http{}之间的内容就行了,修改的第一个地方就是设置服务器组,在http节点之间添加

upstream myServer{
    server www.88181.com:80;  #这里是你自己要做负载均衡的服务器地址1
    server www.linux.com:8080; #这里是要参与负载均衡的地址2
}

将请求指向myServer

 location / {
    proxy_pass  http://myServer;
}

实例:

完整的文件(删除注释)如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream myServer{
    server www.linux.com:80;
    server www.88181.com:8080;
 }
server {
    listen      80;
    server_name  my22;
    location / {
    proxy_pass  http://myServer;
}

你可能感兴趣的:(nginx负载均衡)