Nginx+LNMP+负载均衡 学习笔记

NGINX相关知识

---------------------------------------

nginx 和 tengine(淘宝)  类似,

1.查看网站的head头:

curl -I www.51cto.com

2.安装nginx步骤:

一。先安装pcre依赖,rewrite需要用:

yum install pcre pcre-devel openssl-devel

二。到http://nginx.org官网下载稳定版本:

useradd nginx -s /sbin/nologin -M
tar -zxvf nginx-1.10.2.tar.gz
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make && make install

3.查看安装时的参数:

 ./sbin/nginx -V

4.配置文件:

worker_processes  1;      线程数,配置成CPU核数
worker_connections  1024;   每个worker允许的最大并发数

  默认虚拟主机配置:   

        server {
                listen          80;
                server_name     bbs.wmj.com;
                access_log  logs/www_access.log main;
                location / {
                        root html/bbs;
                        index  index.html index.htm;
                }
}

5.生效配置:

  ./sbin/nginx -t   测试配置文件是否正确

  ./sbin/nginx -s reload  加载配置文件

6.使用include优化配置文件: 

 

include  vhost/*.conf;
7.查看nginx的状态信息:

 

  vim conf/vhost/status.conf

server{
        listen  80;
        server_name     status.wmj.com;
        location / {
          stub_status on;
          access_log  off;
        }
}
 

 

 

8.error日志配置:

 vim conf/nginx.conf

 

error_log  logs/error.log  error;

可以放在  http,server 头里面
9.访问日志配置:

 

 vim conf/nginx.conf

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
 vim conf/vhost/bbs.conf

 

 

access_log  logs/bbs_access.log main;
 

 10.重定向rewrite的使用:

  

        server {
                listen          80;
                server_name     rewrite.wmj.com;
                access_log  logs/www_access.log main;
                rewrite ^/(.*) http://www.wmj.com/$1 permanent;
}

将"http://rewrite.wmj.com/wmj.html"重定向到"http://www.wmj.com/wmj.html"

permanent: 表示301重定向,没有的话是302


二. LNMP架构

----------------------------------

1.FastCGI的工作原理:


2.安装php-5.5.38:

 先到官网上面下载php包:http://cn.php.net/releases/

 安装相关依赖:

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo    (加载阿里云repo源)
yum install zlib-devel libxml2-devel libjpeg-devel libmcrypt-devel
yum install freetype-devel libpng-devel gd-devel curl-devel libxslt-devel
yum -y install libmcrypt-devel mhash mhash-devel mcrypt

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
./configure --prefix=/usr/local/libiconv
make && make install

  开始安装php:

tar xf php-5.5.38.tar.gz
cd php-5.5.38
./configure \
--prefix=/usr/local/php5.5.38 \
--with-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
make 
make install

3.复制默认的配置文件:

  cp php.ini-production /usr/local/php5.5.38/lib/php.ini

  cd /usr/local/php5.5.38/etc/

  cp php-fpm.conf.default php-fpm.conf

4.修改配置文件:

   vim php-fpm.conf

log_level = error
rlimit_files = 65535
pm.max_children = 1024
pm.start_servers = 16
pm.min_spare_servers = 5
pm.max_spare_servers = 20

5.启动Php:

/usr/local/php5.5.38/sbin/php-fpm

6.配置php的nginx虚拟主机:

 vim vhost/blog.conf

        server {
                listen          80;
                server_name     blog.wmj.com;
                access_log  logs/blog_access.log main;
                root html/blog;
                location / {
                        index  index.html index.htm;
                }
                location ~ .*\.(php|php5)?$ {
                        fastcgi_pass  127.0.0.1:9000;
                        fastcgi_index index.php;
                        include  fastcgi.conf;
                }
}
~
7.php测试:

 vim ../../html/blog/phpinfo.php

<?php
phpinfo();
?>

三. Nginx 的负载均衡:
-------------------------------
1.调度算法种类:
   轮询(rr), 权重轮询(wrr),ip_hash(会话保持)

2.配置nginx配置文件:
  
upstream backend {
    server 172.16.1.214:80   weight=5;
    server 172.16.1.210:80   max_fails=2 fail_timeout=30s;
    server 172.16.1.215:80   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
3.将请求的域名和客户端的IP传送到后端:
  
server {
    location / {
        proxy_pass http://backend;
        proxy_set_header Host       $host;        #后端会执行前端访问的域名
        proxy_set_header X-Forwarded-For $remote_addr;  #把客户端的IP传给后端
	proxy_set_header X-Real-IP $remote_addr;
    }
}

4. 生产环境优化反向代理proxy参数配置:
  
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_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;}   大括号里面的根据实际情况调整,也可以不设置用默认
将它保存成文件,然后用include导入。
server {
    location / {
        proxy_pass http://backend;
        include  proxy.conf;
    }
}

5.根据扩展名来实现动静分离:
                location ~ .*.(gif|jpg|png|bmp)$ {
                        proxy_pass http://image;
                        include proxy.conf;
                }


四.Nginx架构师知识


1.Nginx指南:

http://www.jb51.net/books/24955.html

2.Nginx突破10W并发:

http://www.cnblogs.com/zhangrumingbj/p/3887693.html

3.性能测试两个指标:

吞吐率,响应时间

4.用ab进行性能测试:

yum install httpd-tools
ab -n 10000 -c 100 http://172.16.1.211/
-n: 总请求数
-c: 并发数

5.测试结果详解:

Server Software:        Apache/2.2.19    ##apache版本 
Server Hostname:        vm1.jianfeng.com   ##请求的主机名 
Server Port:            80 ##请求端口

Document Path:          /a.html 
Document Length:        25 bytes  ##页面长度,不包含头信息

Concurrency Level:      100  ##并发数 
Time taken for tests:   0.273 seconds  ##处理所有请求共使用了多少时间 
Complete requests:      1000   ##总请求数 
Failed requests:        0   ##失败请求数 
Write errors:           0   
Total transferred:      275000 bytes  ##总共传输字节数,包含http的头信息等 
HTML transferred:       25000 bytes  ##html字节数,实际的页面传递字节数 ,不包含头信息
Requests per second:    3661.60 [#/sec] (mean)  ##每秒处理多少请求,非常重要的参数值,服务器的吞吐量 
Time per request:       27.310 [ms] (mean)  ##用户平均请求等待时间,也非常重要 
Time per request:       0.273 [ms] (mean, across all concurrent requests)  ##服务器平均处理时间,也就是服务器吞吐量的倒数 
Transfer rate:          983.34 [Kbytes/sec] received  ##每秒获取的数据长度

Connection Times (ms) 
              min  mean[+/-sd] median   max 
Connect:        0    1   2.3      0      16 
Processing:     6   25   3.2     25      32 
Waiting:        5   24   3.2     25      32 
Total:          6   25   4.0     25      48

Percentage of the requests served within a certain time (ms) 
  50%     25  ## 50%的请求在25ms内返回 
  66%     26  ## 60%的请求在26ms内返回 
  75%     26 
  80%     26 
  90%     27 
  95%     31 
  98%     38 
  99%     43 
 100%     48 (longest request
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

6.多次测试结果分析:

#按比例增加并发数
ab -n 10000 -c 100 http://172.16.1.211/
ab -n 10000 -c 200 http://172.16.1.211/
ab -n 10000 -c 250 http://172.16.1.211/
ab -n 10000 -c 300 http://172.16.1.211/
ab -n 10000 -c 400 http://172.16.1.211/
并发数 每秒处理多少请求数 平均请求等待时间
100 6059 16
200 5413 36
250 3266 76
300 3255 92
400 3283 121

从上面表格看出,并发200开始每秒请求数和等待时间都开始变差,从而可以得出服务器的合理并发大概为200


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值