Nginx负载均衡配置(http代理)


 

Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势。与旧版本(<=2.2)的Apache不同,nginx不采用每客户机一线程的设计模型,而是充分使用异步逻辑,削减了上下文调度开销,所以并发服务能力更强。整体采用模块化设计,有丰富的模块库和第三方模块库,配置灵活。 在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。nginx同时是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

 

今天我的主题呢,主要是Nginx负载均衡实验,把做的步骤记录下来,作为一个学习笔记吧,也可以给大家做下参考。

 

1.实验环境

 

系统版本:CentOS release 5.9 (Final) x86 32位

 

nginx版本: 1.2.8
nginx负载均衡位置:125.208.14.177 80端口
web1 125.208.12.56:80端口
web2 218.78.186.162:8090端口
web3 125.208.14.177:8080端口

 

这里呢,我在web_1和web_2上使用的是系统自带的apache,按要求改变一下监听端口就ok了,当然也可以安装nginx,这个你自己看着办吧,我在125.208.14.177上安装nginx,作为负载均衡器和web服务器使用,负载均衡使用的端口是80,而web服务使用的是8080端口。

 

2:配置文件

 

[root@host-192-168-2-177 conf]# more nginx.conf

 

worker_processes 1;

 

events {

 

    worker_connections 1024;

 

}

 

 
 
 
 
http {

 

 
 upstream site {

 

                server 125.208.12.56:80;

 

                server 218.78.186.162:8090;

 

                server 125.208.14.177:8080;

 

              }
 
 
    include mime.types;

 

    default_type application/octet-stream;

 

    sendfile on;

 

    keepalive_timeout 65;

 

 
 
 
    server {

 

        listen 80;

 

        server_name localhost;

 

        location / {

 

            proxy_pass http://site;
            root /var/www/html;

 

            index index.php;

 

        }

 

        error_page 500 502 503 504 /50x.html;

 

        location = /50x.html {

 

            root html;

 

        }

 

       

 

        location ~ \.php$ {

 

            root /var/www/html;

 

            fastcgi_pass 127.0.0.1:9000;

 

            fastcgi_index index.php;

 

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 

            include fastcgi_params;

 

        }

 

    }

 

   

 

 
      server {

 

        listen 8080;

 

        server_name localhost2;

 

        location / {

 

            root /var/www/html2;

 

            index index.php;

 

        }

 

        error_page 500 502 503 504 /50x.html;

 

        location = /50x.html {

 

            root html;

 

        }

 

 

 

        location ~ \.php$ {

 

            root /var/www/html2;

 

            fastcgi_pass 127.0.0.1:9000;

 

            fastcgi_index index.php;

 

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 

            include fastcgi_params;

 

        }

 

    }

 

   

 

}

 

3:测试

 

[root@host-192-168-2-177 conf]# curl 125.208.14.177

 

404 Not Found

 


404 Not Found

 

--------------------------------------------------------------------------------
nginx

 


[root@host-192-168-2-177 conf]# curl 125.208.14.177
1234
[root@host-192-168-2-177 conf]# curl 125.208.14.177
this dir is /var/www/html2

 

---成功,轮询访问

更多Nginx负载均衡配置相关教程见以下内容

Linux下Nginx+Tomcat负载均衡和动静分离配置要点 

Docker+Nginx+Tomcat7配置简单的负载均衡 

Nginx负载均衡(主备)+Keepalived 

使用Nginx作为负载均衡器

CentOS环境下Nginx实现3台虚拟机负载均衡

Nginx反向代理负载均衡群集实战 

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址

相关内容