【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
保存退出

2.安装nginx

yum -y install nginx 

3.查看nginx默认模块

nginx -V

4.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访问测试
            

5.nginx目录索引(autoindex自动索引模块) ***  

    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
    

6.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请求数
  

7.nginx基于ip的访问控制(access模块)

    例子:仅允许内部网段或vpn访问status
    vim  /etc/nginx/conf.d/www.conf
    修改为:
         location /status {
            stub_status;
            access_log off;
            allow 192.168.1.0/24;    仅允许1.0网段访问
            deny all;                拒绝其他所有
        }

8.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;    用户验证文件路径
        }

9.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        请求频率限制
    例子:
    http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;    创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
        server {
            location /download {
                limit_req zone=one burst=5;                        调用请求速率区域,另外设置额外突发5次
                }
            }
    }
    

10.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            #请求花费的时间,单位为秒,精度为毫秒 ***
    

11.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文件,测试访问
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值