• Nginx网站服务


    Nginx网站服务

    一.概述及编译安装Nginx

    (一).Nginx优点

    - 一款高性能、轻量级Web服务软件
    - 稳定性高
    - 系统资源消耗低
    - 对HTTP并发连接的处理能力高
    - 单台物理服务器可支持30000~50000个并发请求
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (二)Ningx与Apache区别

    Nginx:
    1、轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源
    2、抗并发,nginx以epoll/kqueue作为开发模型,处理请求是异步非阻塞的,负载能力比
    apache高很多,而apache则是阻塞型的。在高并发下nginx能保持低资源低消耗高性能,
    而apache在PHP处理慢或者前端压力很大时,很容易出现进程数飙升,从而拒绝服务的现象。
    3、nginx 处理静态文件好,静态处理性能比 apache 高三倍以上
    4、nginx 的设计高度模块化,编写模块相对简单
    5、nginx 配置简洁,正则配置让很多事情变得简单,apache配置复杂(配置出错,会很崩溃)
    6、nginx 可作为负载均衡服务器
    7、nginx 本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器
    8、启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动,
    还能够不间断服务的情况下进行软件版本的升级
    9、社区活跃,各种高性能模块出品迅速
    
    Apache
    1、apache 的 rewrite 比 nginx 强大,在 rewrite 频繁的情况下,用 apache
    2、apache 发展到现在,模块超多,基本想到的都可以找到
    3、apache 更为成熟,少 bug ,nginx 的 bug 相对较多
    4、apache 超稳定
    5、apache 对 PHP 支持比较简单,nginx 需要配合其他后端用
    6、apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求要apache去做,
    nginx适合静态和反向。
    7、apache 仍然是目前的主流,拥有丰富的特性,成熟的技术和开发社区
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (三)装备安装包并编译安装

    1.关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
    2.安装依赖包 
    yum -y install pcre-devel zlib-devel gcc gcc-c++ make   //pcre:兼容一些正则表达式,zlib:解压缩包 
    3.创建运行用户 
    useradd -M -s /sbin/nologin nginx 
    4.编译安装Nginx 拖入安装包 
    cd /opt
    tar zxvf nginx-1.12.2.tar.gz -C /opt/  //解压软件包
    cd nginx-1.12.2/
    ./configure \
    --prefix=/usr/local/nginx \         //指定nginx的安装路径
    --user=nginx \                      //指定用户名
    --group=nginx \                     //指定组名
    --with-http_stub_status_module      //启用 http_stub_status_module 状态统计模块
    make -j3 && make install
    5.优化路径 
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    //让系统识别nginx的操作命令
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、Nginx的检查,启动,重启,停止服务

    (一)命令启动,停止,重启,重载

    nginx -t      //检查配置文件是否配置正确
    [root@zz nginx-1.12.2]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
    nginx        //启动
    
    
    ##——停止——##
    cat /usr/local/nginx/logs/nginx.pid       //先查看nginx的PID号
    [root@zz nginx-1.12.2]# cat /usr/local/nginx/logs/nginx.pid
    15858
    
    
    
    kill -3 
    kill -s QUIT 
    killall -3 nginx
    killall -s QUIT nginx
    ##——重载——##
    kill -1 
    kill -s HUP 
    killall -1 nginx
    killall -s HUP nginx
    ##——日志分隔,重新打开日志文件——##
    kill -USR1 
    ##——平滑升级——##
    kill -USR2 
    
    
    
    | 信号编号 | 信号名 | 含义                                                         |
    | -------- | ------ | ------------------------------------------------------------ |
    | 0        | EXIT   | 程序退出时收到该信息。                                       |
    | 1        | HUP    | 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。 |
    | 2        | INT    | 表示结束进程,但并不是强制性的,常用的 "Ctrl+C" 组合键发出就是一个 kill -2 的信号。 |
    | 3        | QUIT   | 退出。                                                       |
    | 9        | KILL   | 杀死进程,即强制结束进程。                                   |
    | 11       | SEGV   | 段错误。                                                     |
    | 15       | TERM   | 正常结束进程,是 kill 命令的默认信号                         |
    
    • 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
    • 37
    • 38
    • 39
    • 40

    查看nginx的进程号
    四种方式

    cat /usr/local/nginx/logs/nginx.pid 
    ss -ntpl | grep 80        //推荐使用,读取速度快
    netstat -natpl | grep 80
    netstat -natpl | grep nginx
    lsof -i :80
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    (二)添加Nginx系统服务

    1.方法一(编写脚本)启动速度没有方法二快
    vim /etc/init.d/nginx
    
    #!/bin/bash                 //解释器
    #chkconfig:35 99 20         //在运行界别3 5  开机第99个启动 关机20关闭
    #desc:this is nginx service control scprit
    COM = "/usr/local/nginx/sbin/nginx"
    PID = "/usr/local/nginx/logs/nginx.pid"
    case "$1" in
    start)
    $COM
    ;;
    
    stop)
    kill -s QUIT `cat $PID`
    ;;
    
    restart)
    $0 stop
    $0 start
    ;;
    
    reload)
    kill -s HUP `cat $PID`
    ;;
    
    *)
    echo "please use: $0 {start|stop|restart|reload}"
    exit 1      //返回码,执行失败返回码不是0
    
    esac
    exit 0
    
    ##——增加权限执行——##
    chmod +x /etc/init.d/nginx
    chkconfig --add nginx            //添加进系统服务
    systemctl stop nginx
    systemctl start nginx
    
    • 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
    • 37
    2.方法二
    vim /lib/systemd/system/nginx.service
    
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecrReload=/bin/kill -s HUP $MAINPID
    ExecrStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
    chmod 754 /lib/systemd/system/nginx.service
    systemctl start nginx.service
    systemctl enable nginx.service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    systemctl stop nginx和service nginx start不能一起使用,service会开启失败

    三.新版本升级

    cd /opt
    wget http://nginx.org/download/nginx-1.23.0.tar.gz
    tar zxvf nginx-1.23.0.tar.gz
    cd nginx-1.23.0
    yum install openssl openssl-devel -y
    ./configure \
    --prefix=/usr/local/nginx \		
    --user=nginx \					
    --group=nginx \					
    --with-http_stub_status_module \
    --with-http_ssl_module
    make
    千万不要执行make install,make install 直接重装了,环境就没了
    
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old   //备份
    cp objs/nginx /usr/local/nginx/sbin/nginx
    
    systemctl restart nginx   //重启服务
    nginx -V     //查看版本
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    四.认识Nginx服务的主配置文件 nginx.conf

    vim /usr/local/nginx/conf/nginx.conf 
    
    • 1

    1.全局配置

    #user nobody; 					#运行用户,若编译时未指定则默认为 nobody
    worker_processes 1; 			#工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
    #error_log logs/error.log; 		#错误日志文件的位置
    #pid logs/nginx.pid; 			#PID 文件的位置
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2.I/O 事件配置

    events {
        use epoll; 					#使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
        worker_connections 4096; 	#每个进程处理 4096 个连接
    }
    #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
    #在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
    
    #可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。
    /etc/security/limits.conf
    
    #epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。
    若工作进程数为 8,每个进程处理 4 096 个连接,则允许 Nginx 正常提供服务的连接数
    已超过 3 万个(4 096×8=32 768),当然具体还要看服务器硬件、网络带宽等物理条件的性
    能表现。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    3.HTTP 配置

    使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保
    持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包
    含在子界定标记“server { }”内
    
    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  logs/access.log  main;
    	##支持文件发送(下载)
        sendfile        on;
    	##此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
        #tcp_nopush     on;
    	##连接保持超时时间,单位是秒
        #keepalive_timeout  0;
        keepalive_timeout  65;
    	##gzip模块设置,设置是否开启gzip压缩输出
        #gzip  on;
    	(超过4k大小文件开启压缩模式优化)
    	##Web 服务的监听配置
    	server {
    		##监听地址及端口
    		listen 80; 
    		##站点域名,可以有多个,用空格隔开
    		server_name www.kgc.com;
    		##网页的默认字符集
    		charset utf-8;
    		##根目录配置
    		location / {
    			##网站根目录的位置/usr/local/nginx/html
    			root html;
    			##默认首页文件名
    			index index.html index.php;
    		}
    		##内部错误的反馈页面
    		error_page 500 502 503 504 /50x.html;
    		##错误页面配置
    		location = /50x.html {
    			root html;
    		}
    	}
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    相关字段解释

    日志格式设定:
    $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
    $remote_user:用来记录客户端用户名称;
    $time_local: 用来记录访问时间与时区;
    $request: 用来记录请求的url与http协议;
    $status: 用来记录请求状态;成功是200,
    $body_bytes_sent :记录发送给客户端文件主体内容大小;
    $http_referer:用来记录从哪个页面链接访问过来的;
    $http_user_agent:记录客户浏览器的相关信息;
    通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
    
    location常见配置指令,root、alias、proxy_pass
    root(根路径配置):root /var/www/html
    请求www.kgc.com/test/1.html,会返回文件/var/www/html/test/1.html
    
    alias(别名配置):alias /var/www/html
    请求www.kgc.com/test/1.html,会返回文件/var/www/html/1.html
    
    proxy_pass(反向代理配置)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    五.访问状态统计配置

    1.先使用命令,查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块

    nginx -V
    或者cat /opt/nginx-1.23.0/auto/options | grep YES
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    2.修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置

    cd /usr/local/nginx/conf
    cp nginx.conf nginx.conf_bak_20220719       //备份
    
    vim /usr/local/nginx/conf/nginx.conf
    ......
    http {
    ......
     server {
      listen 80;
      server_name www.zz.com;
      charset utf-8;
      location / {
       root html;
       index index.html index.php;
      }
      ##添加 stub_status 配置##
      location /status {      #访问位置为/status
       stub_status on;     #打开状态统计功能
       access_log off;     #关闭此位置的日志记录
      }
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    排错

    重启时发现获取不到 nginx

    在这里插入图片描述

    在这里插入图片描述

    解决:

    先看进程nginx占用

    在这里插入图片描述

    杀掉进程,出现另一个报错

    在这里插入图片描述

    要加载一下告诉system系统

    在这里插入图片描述

    3.验证结果

    浏览器访问
    在这里插入图片描述

    在这里插入图片描述

    六.基于授权的访问控制

    1.生成用户密码认证文件

    yum install -y httpd-tools
    htpasswd -c /usr/local/nginx/passwd.db zhangsan
    chown nginx /usr/local/nginx/passwd.db   //如果不更改权限不用更改属组
    chmod 400 /usr/local/nginx/passwd.db
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2.修改主配置文件相对应目录,添加认证配置项

    vim /usr/local/nginx/conf/nginx.conf
    ......
     server {
      location / {
       ......
       ##添加认证配置##
       auth_basic "secret";
       auth_basic_user_file /usr/local/nginx/passwd.db;
      }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.重启服务,访问测试

    nginx -t
    systemctl restart nginx
    
    • 1
    • 2

    在这里插入图片描述

    七.基于客户端的访问控制

    访问控制规则如下:
    
    - deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问
    - allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问
    - 规则从上往下执行,如匹配则停止,不再往下匹配
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.更改主配置文件

    vim /usr/local/nginx/conf/nginx.conf
    ......
     server {
      location / {
       ......
       ##添加控制规则##
       deny 192.168.176.100;      #拒绝访问的客户端 IP
       allow all;        #允许其它IP客户端访问
      }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    八.虚拟站点配置

    (一)基于不同域名的虚拟主机

    1.为虚拟主机提供域名解析
    可以写入映射文件,或者做DNS域名解析服务

    echo "192.168.176.100 www.kgc.com www.accp.com" >> /etc/hosts
    
    • 1

    2.准备网页文档

    mkdir -p /var/www/html/kgc
    mkdir -p /var/www/html/accp
    echo "

    www.kgc.com

    " > /var/www/html/kgc/index.html echo "

    www.accp.com

    " > /var/www/html/accp/index.html
    • 1
    • 2
    • 3
    • 4

    3.修改Nginx的配置文件

    vim /usr/local/nginx/conf/nginx.conf
    ......
    http {
    ......
     server {
      listen 80;
      server_name www.kgc.com;     #设置域名www.kgc.com
      charset utf-8;
      access_log logs/kgc.access.log; 
      location / {
       root /var/www/html/kgc;     #设置www.kgc.com 的工作目录
       index index.html index.php;
      }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
       root html;
      }
     }
    
    server {
      listen 80;
      server_name www.accp.com;     #设置域名www.accp.com
      charset utf-8;
      access_log logs/accp.access.log; 
      location / {
       root /var/www/html/accp;
       index index.html index.php;
      }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
       root html;
      }
     } 
    }
    
    nginx -t
    systemctl restart nginx
    
    • 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
    • 37

    (二)基于不同IP地址的虚拟主机

    ifconfig ens33:0 192.168.176.100 netmask 255.255.255.0 
    
    vim /usr/local/nginx/conf/nginx.conf
    ......
    http {
    ......
    server {
      listen 192.168.176.100:80;     #设置监听地址
      server_name www.kgc.com;
      charset utf-8;
      access_log logs/kgc.access.log; 
      location / {
       root /var/www/html/kgc;
       index index.html index.php;
      }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
       root html;
      }
     }
    
    server {
     listen 192.168.176.100:80;     #设置监听地址
     server_name www.accp.com;
     charset utf-8;
     access_log logs/accp.access.log; 
     location / {
      root /var/www/html/accp;
      index index.html index.php;
     }
     error_page 500 502 503 504 /50x.html;
     location = 50x.html{
      root html;
     }
    } 
    }
    
    nginx -t
    systemctl restart nginx
    
    • 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
    • 37
    • 38
    • 39

    (三)基于不同端口号的虚拟主机

    1.基于不同端口号(更改主配置文件的监听地址)

    vim /usr/local/nginx/conf/nginx.conf
    ......
    http {
    ......
    server {
      listen 192.168.71.20:80;     #设置监听地址
      server_name www.kgc.com;
      charset utf-8;
      access_log logs/kgc.access.log; 
      location / {
       root /var/www/html/kgc;
       index index.html index.php;
      }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
       root html;
      }
     }
    server {
     listen 192.168.71.20:8080;     #设置监听地址
     server_name www.accp.com;
     charset utf-8;
     access_log logs/accp.access.log; 
     location / {
      root /var/www/html/accp;
      index index.html index.php;
     }
     error_page 500 502 503 504 /50x.html;
     location = 50x.html{
      root html;
     }
    } 
    }
    
    • 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
  • 相关阅读:
    具有mDNS功能的串口服务器
    尚医通 (二十八) --------- 医院管理相关 (引入注册中心、远程调用)
    QTday3
    案例分享|金融业数据运营运维一体化建设
    【数学】Monocarp and the Set—CF1886D
    问题解决 - Druid的Spring监控不生效(aop-patterns设置了Spring监控依然不生效)
    javaweb基于vue影院在线售票系统的设计与实现
    Verilog开源项目——百兆以太网交换机(二)AES加解密模块设计
    【Android】(最新)跑马灯文字水平滚动(79/100)
    js:深拷贝与浅拷贝的区别,如何实现深拷贝
  • 原文地址:https://blog.csdn.net/weixin_69509991/article/details/125910976