瞅一眼nginx

目录

什么是nginx?

nginx配置官方yum源:

nginx优点

nginx 缺点

查看nginx默认模块

nginx新版本的配置文件:

nginx目录索引

nginx状态监控(status模块)

nginx基于用户的访问控制(auth模块)

nginx的访问限制***

nginx日志格式:log_format

nginx的location ***** 


什么是nginx?

世界最大web服务器软件,以高并发、低消耗著称,源自于俄罗斯,创建者 Igor(伊戈尔),2004年开源,最早以代理服务器的身份出现,2015成立nginx公司,2019年以6.7亿被F5 Networks公司收购。

瞅一眼nginx_第1张图片

 

nginx配置官方yum源:

http://nginx.org/en/linux_packages.html#RHEL-CentOS
vim /etc/yum.repos.d/nginx.repo
添加:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出

nginx优点

高性能:Nginx采用了异步非阻塞的事件驱动模型,能够处理大量并发连接,较低的资源消耗,具有出色的性能表现

高度可靠:它具备优秀的故障恢复能力,能够在面对高峰时段或者异常访问情况下保持稳定。

可扩展性强:反向代理服务器,将请求分发到多个后端服务器,实现负载均衡和高可用性。它还支持动态模块加载,可以通过插件来扩展功能,非常灵活。

内存占用低:相对于传统的Web服务器,Nginx占用的内存更少,可以更好地利用服务器的资源。

nginx 缺点

尽管Nginx拥有许多优点,但它也有一些缺点需要了解比如:在Windows环境下的支持较弱,限制性的脚本语言支持,缺少内置支持,适应范围较小,动态内容处理相对复杂,等一些因素

查看nginx默认模块

nginx -V
 

nginx新版本的配置文件:

全局配置文件:/etc/nginx/nginx.conf
    虚拟主机配置:/etc/nginx/conf.d/*.conf
    例子1:使用域名搭建一台虚拟主机
            mkdir  /www
            复制网页代码到/www目录下
            vim /etc/nginx/conf.d/www.conf
            添加:
            server {
                listen       80;
                server_name  www.yundong.com;
                location / {
                    root   /www;
                    index  index.html index.htm;
                }
    
            }
            保存退出
            systemctl restart nginx 
            客户端修改/etc/hosts访问测试

nginx目录索引

nginx默认不起用目录索引,更不允许列出网站目录提供下载。
    Syntax:    autoindex on | off;                索引功能的开或关
    Default:    autoindex off;                默认关闭
    Context:    http, server, location        场景:全局、某个虚拟主机、某个虚拟主机的目录
    
    例子:在www网站下,创建download下载目录,索引显示
    mkdir /www/download
    复制文件到/www/download目录下
    vim  /etc/nginx/conf.d/www.conf
    在server字段中添加:
    location /download {
        root   /www;
        autoindex on;                        启用索引显示
        charset utf-8,gbk;                    字符编码为中文
        autoindex_exact_size on;            显示文件大小        
        autoindex_localtime on;                显示文件创建时间
    }
    保存退出
    systemctl reload nginx
    客户端测试访问:http://www.www.com/download

nginx状态监控(status模块)

Syntax:    stub_status;                    启用状态化追踪
    Default:    —                            默认关闭
    Context:    server, location            场景:

    例子:针对www网站,启用状态化追踪
    vim  /etc/nginx/conf.d/www.conf
    在server字段中添加:
    location /status {
        stub_status;                        启用状态化追踪
        access_log off;                        关闭status的日志记录
    }
    保存退出
    systemctl reload nginx
    客户端访问:http://www.www.com/status
    客户端显示结果如下:  ***
    Active connections: 1                     当前活跃的连接数
    server  accepts     19                 当前的总tcp连接数
        handled     19                 成功的连接数
        requests    486                总HTTP请求数
  

nginx基于用户的访问控制(auth模块)

环境知识:
    网站用户认证方式:摘要式  基本式身份验证
    
    例子:设置访问/status,用户密码验证
    yum -y install httpd-tools
    htpasswd -b -c /etc/nginx/.auth_conf webadmin 123456
    vim  /etc/nginx/conf.d/www.conf
    修改为:
         location /status {
            stub_status;
            access_log off;
            auth_basic           "input your passwd:";    用户验证启用描述
            auth_basic_user_file /etc/nginx/.auth_conf;    用户验证文件路径
        }

nginx的访问限制***

    limit_conn_module        连接频率限制
    例子:
    http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;   创建zone区域名为addr,大小10m,保存客户端的二进制ip
        server {
            location /download/ {
                    limit_conn addr 1;                    一个ip同一时间点只允许建立一个连接
                    }
            }
    }
    
    limit_req_module        请求频率限制
    例子:
    vim /etc/nginx/nginx.conf
    http {
    第23行插入:
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;    创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
    保存退出 
    
    vim /etc/nginx/conf.d/www.conf 
    server {
            location /download {
                limit_req zone=one burst=5;                        调用请求速率区域,另外设置额外突发5次
                }
            }
    }

nginx日志格式:log_format

例子:
    vim /etc/nginx/nginx.conf
    http {
        log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$request" '        #定义日志输出格式main
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;                                    #调用日志格式main
    }
    #nginx日志格式的变量:
    $remote_addr            #记录客户端的ip地址
    $remote_user            #记录客户端的用户名
    $time_local                #通用的时间格式
    $time_iso8601            #iso8601时间格式
    $request                #请求的方法和请求的HTTP协议
    $status                    #请求状态码
    $body_bytes_sent        #服务器回应的字节数,不包含头部大小
    $bytes_sent                #服务器回应的总字节数
    $msec                    #日志写入时间,单位为秒,精度为毫秒
    $http_referer            #记录链接访问源地址
    $http_user_agent        #记录客户端浏览器信息
    $http_x_forwarded_for    #代理服务器ip
    $request_length            #请求包的长度(请求头+请求正文)
    $request_time            #请求花费的时间,单位为秒,精度为毫秒 ***

nginx的location ***** 

语法详解
语法规则: location [=|~|~*|^~] /uri/ { … }
下列以优先级从高到低排序

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。

~ 开头表示区分大小写的正则匹配                    

~* 开头表示不区分大小写的正则匹配             

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/ 通用匹配,任何请求都会匹配到。

例子:测试匹配符的优先级
cd /etc/nginx/conf.d/
vim test.conf
添加:
server {
        listen 80;
        server_name test.benet.com;

        location / {
                default_type text/html;
                return 200 "location /";
        }
        location =/ {
                default_type text/html;
                return 200 "location =/";
        }
        location ~ / {
                default_type text/html;
                return 200 "location ~ /";
        }
        location ~* / {
                default_type text/html;
                return 200 "location ~* /";
        }
}
保存退出
客户端修改hosts文件,测试访问

真实企业场景配置:***
#通用匹配,任何请求都会匹配到。
location / {
    root /www;
}

#严格区分大小写,匹配.php结尾
location ~ \.php$ {
    fastcgi_pass http://127.0.0.1:9000;
}

#严格区分大小写,匹配.jsp结尾
location ~ \.jsp$ {
    proxy_pass http://127.0.0.1:8080;
}

#不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$ {
    default_type text/html;
    return 403 "启用访问控制";
}    

你可能感兴趣的:(liunx,nginx,运维)