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

Nginx负载均衡配置实战

117次阅读
没有评论

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

今天给网站加上负载均衡,一切顺利,由于需要用到第三方的模块,所以需要重新编译 Nginx,写一下过程,刚刚写了一个 Nginx 升级过程,这里沿用刚才的环境,还需要下载 nginx-upstream-fair

编译升级过程

 # 下载解压 nginx-upstream-fair
 $ curl https://codeload.github.com/gnosek/nginx-upstream-fair/zip/master > nginx-upstream-fair.zip
 $ unzip nginx-upstream-fair.zip
 $ cd nginx-1.6.2
 # 查看当前 nginx 编译参数
 $ nginx -V
 # 输出 configure arguments: –prefix=/usr/local/nginx
 # 加上编译参数,加入刚才下载的模块
 $ ./configure –prefix=/usr/local/nginx –add-module=../nginx-upstream-fair-master
 # 编译
 $ make
 # 如果没有什么问题,应该是编译成功的
 # 先把 nginx 停了,然后备份一下
 $ nginx -s stop
 $ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.6
 # 把刚才编译好的 nginx 二进制文件,覆盖现在文件
 $ cp -f ./objs/nginx /usr/local/nginx/sbin/nginx
 $ 启动 nginx
 $ nginx
 # 查看当前 nginx 编译参数
 $ nginx -V
 # 输出 configure arguments: –prefix=/usr/local/nginx –add-module=../nginx-upstream-fair-master
 # 说明编译成功了

接下来就是配置了

后端 webservice 配置,由于后端接口跟状态无关,所以这里用 fair 策略,也就是刚才编译的模块

 upstream webservice {
     fair;
     server localhost:8180;
     server localhost:8181;
 }

 server {
     listen 9999;
     server_name localhost;

    location / {
        proxy_pass  http://webservice;

        #Proxy Settings
        proxy_redirect    off;
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout        90;
        proxy_read_timeout        90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;

        ## set upload file size
        client_max_body_size 20m;
 } 

前端网站 配置,前端一般都涉及 session 问题,要做负载均衡,要么做 session 同步,要么就将用户请求都转发都一个 tomcat,我这里用的是后者,简单是一种美,简单也不容易出错

 upstream site {
     ip_hash;
     server localhost:8280;
     server localhost:8281;
 }

 server {
     listen 80;
     server_name localhost;

    location / {
        proxy_pass  http://site;

        #Proxy Settings
        proxy_redirect    off;
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout        90;
        proxy_read_timeout        90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;

        ## set upload file size
        client_max_body_size 20m;
 } 

OK,一切顺利,重启 nginx 即可,如果访问量大,继续增加节点即可,最重要的还是要做缓存,并且把静态文件分离出来,这样后台的 tomcat 任务基本就是访问后端,然后返回页面,只需要一个请求,压力大大减小了

————————————– 分割线 ————————————–

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

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

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