阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

183次阅读
没有评论

共计 2802 个字符,预计需要花费 8 分钟才能阅读完成。

 

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 负载均衡和动静分离配置要点  http://www.linuxidc.com/Linux/2016-01/127255.htm

Docker+Nginx+Tomcat7 配置简单的负载均衡  http://www.linuxidc.com/Linux/2015-12/125907.htm

Nginx 负载均衡(主备)+Keepalived  http://www.linuxidc.com/Linux/2015-12/126865.htm

使用 Nginx 作为负载均衡器 http://www.linuxidc.com/Linux/2015-12/125789.htm

CentOS 环境下 Nginx 实现 3 台虚拟机负载均衡 http://www.linuxidc.com/Linux/2015-12/125875.htm

Nginx 反向代理负载均衡群集实战  http://www.linuxidc.com/Linux/2015-08/122111.htm

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-02/128063.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计2802字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中