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 的负载均衡:
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.测试结果详解:
- 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 |