Nginx

1、平滑升级Nginx

Kill命令传输信号给Nginx的主进程,下面这几个信息命令都是发送给nginx的master主进程的pid
TERM, INT(快速退出,当前的请求不执行完成就退出) -s stop
QUIT (优雅退出,执行完当前的请求后退出)  -s quit
HUP (重新加载配置文件,用新的配置文件启动新worker进程,并优雅的关闭旧的worker进程) -s reload
USR1 (重新打开日志文件)  -s reopen
USR2 (平滑的升级nginx二进制文件  拉起一个新的主进程master  旧主进程不停止)
WINCH (优雅的关闭worker子进程)

Kill 选项参数  pid      nginx的master主进程的pid
kill -INT pid          快速关闭
kill -QUIT pid         优雅关闭
重新安装Nginx
1停止掉服务,删除编译的安装的软件包和源码包
2重新解压编译安装即可,有需要就备份配置文件和网站目录里的资源文件

平滑升级Nginx
升级软件版本之后,需要启动新的版本,启动不了,端口已经被旧版本占用。
如果直接把旧版本的服务停止掉,会影响线上业务的使用。
最佳解决办法:
1旧的不先停掉
2新的又可以起来
3旧的和新的同时提供服务,旧的请求完成之后,就停掉旧进程
kill -USR2 旧的主进程号   平滑启动一个新主进程(平滑升级)
kill -WINCH 旧的主进程号  优雅关闭旧子进程
kill -QUIT 旧的主进程号   优雅关闭旧主进程
也可以直接 kill -USR2 旧的主进程号 再kill -QUIT 旧的主进程号
tar xvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
[root@linux nginx-1.16.0]# /usr/local/nginx/sbin/nginx -V     查看版本和配置选项并退出
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
make && make install
升级新版本,需要把软件的安装路径,指定到旧版本上。
以上操作完成之后,会把原来的旧版本备份为nginx.old(/usr/local/nginx/sbin/nginx.old)
[root@linux sbin]# ls
nginx  nginx.old
[root@linux sbin]# ./nginx    新版本起不来,旧版本占用了80端口
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
[root@linux sbin]# ./nginx -v        查看新版本
nginx version: nginx/1.16.0
[root@linux sbin]# ./nginx.old -v    查看旧版本
nginx version: nginx/1.14.2
[root@linux sbin]# ps -aux |grep nginx
root      37209  0.0  0.0  45956  1148 ?        Ss   22:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       37210  0.0  0.0  46412  1924 ?        S    22:38   0:00 nginx: worker process
root      39974  0.0  0.0 112708   972 pts/0    S+   22:59   0:00 grep --color=auto nginx
[root@linux sbin]# cat /usr/local/nginx/logs/nginx.pid
37209
新旧版本同时运行,kill -USR2 主进程号
[root@linux sbin]# kill -USR2 37209
[root@linux sbin]# ps -aux |grep nginx
root      37209  0.0  0.0  45956  1332 ?        Ss   22:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       37210  0.0  0.0  46412  1924 ?        S    22:38   0:00 nginx: worker process
root      40001  0.0  0.0  45980  3388 ?        S    23:01   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       40002  0.0  0.0  46444  1924 ?        S    23:01   0:00 nginx: worker process
root      40004  0.0  0.0 112708   976 pts/0    S+   23:01   0:00 grep --color=auto nginx
旧的和新的同时提供服务,旧的请求完成之后,就停掉旧进程
kill -USR2 旧的主进程号   平滑启动一个新主进程(平滑升级)
kill -WINCH 旧的主进程号  优雅关闭旧子进程
kill -QUIT 旧的主进程号   优雅关闭旧主进程
也可以直接 kill -USR2 旧的主进程号 再kill -QUIT 旧的主进程号
在nginx中,默认提供了平滑升级的操作,只需要执行以下命令
在nginx源码包先执行./configure
make install && make upgrade
原理是执行这个文件/root/soft/nginx-1.16.0/Makefile里面upgrade内容包含kill命令

Nginx配置文件/usr/local/nginx/nginx.conf

#nginx子进程启动用户
#user  nobody;
#子进程数量  一般调整为cpu核数或者倍数
worker_processes  1;
#错误日志定义
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid 存储文件
#pid        logs/nginx.pid;

#事件
events {
    #每个子进程的连接数      nginx当前并发量  worker_processes * worker_connections
    worker_connections  1024;
}

#http协议段
http {
    #引入  文件扩展名和与文件类型映射表
    include       mime.types;
    #默认文件类型   
    default_type  application/octet-stream;
    #访问日志access.log的格式
    #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  logs/access.log  main;
    #linux内核  提供文件读写的机制
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #长连接超时时间  单位为s
    keepalive_timeout  65;
    #gzip压缩
    #gzip  on;
    #server虚拟主机的配置
    server {
        #监听端口
        listen       80;
        #域名  可以有多个 用空格分隔
        server_name  localhost;
        #默认编码
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #location 用来匹配url
        location / {
            #默认访问的网站路径
            root   html;
            #默认访问页面 从前往后的顺序查找
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

server虚拟主机配置
在实际生产业务环境中,一台web服务器,需要使用多个网站部署。搭建server虚拟主机实现不同域名,解析绑定到不同的目录。
一般server虚拟主机配置有三类:
①基于域名,将域名配置到server_name上
②基于IP,将IP配置到server_name上
③基于端口,将端口配置到listen

#基于http的web服务
server{
    #监听端口
    listen 80
    #配置虚拟机
    server_name www.mpp123.com
    root html/mpp;
    location / {
        index index.php index.html index.htm
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

基于域名的虚拟主机

1、建立网站访问目录
cd /usr/local/nginx/html
mkdir mpp
cd mpp
创建测试文件
echo "www.mpp123.com" >> index.html
echo "php" >> index.php
2、解析域名并绑定
当前客户端是通过windows的浏览器,需要在win下的hosts文件(C:\Windows\System32\drivers\etc\hosts)进行解析域名
3、nginx配置文件绑定域名
server {
      #监听端口
      listen 80;
      #绑定域名
      server_name www.mpp123.com;
      #网站目录
      root html/mpp;
      #默认访问页面
      index index.html;
      #这段是解析php文件的
      location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
 }

基于端口的虚拟主机,修改listen配置

server {
      #监听端口
      listen 800;
      #绑定域名
      server_name www.mpp123.com;
      #网站目录
      root html/mpp;
      #默认访问页面
      index index.html;
      #这段是解析php文件的
      location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
 }

基于IP的虚拟主机

1、添加IP
ifconfig ens33:1 10.1.1.20     临时绑定IP
ip a    查看IP是否绑定成功
2、nginx配置文件添加
server {
    listen 80;
    server_name 10.1.1.20;
    root html/ip;
}
3、建立一个IP测试目录
cd /usr/local/nginx/html
mkdir ip
cd ip
echo "ip" >> index.html

gzip压缩,压缩文件,使文件变小,传输更快了。目前市场上大部分浏览器是支持GZIP的。IE6以下支持不好,会出现乱码情况。官方文档:http://nginx.org/en/docs/http/ngx_http_gzip_module.html

#配置到http段里,使整个http服务都启用gzip压缩
#开启gzip压缩
gzip on;
#http协议版本
gzip_http_version 1.0;
#IE浏览器不开启gzip  IE6以下会乱码
gzip_disable 'MSIE [1-6].';
#开启gzip 文件的格式
gzip_types image/jpeg image/jpg image/png text/plain text/css;

验证文件是否开启gzip
在这里插入图片描述
客户端缓存,B/S架构里 browser浏览器 就是客户端,告知浏览器获取的信息是在某个区间时间段是有效的。官方文档:http://nginx.org/en/docs/http/ngx_http_headers_module.html#expires

# 配置到server段里面
location ~ \.(js|css)$ {
    #单位参数 d day 天|h hour 小时
    expires 1h;
}

#在整个http中生效  配置到http段里
expires 1h;

在这里插入图片描述
在这里插入图片描述
基于IP的访问控制,基于ngx_http_access_module模块,默认可使用。官方文档:http://nginx.org/en/docs/http/ngx_http_access_module.html

写在server段里,作用范围是某个应用,写在http段里,作用范围是整个服务
deny ip   禁止ip访问
allow ip  允许ip访问

基于用户的访问控制,基于ngx_http_auth_basic_module模块,默认可用。官方文档:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

auth_basic "提示信息";
auth_basic_user_file  /etc/nginx/htpasswd;
1、创建用户名和密码存储文件
cd /usr/local/nginx/conf
#htpasswd 如果不存在就通过  yum -y install httpd-tools安装
#生成用户名mpp的密码文件passwd.db
htpasswd -c ./passwd.db mpp
#输入密码并再次确认密码
#查看passwd.db文件是否创建成功 cat passwd.db
2、在配置文件中进行配置,vim /usr/local/nginx/conf/nginx.conf
#根据业务需求,配置到server段里
#登录框显示的标题提示
auth_basic "test login";
#加载用户名称和密码校验文件
auth_basic_user_file  /usr/local/nginx/conf/passwd.db; 

目录列表显示,显示文件列表,或者需要做一个下载列表。官方文档:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html#autoindex

#开启目录列表显示
#根据业务需求,配置到server段里
autoindex on;
#index  当index默认找不到时,才会使用目录列表
index index;

注意:如果目录中没有配置的默认index访问项,而autoindex又没有开启,不能够查看访问目录列表,就会报出403错误。
正向代理:用户知道自己使用了代理,需要填写代理服务器的IP等相关连接信息,常见于代理客户端浏览器上网等操作。
反向代理:用户是无感知的,不知道使用了代理服务器。反向代理服务器是和真实访问的服务器是在一起的,有关联的。作用:可以根据实际业务需求,分发代理页面到不同的解释器。可以隐藏真实服务器的路径,常见于代理后端服务器。官方文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html
配置反向代理LNMPA

1、安装httpd,需改端口8080
#安装apache
yum install -y httpd
#修改apache的配置文件
vim /etc/httpd/conf/httpd.conf
修改配置项
listen 8080
2、配置nginx的server并进行转发
location / {
    proxy_pass http://127.0.0.1:8080;
    # proxy_pass https://www.baidu.com;代理到百度测试
}

日志管理
日志类型:
①access.log 访问日志 查看统计用户的访问信息 流量
②error.log 错误日志 错误信息 重写信息
访问日志,官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html

1、查看access.log
cd /usr/local/nginx/logs
cat access.log
access.log日志文件内容示例
10.1.1.1 - - [13/Feb/2024:12:32:19 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
2、查看配置解析参数说明
vim 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"';
#开启访问日志  访问日志的存储路径配置  调用的日志格式
#access_log  logs/access.log  main;

在这里插入图片描述
错误日志,记录一些启动和运行过程中的错误信息。官方文档:http://nginx.org/en/docs/ngx_core_module.html#error_log

# 定义开启错误日志  日志位置    日志级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
cat /usr/local/nginx/logs/error.log
格式示例:
2024/02/15 15:16:38 [error] 10349#0: *8 open() "/usr/local/nginx/html/ip/favicon.ico" failed (2: No such file or directory), client: 10.1.1.1, server: 10.1.1.20, request: "GET /favicon.ico HTTP/1.1", host: "10.1.1.10", referrer: "http://10.1.1.10/"

基于域名日志分割

1、开启日志的定义规则
#定义日志格式  定义http段里
log_format  mylogs  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
2、重启nginx测试查看
#在server段里面配置  也就是在当前server里的访问日志,会被写入定义的这里
#开启访问日志   访问日志的存储路径配置        调用的日志格式(上面配置的mylogs)
access_log  logs/www.mpp123.com_access.log  mylogs;

日志切割的方式有很多种:
①基于域名分开存储
②日志轮转,根据时间段分开存储
③自定义脚本,定时检测大小,根据文件大小进行切割

第三方模块使用

Nginx官方没有的功能,开源开发者定制开发一些功能,把代码公布出来,可以通过编译加载第三方模块的方式,使用新的功能。第三方模块网址:https://www.nginx.com/resources/wiki/modules
上传第三方模块压缩包,编译安装Nginx的第三方模块。

[root@linux soft]# tar -xvf ngx-fancyindex-0.4.3.tar.gz
[root@linux soft]# tar xvf echo-nginx-module-0.61.tar.gz
[root@linux soft]# cd /root/soft/nginx-1.16.0
重新编译带上之前的配置,查看之前编译的配置
[root@linux nginx-1.16.0]# /usr/local/nginx/sbin/nginx -V
求帮助,编译时添加安装第三方模块并带上之前编译的配置
[root@linux nginx-1.16.0]# ./configure --help |grep add
[root@linux nginx-1.16.0]# ./configure  --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --add-module=/root/soft/ngx-fancyindex-0.4.3/ --add-module=/root/soft/echo-nginx-module-0.61
编译安装并升级,[root@linux nginx-1.16.0]# make && make install && make upgrade

fancy-index模块有列表显示功能,https://www.nginx.com/resources/wiki/modules/fancy_index/

查看确认编译模块参数,确认是否包含ngx-fancyindex模块,/usr/local/nginx/sbin/nginx -V
#可以配置到http、server、location等下。推荐配置到server下
#开启fancyindex列表显示功能
fancyindex on;
#显示更为可读的文件大小
fancyindex_exact_size off;
nginx.conf中server部分示例配置:
server {
        listen       80;
        server_name  localhost;
        fancyindex on;
        fancyindex_exact_size off;
        root   html;
        index  aa;
首页(欢迎页)会加载html目录下的aa文件,没有找到aa文件才会显示/usr/local/nginx/html目录列表

在这里插入图片描述
echo模块常用来进行调试使用,比如输出打印Nginx默认系统变量

nginx.conf中server部分示例配置:
server {
        listen       80;
        server_name  localhost;
        root   html/mpp;
        index  aa;
    location / {
        #输出为文本类型
        default_type text/plain;
        #default_type text/html; 输出为html类型
        #打印输出查看变量信息,打印出server中定义的root变量,root html/mpp;
        echo $document_root;
            }

在这里插入图片描述

Nginx发行版本

Nginx社区免费版,https://nginx.org/ 。NGINX+商业版,https://www.nginx.com/
淘宝的tengine,http://tengine.taobao.org/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值