Linux下Nginx基础应用

    

Nginx简介

  • Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
  • Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
  • Nginx不但是一个优秀Web服务软件,还具有反向代理负载均衡功能和缓存服务功能,与lvs负载均衡及Haproxy等专业代理软件相比,Nginx部署起来更为简单,方便;在缓存功能方面,它又类似于Squid等专业的缓存服务软件。

Nginx相对于传统的Apache服务的优缺点:

  • (1) 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。在高连接并发的情况下,Nginx是Apache服务器不错的替代品;
  • (2) Nginx 配置简洁,Apach复杂;Nginx静态处理性能比Apache高3倍以上;Apache对PHP支持比较简单,Nginx需要配合其他后端用;Apache的组件比Nginx多;
  • (3) apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程;
  • (4) nginx处理静态文件好,耗费内存少,但apache目前也有它的优势,有很多丰富的特性,所以还需要搭配着来;当然如果能确定nginx就适合需求,那么使用nginx会是更经济的方式;
  • (5) nginx的负载能力比apache高很多,而且nginx改完配置能-t测试一下配置有没有问题,apache重启的时候发现配置出错了,会很崩溃,改的时候都会非常小心翼翼现在看有好多集群站,前端nginx抗并发,后端apache集群,配合的也不错;

Nginx模块化体系结构:

 

nginx的模块根据其功能基本上可以分为以下几种类型:

  • event module: 搭建了独立于操作系统的事件处理机制的框架,及提供了各具体事件的处理。包括ngx_events_module, ngx_event_core_module和ngx_epoll_module等。nginx具体使用何种事件处理模块,这依赖于具体的操作系统和编译选项。
  • phase handler: 此类型的模块也被直接称为handler模块。主要负责处理客户端请求并产生待响应内容,比如ngx_http_static_module模块,负责客户端的静态页面请求处理并将对应的磁盘文件准备为响应内容输出。
  • output filter: 也称为filter模块,主要是负责对输出的内容进行处理,可以对输出进行修改。例如,可以实现对输出的所有html页面增加预定义的footbar一类的工作,或者对输出的图片的URL进行替换之类的工作。
  • upstream: upstream模块实现反向代理的功能,将真正的请求转发到后端服务器上,并从后端服务器上读取响应,发回客户端。upstream模块是一种特殊的handler,只不过响应内容不是真正由自己产生的,而是从后端服务器上读取的。
  • load-balancer: 负载均衡模块,实现特定的算法,在众多的后端服务器中,选择一个服务器出来作为某个请求的转发服务器。

 Nginx进程流程:

  

 Nginx 服务器,正常运行过程中:

  • 多进程:一个 Master 进程、多个 Worker 进程
  • Master 进程:管理 Worker 进程
    • 对外接口:接收外部的操作(信号)
    • 对内转发:根据外部的操作的不同,通过信号管理 Worker
    • 监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
  • Worker 进程:所有 Worker 进程都是平等的
    • 实际处理:网络请求,由 Worker 进程处理;
    • Worker 进程数量:在 nginx.conf 中配置,一般设置为核心数,充分利用 CPU 资源,同时,避免进程数量过多,避免进程竞争 CPU 资源,增加上下文切换的损耗。

以下是常见http功能模块:

http类模块名模块功能说明
ngx_http_core_module http核心模块,对应配置文件中的http段,包含很多指令,如location指令
ngx_http_access_module 访问控制模块,控制网站用户对nginx的访问,对应于配置文件中的allow和deny等指令
ngx_http_auth_basic_module 通过用户名和密码认证的访问控制,如访问站点时需要数据用户名和密码,指令包括auth_basic和auth_basic_user_file
ngx_http_charset_module 设置网页显示字符集。指令之一为charset,如charset utf-8
ngx_http_fastcgi_module fastcgi模块,和动态应用相关。该模块下有非常多的子模块。
ngx_http_flv_module 支持flv视频流的模块,如边下边播
ngx_http_mp4_module 同flv模块
ngx_http_gzip_module 压缩模块,用来压缩nginx返回的响应报文。一般只压缩纯文本内容,因为压缩比例非常大,而图片等不会去压缩
ngx_http_image_filter_module 和图片裁剪、缩略图相关模块,需要安装gd-devel才能编译该模块
ngx_http_index_module 定义将要被作为默认主页的文件,对应指令为index。"index index.html,index.php"
ngx_http_autoindex_module 当index指令指定的主页文件不存在时,交给autoindex指令,将自动列出目录中的文件autoindex {on/off}  
ngx_http_log_module 和访问日志相关的模块,指令包括log_format和access_log
ngx_http_memcached_module 和memcached相关的模块,用于从memcached服务器中获取相应响应数据
ngx_http_proxy_module 和代理相关,允许传送请求到其它服务器
ngx_http_realip_module 当nginx在反向代理的后端提供服务时,获取到真正的客户端地址,否则获取的是反向代理的IP地址
ngx_http_referer_module 实现防盗链功能的模块
ngx_http_rewrite_module 和URL地址重写相关的模块,需要安装pcre-devel才能编译安装该模块
ngx_http_scgi_module simple cgi,是cgi的替代品,和fastcgi类似,但更简单
ngx_http_ssl_module 提供ssl功能的模块,即实现HTTPS
ngx_http_stub_status_module 获取nginx运行状态信息
ngx_http_upstream 和负载均衡相关模块

Nginx官方站点:【官方下载站点:源码包rpm包,版本号A.B.C,B是偶数为稳定版;奇数则为开发版】

第一部分:CentOS7下使用YUM安装Nginx指定版本。

# 准备环境
[root@Server-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@Server-01 ~]# uname -a
Linux Server-01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@Server-01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@Server-01 ~]# setenforce 0
[root@Server-01 ~]# systemctl stop firewalld
[root@Server-01 ~]# hostname -I
172.16.70.37

# 安装nginx-1.14.2;目前(2021-07)最新版本为nginx-1.20.1
[root@nginxServer ~]# rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
[root@nginxServer ~]# nginx -v
nginx version: nginx/1.14.2

[root@nginxServer ~]# rpm -qa | grep nginx
nginx-1.14.2-1.el7_4.ngx.x86_64
[root@nginxServer ~]# rpm -ql nginx | less
/etc/logrotate.d/nginx
/etc/nginx         # 相关配置文件存放目录
/etc/nginx/conf.d            
/etc/nginx/conf.d/default.conf     
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.2
/usr/share/doc/nginx-1.14.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html    # 默认站点存放目录
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx        # 日志文件存放目录

-----------------------------------------------------------------------------
# systemctl命令:
服务状态 systemctl status nginx
服务启动 systemctl start nginx
服务停止 systemctl stop nginx
服务重启 systemctl restart nginx

# nginx命令语法及参数:
[root@nginxServer ~]# nginx -h
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : 输出版本号
  -V            : 输出版本号以及编译选项
  -t            : 检查配置文件的语法
  -T            : 检查配置文件的语法并输出配置的内容
  -q            : 静默模式,不输出任何信息
  -s signal     : 向主进程发送信号:stop, quit, reopen, reload
  -p prefix     : 设置nginx的basedir(默认为编译时的prefix)
  -c filename   : 指定配置文件
  -g directives : 提前设置全局指令
-------------------------------------------------------------------------------
# 启动,并设置开机启动
[root@nginxServer ~]# systemctl start nginx && systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

[root@nginxServer ~]# ps -ef |grep nginx
root       1466      1  0 16:54 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      1467   1466  0 16:54 ?        00:00:00 nginx: worker process
root       1486   1178  0 16:54 pts/0    00:00:00 grep --color=auto nginx
[root@nginxServer ~]# netstat -nutlp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1466/nginx: master
  • Web检测测试效果。

Nginx默认配置文件解析:

[root@nginxServer ~]# cat /etc/nginx/nginx.conf

user  nginx;      # 配置用户或组
worker_processes  1;      # worker允许生成的进程数

error_log  /var/log/nginx/error.log warn;    # 制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
pid        /var/run/nginx.pid;    # 指定nginx进程运行文件存放位置


events {    # 事件区块
    worker_connections  1024;    # 每个worker进程支持的最大连接数
}


http {    # http区块
    include       /etc/nginx/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;    # 默认log格式

    sendfile        on;    # 允许sendfile方式传输文件,可以在http块,server块,location块
    #tcp_nopush     on;

    keepalive_timeout  65;   # 连接超时时间,可以在http块,server块,location块

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;  # 生效包含自定义配置
}

[root@nginxServer nginx]# cat conf.d/default.conf
server {    # 网站配置区域
    listen       80;  # 提供服务的端口,默认80
    server_name  localhost;    # 提供服务的域名主机名

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {    # location区块开始
        root   /usr/share/nginx/html;    # 站点的根目录(相对与nginx安装目录)
        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;    # 出现对应的http状态码时,使用50x.html回应客户
    location = /50x.html {      # location区块开始,访问50x.html
        root   /usr/share/nginx/html;    # 指定对应的站点目录为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;
    #}
}

-----------------------------------------------------------
# 整个nginx配置文件的核心框架如下
worker_processes 1;
events {
    
    worker_connections 1024;

}
http {
    include mime.types;
    server {
        listen  80;
        server_name localhost;
        location / {
            root  html;
            index  index.html index.htm;
        }
    }
}

 第二部分:设置虚拟主机。

  • nginx使用server容器定义一个虚拟主机。在nginx中,没有严格区分基于IP和基于名称的虚拟主机,它们通过listen指令和server_name指令结合起来形成不同的虚拟主机。
# 配置文件目录结构
[root@nginxServer ~]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│   ├── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

2 directories, 10 files

说明:其中".default"后缀的是对应前缀配置文件的备份配置文件,".params"是对应前缀的参数文件。

# 创建站点目录及站点内容
[root@nginxServer ~]# for i in {1..3}; do mkdir -p /data/web/nginx/www$i; echo "Mysite-$i www$i.nginx.com" > /data/web/nginx/www$i/index.html; done
[root@nginxServer ~]# tree /data/web/nginx/
/data/web/nginx/
├── www1
│   └── index.html
├── www2
│   └── index.html
└── www3
    └── index.html

3 directories, 3 files
[root@nginxServer ~]# cat /data/web/nginx/www*/*
Mysite-1 www1.nginx.com
Mysite-2 www2.nginx.com
Mysite-3 www3.nginx.com

[root@nginxServer ~]# cd /etc/nginx/conf.d/
[root@nginxServer conf.d]# mv default.conf default.conf_bak

# 新增虚拟主机配置文件
[root@nginxServer conf.d]# vim vhosts.conf
# 基于端口port
server {
    listen       8081;
    server_name  www1.nginx.com;
    location / {
        root   /data/web/nginx/www1;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www1;
    }
}

server {
    listen       8082;
    server_name  www2.nginx.com;
    location / {
        root   /data/web/nginx/www2;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www2;
    }
}

# 基于域名FQDN
server {
    listen       80;
    server_name  www1.nginx.com;
    location / {
        root   /data/web/nginx/www1;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www1;
    }
}

server {
    listen       80;
    server_name  www3.nginx.com;
    location / {
        root   /data/web/nginx/www3;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www3;
    }
}

#基于IP地址
server {
    listen       80;
    server_name  172.16.70.37;
    location / {
        root   /data/web/nginx/www2;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www2;
    }
}

server {
    listen       80;
    server_name  172.16.70.38;
    location / {
        root   /data/web/nginx/www3;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www3;
    }
}

# 新增nginx状态信息配置文件
[root@nginxServer conf.d]# vim status.conf
# State information

server{

    listen 80;
    server_name status.nginx.com;
    location / {
        stub_status on;
        access_log off;
    }
}


# 基于域名,添加地址解析
[root@nginxServer conf.d]# echo '172.16.70.37 www1.nginx.com www2.nginx.com www3.nginx.com status.nginx.com' >> /etc/hosts

# 基于IP,添加IP地址
[root@nginxServer conf.d]# ip addr add 172.16.70.38/24 dev ens33
[root@nginxServer conf.d]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 172.16.70.37/24 brd 172.16.70.255 scope global noprefixroute dynamic ens33
    inet 172.16.70.38/24 scope global secondary ens33
[root@nginxServer conf.d]# ping 172.16.70.38 -c2
PING 172.16.70.38 (172.16.70.38) 56(84) bytes of data.
64 bytes from 172.16.70.38: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 172.16.70.38: icmp_seq=2 ttl=64 time=0.059 ms

--- 172.16.70.38 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.015/0.037/0.059/0.022 ms

[root@nginxServer conf.d]# nginx -t    # 检测配置文件语法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@nginxServer conf.d]# nginx -s reload    # 重载nginx
[root@nginxServer conf.d]# netstat -ntupl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1466/nginx: master
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      1466/nginx: master
tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN      1466/nginx: master

# 检测测试结果
[root@nginxServer conf.d]# hostname -I
172.16.70.37
# 基于端口
[root@nginxServer conf.d]# curl 172.16.70.37:8081
Mysite-1 www1.nginx.com
[root@nginxServer conf.d]# curl 172.16.70.37:8082
Mysite-2 www2.nginx.com
# 基于域名
[root@nginxServer conf.d]# curl www1.nginx.com
Mysite-1 www1.nginx.com
[root@nginxServer conf.d]# curl www3.nginx.com
Mysite-3 www3.nginx.com
# 基于IP
[root@nginxServer conf.d]# curl 172.16.70.37
Mysite-2 www2.nginx.com
[root@nginxServer conf.d]# curl 172.16.70.38
Mysite-3 www3.nginx.com

# 显示状态结果
[root@nginxServer conf.d]# curl status.nginx.com
Active connections: 10
server accepts handled requests
 105 105 116
Reading: 10 Writing: 1 Waiting: 11

说明:
  Active connections   当前处于活动状态的客户端连接数,包括正处于等待状态的连接。
  accepts          从服务启动开始到现在已经接收进来的总的客户端连接数。
  handled          从服务启动以来已经处理过的连接数,一般handled的值和accepts的值相等,除非作出了连接数限定。
  requests         从服务启动以来总的客户端请求数,一个连接可以有多个请求。(平均每次握手处理了1.3个数据请求)         
  Reading          nginx正在读取请求首部的数量,即正在从socket recv buffer中读取数据的数量。
  Writing          nginx正在将响应数据写入socket send buffer以返回给客户端的连接数量。
  Waiting          等待空闲客户端发起请求的客户端数量,包括长连接状态的连接以及已接入但socket recv buffer还未产生可读事件的连接,这个值等于active - (reading+writing)

 第三部分:Nginx Web认证

  • 基于用户认证。即需要输入用户名和密码才能访问站点的功能为web身份认证功能。nginx中由ngx_http_auth_basic_module模块提供该功能。指令包括auth_basic和auth_basic_user_file。这两个指令可以在http根段、server段、location段使用。
[root@nginxServer conf.d]# pwd
/etc/nginx/conf.d
[root@nginxServer conf.d]# vim vhosts.conf
server {
    listen       80;
    server_name  www1.nginx.com;
    location / {
        root   /data/web/nginx/www1;
        index  index.html index.htm;
        auth_basic "Auth your name";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/nginx/www1;
    }
}

# htpasswd命令由httpd-tools所提供
[root@nginxServer conf.d]# yum install -y httpd-tools
[root@nginxServer conf.d]# htpasswd -c -b -m /etc/nginx/.ngxpasswd user1 user1
Adding password for user user1
[root@nginxServer conf.d]# htpasswd -b -m /etc/nginx/.ngxpasswd user2 user2
Adding password for user user2

说明:
 -c表示创建密码文件,只能为第一个用户使用该选项,否则后面使用会覆盖前面已经创建过的
 -b选项是表示batch模式,不用交互输入密码
 -m表示强制使用md5

[root@nginxServer conf.d]# cat /etc/nginx/.ngxpasswd
user1:$apr1$MaM8PAS.$FjeZesqyESHjUyUarao.q/
user2:$apr1$3Tvnt.Kp$ysOph6mgMyTHaqgLy6fsH1

[root@nginxServer conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginxServer conf.d]# nginx -s reload
  •  检测测试结果。

  WIN10的hosts文件添加解析:将'172.16.70.37 www1.nginx.com www2.nginx.com www3.nginx.com'添加到C:\Windows\System32\drivers\etc\hosts

 

Nginx访问日志轮询切割

  • 默认情况下Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于日志的分析和处理,因此,有必要对Nginx日志,按天或按小时进行切割,使其分成不同的文件保存。
[root@nginxServer nginx]# pwd
/etc/nginx

[root@nginxServer nginx]# cat cut_nginx_log.sh
#!/bin/bash
#日志切割脚本可挂定时任务,每天00点整执行

Dateformat=`date +%Y%m%d`
Nginxlogdir="/var/log/nginx"
Logname="access"

[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/usr/bin/mv ${Logname}.log ${Logname}.log_${Dateformat}
/usr/sbin/nginx -s reload

# 新增计划任务
[root@nginxServer nginx]# cat /etc/cron.d/cut_nginx_log.cron
#cut nginx access log
00 00 * * *    root /usr/bin/bash /etc/nginx/cut_nginx_log.sh >/dev/null 2>&

 

posted @ 2021-08-02 08:55  讲文张字  阅读(649)  评论(0编辑  收藏  举报
返回顶部