nginx

作者: Miracle001 | 来源:发表于2018-04-15 08:35 被阅读2次
image.png image.png image.png image.png
网站:http://nginx.org/
此版本号为偶数--nginx-1.10.2--稳定版--适合企业使用
此版本号为奇数--nginx-1.13.6--主线版--不建议企业使用

centos7.3
   直接下载rpm包,安装即可
   版本要求"1.10.2"--否则需安装依赖包--libcrypto.so.10(OPENSSL_1.0.1)(64bit)
http://nginx.org/packages/centos/7/x86_64/RPMS/  -->  下载rpm包
    nginx-1.10.2-1.el7.ngx.x86_64.rpm
rz  上传
rpm -ivh nginx-1.10.2-1.el7.ngx.x86_64.rpm

centos7.4
网络文件配置(NAT模式的子网IP和桥接模式的一般IP)
    NAT模式子网IP:192.168.227.3~254
    桥接模式IP:192.168.1.41(变为固定IP后,不能连网,所以用NAT模式)
主机名解析(nameserver)
    vim /etc/resolv.conf
        nameserver 8.8.8.8
        nameserver 8.8.4.4
        search 4-2.fgq.com
方法1:安装epel源        
http://archives.fedoraproject.org/pub/epel/  -->  安装epel源(随笔中有)
rz
yum -y install epel-release-latest-7.noarch.rpm
    生成两个文件:epel.repo(关键)  epel-testing.repo
vim /etc/yum.repos.d/epel.repo
    把[epel]中的baseurl的"#"去掉,metalink行加"#"注释掉,其他两个不需要更改
方法2:安装nginx源--不建议使用
vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx
    baseurl=http://nginx.org/packages/centos/7/x86_64/
    gpgcheck=0
    enabled=1

yum clean all; yum repolist  出现epel源
yum info epel-release
yum install nginx  (默认安装的版本是1.12.2)
rpm -qi nginx
rpm -ql nginx
/etc/nginx/nginx.conf  主配置文件
/usr/sbin/nginx  主程序文件
/usr/share/nginx/html/  nginx的默认测试页面
/usr/lib64/nginx/modules  模块文件
ls /usr/lib64/nginx/modules  centos7.4有模块内容
/usr/lib/systemd/system/nginx.service  服务
nginx -h  帮助,好好看看各个选项的意义
nginx -T  显示生成的配置文件信息,排错使用
nginx -t  检查语法,没有错误
ss -ntl  80端口没有被占用
systemctl start nginx.service
ss -ntl  80端口出现
浏览器:(IP是桥接模式的IP)
192.168.1.7  nginx的测试页面
192.168.1.41  nginx的测试页面

mkdir -pv /www/vhost1
vim /www/vhost1/index.html
<h1>Test Page (Nginx-1.10.2)</h1>
cd /etc/nginx
cp nginx.conf{,.bak}
vim nginx.conf 或 
vim conf.d/vhost1.conf
server {
  server_name www.ilinux.io;
  listen 8080;  80端口被占用,可以使用8080
  root "/www/vhost1";  中间无空格,可以省略引号,但是分号必须加
}
nginx -t  语法检查
nginx -s reload  不用重启,只需加载即可
ss -ntl  8080端口已经监听
浏览器:
192.168.1.7:8080  我们定义的nginx页面
192.168.1.41:8080  我们定义的nginx页面
模块化的好处:
创建虚拟主机,就是创建一个文件,删除--只需要把创建的文件删除即可,不需要更改主配置文件


nginx文档:http://nginx.org/en/docs/
配置指令
vim /etc/nginx/nginx.conf

正常运行必备的配置
1 user nginx;  定义worker进程的身份(ps aux:可以看到)
2 error_log /var/log/nginx/error.log;  
3 pid /run/nginx.pid;  指定存储nginx主进程进程号码的文件路径
4 "include /usr/share/nginx/modules/*.conf;"  指明包含进来的其它配置文件片断
5 load_module file;  指明要装载的动态模块,默认没有,可手动添加

性能优化相关的配置
1 worker_processes auto;  
    worker_processes number | auto;
    worker进程的数量;通常应该等于小于当前主机的cpu的物理核心数;
    auto:当前主机物理CPU核心数;
2 worker_cpu_affinity auto ;  nginx进程的CPU亲缘性,默认没有,可手动添加
  或worker_cpu_affinity cpumask ...;
  worker_cpu_affinity 0000 0001 0000 0010 0000 0100;  
    CPU MASK:
    00000000:
    0000 0001:0号CPU
    0000 0010:1号CPU
    0000 0100:2号CPU
    ... ...
    cpumask可倒过来写,自己设定;一个CPU对应一个nginx;
    watch -n 0.5 'ps axo pid,comm,psr|grep nginx'  观察CPU对应的nginx
3 worker_priority number;  指定worker进程的nice值,设定worker进程优先级;[-20,20]
    值越小,优先级越高,默认没有,可手动添加
    watch -n 0.5 'ps axo pid,comm,psr,ni|grep nginx'  默认为0
4 worker_rlimit_nofile number;  worker进程所能够打开的文件数量上限

调试、定位问题
1 daemon on|off;  是否以守护进程方式运行Nignx;默认没有,可手动添加
  centos7--off--systemd接管
  centos6--on--init.d
  docker--on  另一个区别于nginx服务
2 master_process on|off;  是否以master/worker模型运行nginx;默认为on;
  如果为off,这个进程既接受请求,又发送请求--单进程结构
  当把所有内容输出到控制台,进行定位时,才这样做,否则,为on
3 error_log file [level];  日志级别

事件驱动相关的配置
  events {
    ...
  } 
1、worker_connections number;
    每个worker进程所能够打开的最大并发连接数数量;
    worker_processes * worker_connections
2、use method;  默认无
    指明并发连接请求的处理方法;          
    use epoll;  
    user select;  最大1024,不实用
        因为worker_processes * worker_connections数量远远大于1024
3、accept_mutex on | off;  默认无
    处理新的连接请求的方法;
    on意味着由各worker轮流处理新请求;-->开启
    off意味着每个新请求的到达都会通知所有的worker进程;-->惊群效应--有损性能

http协议的相关配置--看文档

---------------------------------------------------------------------------------
此实验需要买服务器(未做)
Vhost1--80端口--Test Page--www.fgq.com
Vhost2--80端口--Vhost2(/www/vhost2/index.html)--www.feng.com(/etc/nginx/conf.d/vhost2.conf)
vim /etc/hosts
192.168.1.41 www.fgq.com www.feng.com
浏览器:输入不同的网站,二者显示不同的页面
www.fgq.com
www.feng.com

centos7.4
以前面的实验为基础--Vhost1端口是8080
我们自己定义"错误页面,页面生成的状态码"
浏览器:
192.168.1.41:8080  我们定义的nginx页面
192.168.1.41:8080/a.com  nginx的错误界面--我们自己定义错误界面
mkdir /www/error_page
vim /www/error_page/40x.html
    <h1>Error Page 40x</h1>
vim /etc/nginx/conf.d/vhost1.conf
server {
        server_name www.fgq.com;
        listen 8080;
        location / {
                root "/www/vhost1";
        }

        error_page 404 /40x.html;
        location = /40x.html {
                root "/www/error_page";
        }
}
nginx -t ; nginx -s reload
浏览器:
192.168.1.41:8080/a.com  我们定义的nginx错误页面
F12--F5--分析页面--404的状态码-->我们把404的错误变为成功响应
vim /etc/nginx/conf.d/vhost1.conf  更改error_page行即可
    error_page 404 =200 /40x.html;    页面生成的状态码改为我们自己定义的
nginx -t ; nginx -s reload
浏览器:
192.168.1.41:8080/a.com  
F12--F5--200的状态码-->成功响应

ngx_http_access_module模块
    实现基于ip的访问控制
vim /etc/nginx/conf.d/vhost1.conf  
在server内部添加如下信息即可
location /admin/ {
    root "/www/vhost1";
    allow 192.168.1.7;
    deny all;
}
mkdir /www/vhost1/admin
vim /www/vhost1/admin/index.html
  <h2>Admin Area</h2>
nginx -t ; nginx -s reload
centos7
curl 192.168.1.41:8080/admin/  ok,注意admin后面的"/"不能少
vim /etc/hosts
192.168.1.41 www.fgq.com
curl http://www.fgq.com:8080/admin/  ok
centos7.4-2: curl 192.168.1.41:8080/admin/  no

ngx_http_auth_basic_module模块
    实现基于用户的访问控制,使用basic机制进行用户认证
vim /etc/nginx/conf.d/vhost1.conf  
更改"location /admin/"所包含的内容
location /admin/ {
    root "/www/vhost1/";
    auth_basic "Admin Area";
    auth_basic_user_file "/etc/nginx/.ngxpasswd";
}
yum -y install httpd-tools  提供htpasswd这个工具
htpasswd -c -m /etc/nginx/.ngxpasswd fgq
    -c  第一次使用,要加此选项
    -m  使用md5加密  
    指定生成的加密文件和用户名
htpasswd -m /etc/nginx/.ngxpasswd feng
cat /etc/nginx/.ngxpasswd  加密文件
nginx -t ; nginx -s reload
浏览器:192.168.1.41:8080/admin/  弹出对话框--要求输入用户名和密码--即可访问

ngx_http_stub_status_module模块
    用于输出nginx的基本状态信息
nginx -V  显示编译时的"configure arguments",有"--with-http_stub_status_module"
vim /etc/nginx/conf.d/vhost1.conf  
在server中添加内容
location /status {
    root "/www/vhost1";
    stub_status;
}
nginx -t ; nginx -s reload
浏览器:192.168.1.41:8080/status/  出现nginx状态信息
curl 192.168.1.41:8080/status/  也ok
    Active connections: 活动状态的连接数;
    accepts:已经接受的客户端请求的总数;
    handled:已经处理完成的客户端请求的总数;
    requests:客户端发来的总的请求数;
    Reading:处于读取客户端请求报文首部的连接的连接数;
    Writing:处于向客户端发送响应报文过程中的连接数;
    Waiting:处于等待客户端发出请求的空闲连接数;
将来可能会生成脚本,输出至zabix,用图形化方式进行展示
curl -s 192.168.1.41:8080/status|awk -F: '/^Active/{print $2}'  活动状态的连接数
    -s  减少输出的信息,比如进度
curl -s 192.168.1.41:8080/status|awk 'NR==3{print $2}'  已经接受的客户端请求的总数--accepts
curl -s 192.168.1.41:8080/status|awk 'NR==3{print $3}'  已经处理完成的客户端请求的总数--handled

image.png
ngx_http_log_module模块
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]
某个虚拟主机使用单独的访问日志
centos7.4
vim /etc/nginx/conf.d/vhost1.conf  
  在server中添加一条信息
  access_log /var/log/nginx/vhost1_access.log main;
nginx -t ; nginx -s reload
centos7.4-2:curl -s 192.168.1.41:8080/status  访问一下,产生日志
tail /var/log/nginx/vhost1_access.log  

ngx_http_gzip_module--压缩模块
    节省带宽--主要压缩文本,其他看情况--不要全部都压缩,会减少CPU使用寿命
    CPU空闲--增大压缩比,节约更多的带宽
vim /etc/nginx/conf.d/gzip.conf
  gzip  on;  开启
  gzip_comp_level 6;  压缩级别
  gzip_min_length 64;  小于64字节不压缩
  gzip_proxied any;  nginx作为代理服务器,后端服务器提供数据,都压缩
  gzip_types text/xml text/css  application/javascript;  压缩什么类型的数据
nginx -t ; nginx -s reload
cp /var/log/messages /www/vhost1/messages.html
chmod +r /www/vhost1/messages.html
curl -I --compressed 192.168.1.41:8080/messages.html
    -I  只显示请求头信息 
    --compressed  压缩
    显示Content-Encoding: gzip
浏览器:192.168.1.41:8080/messages.html--F12显示Content-Encoding:gzip

电脑更新,IP重置了
centos7.3--192.168.200.127(仅主机)+桥接模式(IP--dhcp)
centos7.4--192.168.200.131(仅主机)+桥接模式(IP--dhcp)
centos7.4--192.168.200.132(仅主机)+桥接模式(IP--dhcp)

ngx_http_ssl_module模块
centos7.4
生成私钥
cd /etc/pki/CA;
openssl genrsa -out private/cakey.pem 4096
私钥文件权限600
chmod go= private/cakey.pem 
ll private/cakey.pem
生成自签证书
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
    CN--HA--Zhengzhou--fgq.com--beiguobu--ca.fgq.com--回车--结束
touch index.txt
echo 01 > serial
给nginx发证书
生成密钥
mkdir /etc/nginx/ssl ; cd /etc/nginx/ssl
(umask 077;openssl genrsa -out nginx.key 2048)
生成签署请求
openssl req -new -key nginx.key -out nginx.csr
    CN--HA--Zhengzhou--fgq.com--beiguobu--www.fgq.com--回车--回车--回车--结束
签发证书
openssl ca -in nginx.csr -out nginx.crt
    y--y--结束
ssl基于IP地址创建
多个IP地址,但只创建一个主机,那么只能为一个虚拟主机提供ssl服务
cd /etc/nginx/conf.d
cp vhost1.conf vhost1.ssl.conf 
vim vhost1.ssl.conf 
只更改listen和添加ssl信息即可,其他不变
server {
        server_name www.fgq.com;
        listen 443 ssl;  要求:必须只能通过ssl来通信
        root "/www/vhost1";

        error_page 404 =200 /40x.html;
        location = /40x.html {
                root "/www/error_page";
        }

        location /admin/ {
                auth_basic "Admin Area";
                auth_basic_user_file "/etc/nginx/.ngxpasswd";
        }
        location /status {
                stub_status;
        }

        access_log /var/log/nginx/vhost1_access.log main;

        ssl on;  启用ssl功能
        ssl_certificate /etc/nginx/ssl/nginx.crt;  证书
        ssl_certificate_key /etc/nginx/ssl/nginx.key;  密钥文件
        ssl_session_cache shared:sslcache:10m;  打开ssl缓存,缓存名字及空间大小
        ssl_session_timeout 600s;  缓存的ssl参数的有效时长

}
nginx -t ; nginx -s reload
ss -tnl  443端口
浏览器:"https://192.168.200.131"  ok--证书不被信任


ngx_http_rewrite_module模块
http-->跳转至https
vim /etc/nginx/conf.d/vhost1.conf 
只添加rewrite规则即可,其他不变
server {
        server_name www.fgq.com;
        listen 8080;
        root "/www/vhost1";

        rewrite /(.*)$ https://192.168.200.131/$1;    $1--后项引用,临时重定向--302
            或者
            rewrite /(.*)$ https://192.168.200.131/$1 permanent;  永久重定向--301

        error_page 404 =200 /40x.html;
        location = /40x.html {
                root "/www/error_page";
        }

        location /admin/ {
                auth_basic "Admin Area";
                auth_basic_user_file "/etc/nginx/.ngxpasswd";
        }
        location /status {
                stub_status;
        }

        access_log /var/log/nginx/vhost1_access.log main;

}
nginx -t ; nginx -s reload
浏览器:
192.168.200.131:8080-->跳转至"https://192.168.200.131:8080"--F12--200(status)
"http://192.168.200.131:8080"-->跳转至"https://192.168.200.131:8080"--F12--302/1
访问www.fgq.com/bbs/-->跳转至bbs.fgq.com/(未作)
访问本站内的url-->跳转至另一个url  如:访问wmv--替换为mps格式(未作)


ngx_http_referer_module模块--防止盗链
ngx_http_proxy_module模块--代理
生产中的服务器--3个接口
  一个对内--从内部的后端服务器复制数据
  一个对外--供用户访问
  一个管理
这里的实验进行了简化,只要可以代理后端服务器即可
centos7.4--代理服务器
  从一个接口代理请求,并从同一个接口代理到服务器上,生产中不会这么搞
centos7.3--被代理服务器
centos7.3
yum -y install httpd
mkdir /var/www/html/bbs
vim /var/www/html/index.html
<h1>Upstream Server Home Page</h1>  后端主机
vim /var/www/html/bbs/index.html
<h1>BBS Application</h1>
systemctl start httpd.service
ss -ntl  80端口
centos7.4
cat /etc/nginx/conf.d/vhost2.conf
server {
        server_name www.feng.com;
        listen 8080;
        root "/www/vhost2";

}
ss -ntl
systemctl start nginx
centos7.4-2--客户端
curl www.feng.com:8080  ok
centos7.4  启用代理功能
vim /etc/nginx/conf.d/vhost2.conf
server {
        server_name www.feng.com;
        listen 8080;
        root "/www/vhost2";
        location / {
                proxy_pass http://192.168.1.7;  IP外网
        }
}
    即便本地存在root,但是proxy_pass优先级更高
nginx -t ; nginx -s reload
centos7.4-2--客户端
curl www.feng.com:8080  ok
centos7.4  启用代理功能
vim /etc/nginx/conf.d/vhost2.conf
解析
  location / { proxy_pass http://192.168.1.7/ }  后面的斜线"/"--此处没有差别
    访问www.feng.com:8080-->映射为-->192.168.1.7
    访问www.feng.com:8080/bbs/-->映射为-->192.168.1.7/bbs/
  location / { proxy_pass http://192.168.1.7/bbs/; }
    访问www.feng.com:8080-->映射为-->192.168.1.7/bbs/
  location /bbs/ { proxy_pass http://192.168.1.7/bbs/; }
    访问www.feng.com:8080-->映射为-->centos7.4本机信息:Test Page Vhost2
    访问www.feng.com:8080/bbs/-->映射为-->192.168.1.7/bbs/
  location /bbs/ { proxy_pass http://192.168.1.7; }
    访问www.feng.com:8080-->映射为-->centos7.4本机信息:Test Page Vhost2
    访问www.feng.com:8080/bbs/-->映射为-->192.168.1.7/bbs/
  location /bbs/ { proxy_pass http://192.168.1.7/; }  "/"有无差别很大
    访问www.feng.com:8080-->映射为-->centos7.4本机信息:Test Page Vhost2
    访问www.feng.com:8080/bbs/-->映射为-->192.168.1.7
    访问www.feng.com:8080/bbs/bbs/-->映射为-->192.168.1.7/bbs/

"proxy_pass"不能有URI(/...)的情况:
    regular expression  正则表达式
    inside named location  
    inside "if" statement
    inside "limit_except" 
centos7.4  启用代理功能
vim /etc/nginx/conf.d/vhost2.conf
server {
        server_name www.feng.com;
        listen 8080;
        root "/www/vhost2";
        location /bbs/ {
                proxy_pass http://192.168.1.7;
        }
        location ~* \.(jpg|gif|png|jpeg|svg)$ {  (~*:不区分字符大小写)
                proxy_pass http://192.168.1.7;  此处不能有"/",因为是location是正则
        }
}
nginx -t ; nginx -s reload
centos7.3--被代理服务器
find /usr/share/ -iname "*.jpg"
find /usr/share/ -iname "*background*.jpg" -exec cp {} /var/www/html/ \;
ls /var/www/html/
centos7.4-2--客户端--图形界面
firefox www.feng.com:8080/background.jpg  ok -.-
centos7.4  启用代理功能
vim /etc/nginx/conf.d/vhost2.conf
添加一个location信息,
访问".php"结尾的资源,都转给192.168.1.8这台主机(动态)
访问".jpg"结尾的资源,都转给192.168.1.7这台主机(静态)  动静分离
server {
        server_name www.feng.com;
        listen 8080;
        root "/www/vhost2";
        location /bbs/ {
                proxy_pass http://192.168.1.7;
        }
        location ~* \.(jpg|gif|png|jpeg|svg)$ {
                proxy_pass http://192.168.1.7;
        }
        location ~* \.php$ {
                proxy_pass http://192.168.1.8;
        }
}
css,jss文件(动态)--纯代理服务器不会有--可能在动态服务器or静态服务器上
随着版本的提升/更新--css,jss文件--被替换掉--有可能在动态服务器上
css,jss文件(动态)--可能被缓存--数据流--当静态来用
网游--当前的画面(不能缓存)--血槽/边框(可缓存)

centos7.4-2
vim /etc/hosts
192.168.1.6 www.fgq.com www.feng.com
192.168.1.6-->centos7.4的IP

proxy_cache_path
    定义可用于proxy功能的缓存;Context:    http(只能在http中)
centos7.4
cp /etc/nginx/nginx.conf{,.bak}
vim /etc/nginx/nginx.conf
在http中定义
proxy_cache_path /data/nginx/cache levels=2:1:1 keys_zone=pgcache:10m max_size=2g;
  虽然定义了缓存,但是只有被调用了,才会被缓存下来,生效
mkdir -p /data/nginx/cache
chown -R nginx:nginx /data/nginx/cache
vim /etc/nginx/conf.d/vhost2.conf
在 location ~* \.(jpg|gif|png|jpeg|svg)$ 中添加如下信息:
        location ~* \.(jpg|gif|png|jpeg|svg)$ {
                proxy_pass http://192.168.1.5;(dhcp,所以IP变更了--centos7.3)
                proxy_cache pgcache;
                proxy_cache_key $request_uri;  无端口和协议(默认是端口:协议:uri)
                proxy_cache_valid 200 302 10m;
                proxy_cache_valid 301      1h;
                proxy_cache_valid any      1m;
                proxy_cache_use_stale error timeout invalid_header;
        }
nginx -t ; nginx -s reload
ll /data/nginx/cache  无内容
centos7.4-2--客户端--图形界面
firefox www.feng.com:8080/background.jpg  ok
ll /data/nginx/cache  有内容,如下图1

proxy_hide_header field;
centos7.3
yum -y install php
vim /var/www/html/info.php
<?php
        phpinfo();
?>
systemctl restart httpd
centos7.4
vim /etc/nginx/conf.d/vhost2.conf
把IP地址更改一下即可
        location ~* \.php$ {
                proxy_pass http://192.168.1.5;
        }
nginx -t ; nginx -s reload
centos7.4-2--客户端--图形界面
firefox www.feng.com:8080/info.php  ok
  F12--Network Monitor--X-Powered-By--隐藏数据包里的此信息"X-Powered-By"
centos7.4
vim /etc/nginx/conf.d/vhost2.conf
        location ~* \.php$ {
                proxy_pass http://192.168.1.5;
                proxy_hide_header X-Powered-By;
        }
nginx -t ; nginx -s reload

proxy_set_header field value;
centos7.3
tail /var/log/httpd/access_log  都是代理服务器的IP地址,无意义
centos7.4
vim /etc/nginx/conf.d/vhost2.conf
下面的信息放在server里(如下图2)
        proxy_set_header X-Real-IP $remote_addr; 客户端的IP
centos7.3
更改LogFormat
cp /etc/httpd/conf/httpd.conf{,.bak}
vim /etc/httpd/conf/httpd.conf
/LogFormat  
  第一行的LogFormat中,增加信息: %{X-Real-IP}i(如下图3)
  或者
  第一行的LogFormat中,%h--更改为--%{X-Real-IP}i(如下图4)
httpd -t
systemctl restart httpd
centos7.4-2--客户端--图形界面
firefox www.feng.com:8080/info.php
centos7.3
tail /var/log/httpd/access_log  
    图3--代理服务器的IP(最前面)+真实客户端IP(最后面)
    图4--真实客户端IP(最前面)--combined格式

图1


image.png

图2


image.png
图3
image.png

图4


image.png
nap(nginx+httpd/php_module)--久经沙场+mariadb-->namp
  nginx--http协议--http server(httpd+php;http协议)
  使用proxy模块即可实现
np(nginx+fpm/php)+mariadb-->nmp
  nginx--fastcgi协议--fpm server(9000端口;fastcgi协议)
  使用ngx_http_fastcgi_module模块实现
二者明显的不同:协议不同,后端服务器不同
使用哪种方式--看你自己了
访问mysql的是php程序,php解析器自己不会访问mysql
程序员写的代码调用了连接mysql的驱动,利用数据库的API来完成数据的存取

ngx_http_fastcgi_module模块
后端服务器--centos7.3-2
  (centos7.3之前安装了php,可能会冲突,因此使用centos7.3-2,其他不变)
代理服务器--centos7.4
客户端--centos7.4-2
centos7.3-2
yum -y install php-fpm
  较为繁重的负载下使用
vim /etc/php-fpm.d/www.conf
更改连接池
listen = 127.0.0.1 -->最小权限法则
  listen在一个内网,且能够被前端主机访问的地址上
  listen = 0.0.0.0:9000  此处实验,生产环境不能这么开放
  能开发给一个主机,不能开放给一个网段;
  但是太严格--稍微更改环境,就要更改配置
listen.allowed_clients = 127.0.0.1  
  授权-->允许哪些客户端主机访问
  ";"注释掉--允许所有网段访问,或者只开放给一个网段访问--192.168.0.0/16
进程管理方式--两种--动态(相当于prefork)和静态
以下是默认值,生产环境中,会经常进行调整
服务上线前要进行压测--线上系统进行压力测试
在80%的资源利用率下,能承载多少个,能启动多少个,进行调整即可
为了性能更好,建议使用静态的
如:测试后,可以同时响应200个连接
那么就定义:pm为static,max_children为200,随时请求就可以随时响应
动态的好处--平时没人用时,可以节约资源
动态的缺点--当并发数量处于尖峰时--需要临时创建进程,速度会慢一点点
此处实验,就使用默认值了,启用pm内部的状态页和ping
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
;pm.status_path = /status  启用pm内部的状态页,去掉";",并更改名称
  pm.status_path = /pmstatus
;ping.path = /ping  探测pm后端主机的存活性,去掉";",并更改名称
  ping.path = /pmping
......
systemctl start php-fpm.service 
ss -ntl  9000端口
建立php数据文件夹
mkdir -p /data/fpm/app1
vim /data/fpm/app1/info.php
<?php
        phpinfo();
?>
centos7.4
nginx反代用户请求
vim /etc/nginx/conf.d/vhost2.conf
更改 server 和 location ~* \.php$ 内容(如下图1)
        server_name www.feng.com;
        listen 8080;
        location / {
                root "/www/vhost2";
                index index.php index.html;
        }
        location ~* \.php$ {
                fastcgi_pass 192.168.1.8:9000;  centos7.3-2的IP
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /data/fpm/app1$fastcgi_script_name;
                include fastcgi_params;  默认去/etc/nginx/下去找
        }
cat /etc/nginx/fastcgi_params  需要传递至后端服务器的fastcgi的参数
nginx -t ; nginx -s reload
centos7.4-2
firefox www.feng.com:8080/info.php  ok
  "System"和"Server API"发生变化
然后再部署一个mariadb/mysql就可以完成一个nmp
centos7.4
通过/pm_status和/ping来获取fpm server状态信息
vim /etc/nginx/conf.d/vhost2.conf
添加如下信息(如下图2)
        location ~* ^/(pmstatus|pmping)$ {
                fastcgi_pass 192.168.1.8:9000;
                fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
                include fastcgi_params;
        }
nginx -t ; nginx -s reload
centos7.4-2
firefox www.feng.com:8080/pmstatus  ok
firefox www.feng.com:8080/pmstatus?full  ok,完整格式
  accepted conn  已经处理过的连接请求数
  idle processes  空闲进程数
  max children reached  达到上限的次数
  第一个是总的信息,下面5个是每个进程当前的信息
  生产环境中,会显示很多进程,可以写一个脚本,获取这些状态信息,导入到系统里来显示fpm的工作情形
firefox www.feng.com:8080/pmstatus?json  ok,json格式
firefox www.feng.com:8080/pmstatus?xml  ok,xml格式
firefox www.feng.com:8080/pmping  ok,pong(存活,健康的意思)

fastcgi_cache_...  fastcgi缓存类似proxy_cache_...参考文档,不做演示了
centos7.4
vim /etc/nginx/nginx.conf
在http中定义
fastcgi_cache_path /data/nginx/fcgicache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
chown -R nginx:nginx /data/nginx/fcgicache/
vim /etc/nginx/conf.d/vhost2.conf
更改 location ~* \.php$ 内容(如下图3)
        location ~* \.php$ {
                fastcgi_pass 192.168.1.8:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /data/fpm/app1$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_cache fcgi;
                fastcgi_cache_key $request_uri;
                fastcgi_cache_valid 200 302 10m;
                fastcgi_cache_valid 301      1h;
                fastcgi_cache_valid any      1m;
                fastcgi_cache_use_stale error timeout invalid_header;
        }
nginx -t ; nginx -s reload
ll /data/nginx/fcgicache/  没有内容
centos7.4-2
firefox www.feng.com:8080/info.php
centos7.4
ll /data/nginx/fcgicache/  有内容

fastcgi_keep_conn on | off
默认响应完请求后,就断开-->off
如果为on,响应完请求后不断开,一直连接-->可以加速访问
  优势:有请求过来,立即发送给后端服务器,不用再重新创建连接,节省时间

np不在同一台主机上--wordpress--前后端服务器都要有--此处不做
  如果要到了分开的程度,访问量极大,本身的应用程序就已经分割开了
np在同一台主机上--简单--此处使用
    无论动态/静态内容,都到同一个目录下找即可
单台主机实现nmp
centos7.4-2--nginx和fpm-php都在一个主机上
安装epel源--前面有
yum -y install nginx
yum -y install php-fpm
vim /etc/php-fpm.d/www.conf
  listen = 0.0.0.0:9000
  listen.allowed_clients = 127.0.0.1  ";"注释掉
  pm.status_path = /status  启用pm内部的状态页,去掉";"
  ping.path = /ping  探测pm后端主机的存活性,去掉";"
  其他都使用默认值
systemctl start php-fpm.service 
ss -ntl  9000端口
vim /etc/nginx/nginx.conf
在http的server中(如图4)
        root         /data/web/wordpress/;  
        location / {
                index index.php index.html;
        }
        location ~* \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /data/web/wordpress$fastcgi_script_name;
                include fastcgi_params;
        }
mkdir -p /data/web/wordpress
nginx -t ; nginx -s reload
systemctl start nginx.service
ss -ntl  80端口
安装mariadb和wordpress
...... 以前有,不做陈述了

将来使用nginx时,可能不在同一个主机上,程序很大
意味着:动静内容在设计上,程序提供的就是程序,
程序中对url的引用本来就应该是对另一个服务器上的图片服务器的引用,设计的时候就分割开来

图1

image.png
图2
image.png
图3
image.png
图4
image.png
图5 web架构
image.png
图6
image.png
图7
image.png
图8
image.png
nginx做代理服务器时,收到的请求量很大,且都能代理到后端服务器上
    请求量很大--需要多台后端服务器共同处理
    nginx调度时的session问题
nginx调度与lvs不同:
    首先nginx把后台的主机定义成组-->ngx_http_upstream_module
    其次nginx把请求代理到组上,组自身能够调度请求至各后台主机上
ngx_http_upstream_module (如上图6)
    将服务器分组  能够让我们指定调度算法  把nginx作为负载均衡器
      常用的两种hash算法
        静态数组法--默认 (如上图7)
        一致性hash算法--consistent (如上图8)
centos7.3-后端主机1
centos7.3-2-后端主机2
centos7.4-nginx负载均衡器
centos7.3-后端主机1
vim /var/www/html/index.html
<h1>Upstream Server 1</h1>
systemctl restart httpd
ss -ntl
centos7.3-2-后端主机2
yum -y install httpd
vim /var/www/html/index.html
<h1>Upstream Server 2</h1>
systemctl restart httpd
ss -ntl
centos7.4-nginx负载均衡器
curl 192.168.1.5--后台主机1的IP
curl 192.168.1.8--后台主机2的IP
centos7.4-nginx负载均衡器
vim /etc/nginx/conf.d/vhost3.conf
server {
        listen 8080;
        server_name www.csn.io;
        location / {
                root "/data/nginx/vhost3";
        }
}
mkdir -p /data/nginx/vhost3
vim /data/nginx/vhost3/index.html
<h1>Nginx Vhost3</h1>
nginx -t ; nginx -s reload
centos7.4-2--客户端
vim /etc/hosts
192.168.1.6 www.fgq.com www.feng.com www.csn.io
    192.168.1.6-centos7.4的IP
图形界面:firefox www.csn.io:8080  ok
centos7.4-nginx负载均衡器
vim /etc/nginx/nginx.conf
http中定义upstream
    upstream webservers {
        server 192.168.1.5:80 weight=1 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=1 max_conns=4000 max_fails=2 fail_timeout=3;
    }
nginx -t ; nginx -s reload
vim /etc/nginx/conf.d/vhost3.conf
server {
        listen 8080;
        server_name www.csn.io;
        location / {
                root "/data/nginx/vhost3";
                proxy_pass http://webservers;  指明定义的组名
        }
}
nginx -t ; nginx -s reload
centos7.4-2
for i in {1..10};do curl www.csn.io:8080;sleep 1;done  轮询(weight都是1)
centos7.4-nginx负载均衡器
vim /etc/nginx/nginx.conf
更改权重
    upstream webservers {
        server 192.168.1.5:80 weight=2 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=3 max_conns=4000 max_fails=2 fail_timeout=3;
    }
nginx -t ; nginx -s reload
centos7.4-2
for i in {1..10};do curl www.csn.io:8080;sleep 1;done  wrr
centos7.4-nginx负载均衡器
vim /etc/nginx/nginx.conf
添加信息"least_conn",算法就变为wlc,长连接时才会显示的明显(如:ssh)
    upstream webservers {
        server 192.168.1.5:80 weight=2 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=3 max_conns=4000 max_fails=2 fail_timeout=3;
        least_conn;  最好用在长连接中,短连接用wrr
    }
        "least_conn;"-->"ip_hash;"  把同一个请求始终绑定在同一服务器上
        "least_conn;"-->"hash $remote_addr;"  静态数组方式,效果与"ip_hash;"相同
            使用hash指令粘滞其他属性,自定义调度方式
        "least_conn;"-->"hash $remote_addr consistent;"  一致性hash算法
        "least_conn;"-->"hash $request_uri consistent;"  
            hash什么,什么就是"键",任何客户端只要请求的是同一个内容,就访问同一个服务器(根据uri来绑定)
            好处:当后端主机是缓存服务器时,可以提高命中率
nginx -t ; nginx -s reload
centos7.4-2
for i in {1..10};do curl www.csn.io:8080;sleep 1;done  wlc
for i in {1..10};do curl www.csn.io:8080;sleep 1;done  会话粘滞(一直是同一服务器)

centos7.3--后端主机1
for i in {1..10};do echo "Test Page $i @US1" > /var/www/html/test$i.html;done
centos7.3-2--后端主机2
for i in {1..10};do echo "Test Page $i @US2" > /var/www/html/test$i.html;done
centos7.4--nginx调度器
vim /etc/nginx/nginx.conf
先是wrr,
再去掉#变为"一致性hash算法"  (两次操作)
    upstream webservers {
        server 192.168.1.5:80 weight=2 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=3 max_conns=4000 max_fails=2 fail_timeout=3;
        #hash $request_uri consistent;
    }
nginx -t ; nginx -s reload
centos7.4-2和centos7.4-3(客户端)
for i in {1..10};do curl www.csn.io:8080/test1.html;sleep 0.3;done  wrr
for i in {1..10};do curl www.csn.io:8080/test1.html;sleep 0.3;done  一致性hash算法
for i in {1..10};do curl www.csn.io:8080/test10.html;sleep 0.3;done
一致性hash算法:二者访问的内容相同时,都会转到同一个服务器上

centos7.4--nginx调度器
vim /etc/nginx/nginx.conf
更改权重都为1(下面是两次操作)
rr算法
    upstream webservers {
        server 192.168.1.5:80 weight=1 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=1 max_conns=4000 max_fails=2 fail_timeout=3;
        #hash $request_uri consistent;
    }
在第二个server后面添加"backup"  
    upstream webservers {
        server 192.168.1.5:80 weight=1 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=1 max_conns=4000 max_fails=2 fail_timeout=3 backup;
        #hash $request_uri consistent;
    }
        第二个server是第一个server的"sorry server"(备份)
        只有当第一个server是down机时,第二个server才会上线,否则第2个server不起作用
        备用服务器有2个时,如果在线的所有的服务器都down机了,二者才会轮询
nginx -t ; nginx -s reload
centos7.4-2和centos7.4-3(客户端)
for i in {1..10};do curl www.csn.io:8080/test1.html;sleep 0.3;done  rr
for i in {1..10};do curl www.csn.io:8080/test1.html;sleep 0.3;done  
  无论访问"test$i"的哪个,都是后台主机1
  当systemctl stop httpd(centos7.3--down机),后台主机2才生效

centos7.4--nginx调度器
vim /etc/nginx/nginx.conf
在第二个server后面的"backup"-->"down"  
    upstream webservers {
        server 192.168.1.5:80 weight=1 max_conns=3000 max_fails=2 fail_timeout=3;
        server 192.168.1.8:80 weight=1 max_conns=4000 max_fails=2 fail_timeout=3 down;
        #hash $request_uri consistent;
    }
  明确说明第二个server是无用的
  当第一个server是down机时,第二个server也不起作用
  管理down--发布新版本时使用
nginx -t ; nginx -s reload
centos7.4-2和centos7.4-3(客户端)
for i in {1..10};do curl www.csn.io:8080/test1.html;sleep 0.3;done  
  无论访问"test$i"的哪个,都是后台主机1
  当systemctl stop httpd(centos7.3--down机),后台主机2不会生效
  显示信息:502 Bad Gateway

管理down--发布新版本时使用--分析
  蓝绿发布(一半一半):
    进行网站的版本更新时,一半服务器先定义为"down",用来更新,另一半供用户在线访问
    等这部分更新完成后,即刻上线,供用户访问,再定义另一半的服务器为"down",进行更新      
  灰度发布(一部分):
    访问量太大,只能定义一小部分为down,不能一半一半的down
更新后,上线--遇到bug,就要"回滚"--回滚方式:
  链接方式
      /data/apps/wordpress-->...  
        -->wordpress-4.1
        -->wordpress-4.2
        -->wordpress-4.3
      链接到新版本文件,多个版本并存(最近的3-5个版本),更新上线后遇到bug就更改链接至旧版本文件
  版本控制系统 git/cbs/...
    把每一个旧版本进行快照保存下来,同时新版本也会保存至"版本控制系统"中
    更新时,只需要从"版本控制系统"中提取新版本即可
    上线后,出现bug,把此服务器下线,从"版本控制系统"中提取旧版本恢复即可
运维工程师的3大核心任务
  1.发布--新版本(线上系统)
    开发人员:敏捷开发,持续集成--1周出一个版本
    访问量最小的时候进行
  2.变更--程序版本,主机的上线,替换,下线
  3.故障处理--救火
运维自动化
  发布:server--down--reload--上线--100台主机--100次--no噩梦
  发布脚本--自动连接前端服务器--down--停止服务--发布新版本--启动服务--上线
    没有问题--继续执行100次发布脚本
    发布脚本--充分测试--单元/集成/功能测试--找bug
  应用程序
    每一个主机部署一个agent,执行本地操作,在远程控制台"点点点"
  python的web开发--web页面--"点点点"
    摘除--发布--上线--测试--没问题
  docker
    程序员解决:服务安装方式--一键直达--所有应用/配置/依赖关系/随时回滚
ngx_http_flv_module  流媒体/CDN(用的少,可以随时补充)


ngx_stream_core_module;ngx_stream_proxy_module
前面实验做基础
centos7.3--后端主机1
centos7.3-2--后端主机2
centos7.4--nginx--前端主机
centos7.4-2和centos7.4-3--客户端主机

centos7.3--后端主机1--MySQL服务
rpm -q mariadb-server
yum -y install mariadb-server
systemctl start mariadb.service
mysql
grant all on *.* to 'admin'@'192.168.%.%' identified by 'qianggedu' with grant option;
    with grant option  自己得到的授权权限可以给别人
flush previlege;
quit
centos7.4-2和centos7.4-3(客户端主机)
yum -y install mariadb  安装MySQL的客户端
mysql -u admin -p -h 192.168.1.5  ok
不让用户直接访问192.168.1.5(centos7.3),而用nginx代理用户请求
centos7.4--nginx--前端主机
systemctl stop nginx.service
cp /etc/nginx/nginx.conf{,.http}  备份之前的配置文件
vim /etc/nginx/nginx.conf
光标移动到http上一行
:.,$d  删除光标所在行至最后行的内容--删除http的内容
添加信息
stream {
        server {
                listen 3306;
                proxy_pass 192.168.1.5:3306;
        }
}
nignx -t ; systemctl restart nginx.service ; ss -tnl  3306端口
centos7.4-2(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  ok
  nginx扮演一个四层反代的功能

把服务器定义成组--实现nginx的负载均衡--ngx_stream_upstream_module
  可以反代--就可以负载均衡
centos7.3-2--后端主机2--也安装MySQL服务
rpm -q mariadb-server
yum -y install mariadb-server
systemctl start mariadb.service
mysql
grant all on *.* to 'admin'@'192.168.%.%' identified by 'qianggedu' with grant option;
    with grant option  自己得到的授权权限可以给别人
flush previlege;
quit
centos7.4-2(客户端主机)
mysql -u admin -p -h 192.168.1.8(centos7.3-2的IP)  ok
centos7.4--nginx--前端主机
vim /etc/nginx/nginx.conf
在stream中使用upstream
stream {
        upstream mysrvs {
                server 192.168.1.5:3306;
                server 192.168.1.8:3306;
        }
        server {
                listen 3306;
                proxy_pass mysrvs;  没有协议,因为我们不是在http中,注意前后对应
        }
}
    权重默认1--加权轮询算法,backup功能(与前面类似)
nignx -t ; nginx -s reload
centos7.4-2和centos7.4-3(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  ok--wrr
centos7.4
ss -nt  查看访问的是哪个主机:State-ESTAB
centos7.3-2--后端主机2
systemctl stop mariadb.service
centos7.4-2和centos7.4-3(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  ok,请求都发送至后端主机1

ngx_stream_log_module
centos7.4--nginx--前端主机
vim /etc/nginx/nginx.conf
在stream中使用"log_format"  如图1
        log_format basic '$remote_addr $time_local'
                         '$protocol $status $bytes_sent $bytes_received'
                         '$session_time';
        access_log /var/log/nginx/stream-access.log basic;
nginx -t ; nginx -s reload
tail -f /var/log/nginx/stream-access.log
centos7.4-2和centos7.4-3(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  ok centos7.4出现日志信息

ngx_stream_access_module  访问控制列表
centos7.4--nginx--前端主机
vim /etc/nginx/nginx.conf
在stream的server中添加访问控制列表
        server {
                listen 3306;
                proxy_pass mysrvs;
                deny 192.168.1.11;  (centos7.4-3的IP)
                allow 192.168.0.0/16;
                deny all;
        }
nginx -t ; nginx -s reload
centos7.4-2(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  ok
centos7.4-3(客户端主机)
mysql -uadmin -p -h192.168.1.6(centos7.4的IP)  no
  显示信息:'reading initial communication packet'

ngx_http_geo_module ; ngx_http_geoip_module(epel源)
  根据客户端地理位置的不同,返回不同的server配置
  调度用户请求至距离用户最近的服务器
centos7.4--nginx
yum list all |grep -i geoip
yum info GeoIP  -->  C library(默认已经安装)
yum info GeoIP-data  -->  database √(未安装)--GeoIP1
官网:www.maxmind.com--免费和商业版
yum -y install GeoIP GeoIP-data
rpm -ql GeoIP
rpm -ql GeoIP-data
/usr/share/GeoIP/GeoIPCity-initial.dat  注意要与nginx配置文件对应
GeoIP1(epel)与GeoIP2(官网)  不兼容,API不一样,看程序支持哪个版本,进行下载
此处不做实验--IP都是内网的,后期在其他系统演示GeoIP的配置
  搜索引擎--基于地址分类后,世界地图展示"所模拟的全球各地IP"


图1


image.png
nginx编译安装
参考文档
使用已经编译好的nginx--都是经过充分测试的--特殊需求除外

相关文章

网友评论

    本文标题:nginx

    本文链接:https://www.haomeiwen.com/subject/gecrhftx.html