Nginx的安装、部署和配置管理

HTTP
HTTP协议是超文本传输协议,从服务器传输超文本到本地浏览器的协议。

HTTPS 协议:可以理解为HTTP+SSL/TLS, 即 HTTP 下加入 SSL 层,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL,用于安全的 HTTP 数据传输。

SSL协议分为两层:SSL握手协议(相当于连接)、SSL记录协议(相当于通信)

工作原理:
浏览器作为HTTP客户端通过url向HTTP服务端(web服务器)发送所有请求。
常见的web服务器:nginx、apache、IIS等。
端口号默认为80
HTTP使用统一的资源标识符(URI)来传输数据和建立连接。

HTTP1.0和HTTP1.1的主要区别如下:
1、缓存处理:1.1添加更多的缓存控制策略(如:Entity tag,If-Match)
2、网络连接的优化:1.1支持断点续传
3、错误状态码的增多:1.1新增了24个错误状态响应码,丰富的错误码更加明确各个状态
4、Host头处理:支持Host头域,不在以IP为请求方标志
5、长连接:减少了建立和关闭连接的消耗和延迟。

HTTP1.1和HTTP2.0的主要区别:
1、新的传输格式:2.0使用二进制格式,1.0依然使用基于文本格式
2、多路复用:连接共享,不同的request可以使用同一个连接传输(最后根据每个request上的id号组合成正常的请求)
3、header压缩:由于1.X中header带有大量的信息,并且得重复传输,2.0使用encoder来减少需要传输的hearder大小
4、服务端推送:同google的SPDUY(1.0的一种升级)一样

URL=URI+URN
URL:统一资源定位符,一种定位资源的主要访问机制的字符串。一个标准的URL包括:protocol、host、port、path、parameter、anchor。
URI:统一资源标识符,用来标识抽象或者物理资源的一个紧凑字符。
URN:统一资源名称,通过特定的命名空间中唯一名称或ID来标识资源。

客户端请求消息
请求行、请求头部、空行、请求数据

# curl -v http://www.testpm.cn
Connected to www.testpm.cn (47.244.247.240) port 80 (#0)
> GET /hello.txt HTTP/1.1   # 请求方式与版本协议。
> User-Agent: curl/7.29.0   #用什么客户端访问
> Host: www.testpm.cn  #主机名,域名。主机和端口号,
> Accept: */*  #匹配什么文件类型,“*” 是通用匹配。匹配所有类型

服务器响应消息
状态行、消息报头、空行、响应报文

< HTTP/1.1 200 OK       #请求返回的状态码
< Server: nginx/1.16.0  #请求的服务和版本号
< Date: Thu, 04 Jul 2019 08:19:40 GMT
< Content-Type: text/plain #文本类型,有html,plain:普通文本
< Content-Length: 12
< Last-Modified: Thu, 04 Jul 2019 08:13:25 GMT
< Connection: keep-alive  #是否支持长连接
< ETag: "5d1db525-c"  #标识,每次访问如果与最开始的一样返回304否则校验不一致返回200
< Accept-Ranges: bytes

HTTP请求方法
HTTP1.0:GET、POST、HEAD
HTTP1.1:OPTIONS、PUT、DELETE、TRACE、CONNECT

HTTP状态码
在这里插入图片描述

200:请求成功(一般用于get、post请求)
301:永久重定向
302:临时重定向
305:使用代理。所请求的资源必须通过代理访问
306:废弃的HTTP状态码
400:客户端请求语法错误
401:请求要求用户身份验证
403:权限被拒
404:资源未找到
405:客户端请求方法被终止
500:服务器内部错误
501:服务器不支持请求功能
502:无效相应
503:暂时无法处理客户端的请求
505:不支持请求的HTTP协议版本

NGINX服务
Nginx(engine x) 是一个轻量级,高性能的 HTTP 和 反向代理 服务,也是一个IMAP/POP3/SMTP服务。因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
nginx是一个高性能的web和反向代理服务器,具有很多优越的特性,单机环境下参考服务器配置,并发连接在7000±–8000左右,集群模式20000+。
作为web服务器:与apache相比,nginx占用更少的资源,支持更多并发连接,体现更高的效率。
作为负载均衡器:nginx既可以在内部支持 Rails 和 PHP,也可支持作为HTTP代理服务器对外进行服务。
作为邮件服务器:nginx是一个非常优秀的邮件代理服务器(最早开发这个产品目的也是作为邮件代理服务器)。

NGINX优点
高并发、内存消耗少、模块化程度高、低成本、支持多系统、支持Rwrite重写规则(根据域名、URL的不同,,将HTTP请求分发到不同的服务器群组中)
缺点
动态处理差、rewrite弱(虽然支持Rwrite,但是apache比nginx的 rewrite要强)。

I/O多路复用
I/O multiplexing
1)多进程并发(每进来一个新的I/O流会分配一个新的进程管理)
2)I/O多路复用:通过记录跟踪每个I/O流(sock)的状态,来同时管理多个I/O流,发明其的原因是提高服务器的吞吐能力,在同一个线程里面,通过拨开关的方式,来同时传输多个I/O流。
epoll:epoll监控所有进来的连接,谁有数据就调用相应的代码处理,不仅会告诉你sock里面的数据,还会告诉你具体哪个sock有数据。
3)异步,非阻塞:一个master,两个work进程(银行柜台管理员的工作模式)

NGINX官方网站:http://www.nginx.org
NGINX yum安装
安装工具组:

yum -y install yum-utils

配置nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

安装nginx

yum -y install nginx

查看版本号

nginx -v

查看依赖关系

nginx -V

关闭防火墙和selinux,并启动服务

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
systemctl stop firewalld 
systemctl disable firewalld
systemctl start nginx

测试访问页面
在这里插入图片描述
NGINX源码安装

安装环境

yum -y install gcc gcc-c++    #安装编译环境
yum install -y pcre pcre-devel gd-devel     #安装pcre包
yum install -y openssl openssl-devel        #安装openssl-devel
yum install -y zlib zlib-devel        #安装zlib

创建nginx用户

useradd   nginx

安装nginx

[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@localhost ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@localhost nginx-1.16.0]# make && make install

修改配置文件
nginx.conf由三部分组成:全局块、events块、http块(http块中包含http全局块,多个server块,server块中包含server全局块和location块)

# 全局参数设置
user nginx;  #设置nginx使用的用户
worker_processes  auto;          #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 
error_log  logs/error.log;    #指定错误日志 

pid        /var/run/nginx.pid; 
events { 
    worker_connections  1024; #设置一个进程的最大并发连接数 
}
# http 服务相关设置 
http { 
    include      mime.types; 
    default_type  application/octet-stream; 
    log_format  main  'remote_addr - remote_user [time_local] "request" '
                      'status body_bytes_sent "$http_referer" '
                      '"http_user_agent" "http_x_forwarded_for"'; 
    access_log  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on; 
    #gzip              on;      #是否开启gzip压缩,将注释去掉开启 
    keepalive_timeout  65;     #设置长连接的超时时间
# 虚拟服务器的相关设置 
    server { 
        listen      80;        #设置监听的端口 
        server_name  localhost;        #设置绑定的主机名、域名或ip地址 
        #charset koi8-r;        # 设置编码字符 
        location / { 
            root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;    #设置默认打开的文档 
            } 
        error_page  500 502 503 504  /50x.html; #设置错误信息返回页面 
        location = /50x.html { 
            root  html;        #这里的绝对位置是/usr/local/nginx/html
        } 
    } 
 }

检查配置文件,启动服务

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx

测试访问页面
在这里插入图片描述nginx日志文件详解

log_format  main  'remote_addr - remote_user [time_local] "request" '
                  'status body_bytes_sent "$http_referer" '
                  '"http_user_agent" "http_x_forwarded_for"';



remote_addr   #客户端IP
remote_user   #客户端名称
time_local    #访问时本地的时间
request       #请求的URL和http协议
status        #访问的状态码
body_bytes_sent		#发送给客户端的主题内容大小
http_referer	#记录客户端从哪个页面链接访问过来的若没有链接则为“-”
http_user_agent     #客户端使用浏览器的相关信息

limit_rate限制客户端传输数据的速度

location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate  2k;  #对每个连接的限速为2k/s
        }

虚拟主机
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,可以分三种:
1、基于域名的虚拟主机(server-name区分虚拟主机------主要应用于外部网站)
2、基于IP的虚拟主机(一个主机绑定多个IP地址)
3、基于端口的虚拟主机(端口区分虚拟主机-----应用:公司内部网站,外部网站的管理后台)

基于域名的虚拟主机

[root@localhost ~]# cat /etc/nginx/nginx.conf
worker_processes  4;
#error_log  logs/error.log;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    server {
        listen       80;
        server_name  web.testpm.com;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        	}
        }
    
    server {
        listen       80;
        server_name  web.1000phone.com;
        location / {
            root   /1000phone/html;
            index  index.html index.htm;
        	}
        }
}



创建index文件
[root@localhost ~]# mkdir -p /1000phone/html   #创建发布目录
[root@localhost ~]# echo "123"  >/1000phone/html/index.html

重新加载配置文件
[root@nginx]# /usr/local/nginx/sbin/nginx -s reload

测试并访问

基于IP的虚拟主机

[root@localhost ~]# ip a 
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.105.199  netmask 255.255.255.0  broadcast 10.0.105.255
        inet6 fe80::9d26:f3f0:db9c:c9be  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)
        RX packets 9844  bytes 1052722 (1.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5567  bytes 886269 (865.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.105.201  netmask 255.255.255.0  broadcast 10.0.105.255
        ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)



修改配置文件通过IP区分虚拟主机
[root@localhost ~]# vim  /etc/nginx/nginx.conf
user  root;
worker_processes  4;
#error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    server {
        listen       10.0.105.199:80;
        server_name  web.testpm.com;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        }
        
     server {
        listen       10.0.105.201:80;
        server_name  web.testpm.com;
        location / {
            root   /1000phone/html/;
            index  index.html index.htm;
        	}
        }
}


重新加载配置文件
测试并访问
删除绑定的vip
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 down
[root@localhost ~]# systemctl restart nginx

基于端口的虚拟主机

[root@localhost ~]# vim /etc/nginx/nginx.conf
user  root;
worker_processes  4;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
   log_format   #nginx日志格式
    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  web.testpm.com;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        }
           
     server {
        listen       8080;
        server_name  web.1000phone.com;
        location / {
            root   /1000phone/html/;
            index  index.html index.htm;
        	}
        }
}


重新加载配置文件
测试并访问

nginx Proxy代理(模块ngx_http_proxy_module)
反向代理实现:
需要一个负载均衡设备(反向代理服务器),分发用户的请求,将请求分发到后端提供服务的服务器上,服务器返回自己的服务到负载均衡上,负载均衡返回到用户;反向代理的过程隐藏了真实的服务器,使客户不知道提供服务的人是谁。

正向代理和反向代理的区别:
正向代理的对象是客户端,反向代理的对象是服务端

proxy详解

proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址
proxy_set_header:重新定义或者添加发往后端服务器的请求头
proxy_set_header X-Real-IP $remote_addr;#只记录连接服务器的上一个ip地址信息。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #通过这个选项可以记录真正客户端机器的ip地址





修改配置文件
[root@nginx-server ~]# vim /etc/nginx/conf.d/default.conf
server {
    server {
    listen       80;
    server_name  localhost;

    location / {
    proxy_pass http://10.0.105.199:80;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }
}

重新加载配置文件

负载均衡算法(upstream)
轮询(默认)、IP_hash、url_hash、fair

加权轮询

upstream myweb { 
      server 172.17.14.2:8080 weight=1;
      server 172.17.14.3:8080 weight=2;
}
配置状态参数:max_fails          #允许请求失败次数,默认为1
			fail_timeout       #max_fails次失败后,暂停服务的时间单位秒。两者可以一起使用

ip_hash

upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;
      ip_hash;
    }

测试

**nginx动静分离:**为了加快网站的解析速度,动态、静态由不同的服务器去解析,降低单个服务器的压力。

1.配置编译安装的nginx为反向代理upstream;
upstream static {
        server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
        }
upstream php {
        server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
        }
     server {
        listen      80;
        server_name     localhost
        #动态资源加载
        location ~ \.(php|jsp)$ {
            proxy_pass http://phpserver;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        #静态资源加载
        location ~ \.(html|jpg|png|css|js)$ {
            proxy_pass http://static;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        }




静态资源配置---10.0.105.196
server {
        listen 80;
        server_name     localhost;

        location ~ \.(html|jpg|png|js|css)$ {
        root /var/www/nginx;
        }
}


安装php7.1
[root@nginx-server ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@nginx-server ~]#yum install -y php71w-fpm
[root@nginx-server ~]#systemctl start php-fpm
[root@nginx-server ~]#systemctl enable php-fpm
#修改配置文件
[root@nginx-server ~]# cd /etc/nginx/conf.d/
[root@nginx-server conf.d]# vim phpserver.conf 
server {
        listen      80;
        server_name     localhost;
        location ~ \.php$ {
            root           /home/nginx/html;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #指定访问地址
            fastcgi_index  index.php;		#指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
        		}
        }
        

location指令
查找顺序和优先级

首先是=,其次是^~,其次是正则匹配最后是/


location前缀含义
=    表示精确匹配,优先级也是最高的 
^~   表示uri以某个常规字符串开头,理解为匹配url路径即可 
~    表示区分大小写的正则匹配  
~*   表示不区分大小写的正则匹配
!~   表示区分大小写不匹配的正则
!~*  表示不区分大小写不匹配的正则
/    通用匹配,任何请求都会匹配到
@    内部服务跳转

地址重写rewrite

**语法:应用环境**
server   location


if (condition) {}
if 可以支持如下条件判断匹配符号
~ 					正则匹配 (区分大小写)
~* 				    正则匹配 (不区分大小写)
!~                  正则不匹配 (区分大小写)
!~*		            正则不匹配  (不区分大小写)
-f 和!-f 		    用来判断是否存在文件
-d 和!-d 		    用来判断是否存在目录
-x 和!-x 		    用来判断文件是否可执行

在匹配过程中可以引用一些Nginx的全局变量
$args				请求中的参数;
$document_root	    针对当前请求的根路径设置值;
$host				请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$remote_addr		客户端地址;
$remote_port		客户端端口号;
$request_filename   当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images /a.jpg)
$request_uri		当前请求的文件路径名(不带网站的主目录/images/a.jpg)
$scheme				用的协议,比如http或者是https
$server_name		请求到达的服务器名;




**小记**
set指令用来定义一个变量,并且赋值
return指令用来返回状态码给客户端

rewrite flag标记

last 			    #相当于Apache里的[L]标记,表示完成rewrite。默认为last。
break 				#本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 			#返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 		    #返回301永久重定向,浏览器地址会显示跳转后URL地址


注意:
last 标记在本条 rewrite 规则执行完后,会对其所在的 server {} 标签重新发起请求;
break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;
使用 proxy_pass 指令时,则必须使用break

日志的级别

 debug级别:低级别,包含的信息非常详细
 info级别:稍微的高一点了。用的多一些。
 notice 和warning
 notice:相当于提示
 warning:警告 和warn 一样
 err和error 一样。
 crit:比较严重了
 alert:告警,很严重
 emerg: 恐慌级别, 级别最高的

日志轮转

[root@192 ~]# rpm -ql nginx |grep log
/etc/logrotate.d/nginx
/var/log/nginx
[root@192 ~]# vim /etc/logrotate.d/nginx
/var/log/nginx/*.log {           #指定需要轮转处理的日志文件
        daily     #日志文件轮转周期,可用值为: daily/weekly/yearly
        missingok               # 忽略错误信息
        rotate 7               # 轮转次数,即最多存储7个归档日志,会删除最久的归档日志
        minsize 5M	       #限制条件,大于5M的日志文件才进行分割,否则不操作
        dateext             # 以当前日期作为命名格式
        compress         # 轮循结束后,已归档日志使用gzip进行压缩
        delaycompress    # 与compress共用,最近的一次归档不要压缩
        notifempty         # 日志文件为空,轮循不会继续执行
        create 640 nginx nginx     #新日志文件的权限
        sharedscripts     #有多个日志需要轮询时,只执行一次脚本
        postrotate    # 将日志文件转储后执行的命令。以endscript结尾,命令需要单独成行
                if [ -f /var/run/nginx.pid ]; then    #判断nginx的PID。# 默认logrotate会以root身份运行
                        kill -USR1 cat /var/run/nginx.pid
                fi
        endscript
}

执行命令:
[root@192 nginx]# /usr/sbin/logrotate -f /etc/logrotate.conf
创建计划任务:
[root@192 nginx]# crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.conf

加粗样式

NGINX平滑升级


#原理

(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。

为什么要对nginx平滑升级?
随着nginx越来越流行,并且nginx的优势也越来越明显,nginx的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,例如 stream 四层代理功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就是运维的工作了

NGINX信号简介
主进程支持的信号
QUIT 等待工作进程结束后再退出
KILL 强制终止进程
HUP 重新加载配置文件,使用新的配置启动工作进程,并逐渐关闭旧进程
USR1 重新打开日志文件
USR2 启动新的主进程,实现热升级
WINCH 逐步关闭进程

工作进程支持的信号
TERM,INT 立刻退出
QUIT 等待工作进程结束后再退出
USR1 重新打开日志文件

平滑升级实战

[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V

只需要make,不需要make  install
[root@nginx-server ~]# cd /usr/local/nginx-1.16.0/
[root@nginx-server nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream --with-http_image_filter_module
[root@nginx-server nginx-1.16.0]# make

备份二进制文件和 nginx 的配置文件(期间nginx不会停止服务)
[root@nginx-server nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak


复制新的nginx二进制文件,进入新的nginx源码包
[root@nginx-server nginx-1.16.0]# cp /usr/local/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/

测试新版本是否正常
[root@nginx-server nginx-1.16.0]# /usr/local/nginx/sbin/nginx -t

给nginx发送平滑迁移信号(若不清楚pid路径,请查看nginx配置文件)
[root@nginx-server ~]# kill -USR2 `cat /var/run/nginx.pid`

查看nginx   pid
[root@nginx-server ~]# ll /var/run/nginx.pid*
-rw-r--r-- 1 root root 5 Jul  1 11:29 /var/run/nginx.pid
-rw-r--r-- 1 root root 5 Jul  1 09:54 /var/run/nginx.pid.oldbin

关闭旧的进程
[root@nginx-server ~]# kill -WINCH `cat /var/run/nginx.pid.oldbin`

此时不重载配置启动旧的工作进程
[root@nginx-server ~]# kill -HUP `cat /var/run/nginx.pid.oldbin`

结束工作进程,完成此次升级
[root@nginx-server ~]# kill -QUIT `cat /var/run/nginx.pid.oldbin`

验证Nginx是否升级成功
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V

nginx流量控制
流量控制(rate_limiting):我们可以用来限制用户在给特定时间内HTTP请求数量。更常见的是该功能被用来保护上有应用服务器不被同时太多用户请求压垮

流量限制的指令:limit_req_zone limit_req
发送到客户端的错误代码:limit_req_status(用来设置其他的状态码)

nginx的访问控制
1、基于IP访问控制:http_access_modle
配置语法

allow   允许     //ip或者网段
deny    拒绝     //ip或者网段


注意:
按顺序匹配,已经匹配的IP或者网段,后面将不再匹配
如果先允许所有IP访问,再定义拒绝访问,拒绝访问将不生效
默认为allow  all

2、基于用户信任登录: http_auth_auth_basic_modle
配置实例

[root@192 ~]# vim /etc/nginx/conf.d/auth_mod.conf 
server {
	listen 80;
	server_name localhost;
	location ~ /admin {
		root /var/www/html;
		index index.html index.hml;
		auth_basic "Auth access test!";   #开启登录验证
		auth_basic_user_file /etc/nginx/auth_conf;  #指定账号密码文件
		}
}

[root@192 ~]# nginx -t
[root@192 ~]# nginx -s reload
[root@192 ~]# mkdir /var/www/html    #创建目录
[root@192 ~]# vim /var/www/html/index.html    #创建文件

auth_basic不为off,开启登录验证功能,auth_basic_user_file加载账号密码文件。


建立口令文件
[root@192 ~]# yum install -y httpd-tools #htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件
[root@192 ~]# htpasswd -cm /etc/nginx/auth_conf user10 # -c 创建解密文件,-m MD5加密
[root@192 ~]# htpasswd -m /etc/nginx/auth_conf user20
[root@192 ~]# cat /etc/nginx/auth_conf 
user10:$apr1$MOa9UVqF$RlYRMk7eprViEpNtDV0n40
user20:$apr1$biHJhW03$xboNUJgHME6yDd17gkQNb0

测试并访问

在这里插入图片描述

NGINX监控
进程监控和端口监控
监控的基本活跃指标
Accepts(接受)、Handled(已处理)、Requests(请求数)、Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)。


配置nginx状态监控
[root@localhost ~]# vim /etc/nginx/conf.d/status.conf 
server {
        listen 80;
        server_name localhost;
        location /nginx-status {
                stub_status     on;
                access_log      on;
                }
}

扩展
SSL协议提供的服务
1)认证用户和服务器,确保数据发送到正确的客户机和服务器
2)加密数据以防止数据中途被窃取
3)维护数据的完整性,确保数据在传输的过程中不被改变

HTTP 传输面临的风险有
窃听风险、篡改风险、冒充风险

SSL证书
ssl证书包括证书的发布机构CA,证书的有效日期、公钥、整数的所有者、签名(防伪标签)
证书的三大功能:加密、签名、身份验证
CA(Certificate Authority)证书颁发机构主要负责证书的颁发、管理以及归档和吊销。证书内包含了拥有证书者的姓名、地址、电子邮件帐号、公钥、证书有效期、发放证书的CA、CA的数字签名等信息。

HTTP转HTTPS

80 ======> 443 :80转443端口
server {
    listen       80;
    server_name  www.testpm.cn;
    access_log  /var/log/nginx/http_access.log  main;
    return 301 https://www.testpm.cn;
}

server {
    listen 443 ssl;
    server_name www.testpm.cn;
    access_log  /var/log/nginx/https_access.log  main;

    
    ssl_certificate   /etc/nginx/cert/2447549_www.testpm.cn.pem;
    ssl_certificate_key  /etc/nginx/cert/2447549_www.testpm.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
    ssl_prefer_server_ciphers on;

    location / {
        root  /usr/share/nginx/html;
        index index.html index.htm;
    }
}

[root@nginx-server ~]# curl -I http://www.testpm.cn
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.0
Date: Wed, 03 Jul 2019 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.testpm.cn/

NGINX的性能优化
1、当前系统的结构瓶颈
2、业务模式
3、系统与nginx性能优化
1)网络 (网络流量、是否有丢包,网络的稳定性都会影响用户请求)
2)系统 (系统负载、内存使用率、硬件磁盘是否有损坏)
3)服务 (连接优化、http服务请求优化都可以在nginx中根据业务来进行设置)
4)程序 (接口性能)

发布了7 篇原创文章 · 获赞 2 · 访问量 134

猜你喜欢

转载自blog.csdn.net/weixin_45641605/article/details/104439202
今日推荐