在虚拟机上使用nginx实现负载均衡

前提:准备三台虚拟机,其中一台作为负载均衡服务器,安装nginx,另外两台作为相应的服务器,需要安装httpd

1、在安装nginx的虚拟机上配置nginx.conf文件,而达到负载均衡
进入nginx.conf文件中找到http,在其包括的范围内找到 server,在server外配置另外两台虚拟机的ip和端口号,格式是:upstream name(这个名字是任意的) {server IP:PORT; server IP:PORT; …},然后在 server 内添加 location url(url即匹配的路径){ proxy_pass http://name/; }
到这关于nginx上的配置就完成了,然后重新启动nginx就行了,下边是一个配置文件的例子。
nginx重新加载:在/sbin目录下 ./nginx -s reload,./nginx -t 测试配置文件修改是否成功。

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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   upstream hes {
        server 192.168.109.68:80;
        server 192.168.109.69:80;
   }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /ooxx {
                proxy_pass http://hes/;
        }

2、在另外两台虚拟机上安装 httpd 使用 yum install httpd -y ,安装完成后启动 service httpd start。

你可能感兴趣的:(高并发负载均衡)