nginx 服务企业应用

1.nginx安装

 编译  模块编译少了,后期维护成本高。
 yum   默认集成模块,基本上够用。
 官方:版本较为新,配置文件简洁明了。
 epel:版本较低,而且主要的配置文件注释过多,不易读

配置官方源
cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 yum install nginx -y 安装nginx

2.nginx常用基础模块

1.nginx目录索引
Syntax:    autoindex on | off;
Default:    
autoindex off;
Context:    http, server, location

#Syntax:    autoindex_exact_size on | off;
Default:    
autoindex_exact_size on;
Context:    http, server, location
默认为on,显示出文件得确切大小,单位是bytes
修改为off,显示出文件的大概大小,单位是KB或者MB或者GB


Syntax:    autoindex_localtime on | off;
Default:    
autoindex_localtime off;
Context:    http, server, location
默认为off ,显示文件时间为GMT时间
修改为on,显示文件时间为文件的服务器时间

charset utf-8,gbk;
默认中文目录乱码,添加解决乱码

 2.状态模块

1. location = /basic_status {
    stub_status;
}



Active connections: 291 
server accepts handled requests
 16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106

 2.Nginx 状态详细信息如下
 Active connections  # 当前活动的连接数
 accepts             # 当前的总连接数TCP     accepts-handled=失败的总连接数
 handled             # 成功的连接数TCP
 requests            # 总的http请求数

 Reading             # 请求
 Writing             # 响应
 Waiting             # 等待的请求数,开启了keepalive

 3.nginx访问控制

基于ip的访问控制

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}


基于来源IP
如果匹配成功,则不继续匹配下面的内容。
如果匹配不成功,则继续往下寻找能匹配成功的内容。
deny和allow的顺序是有影响的。默认是从第一条开始进行匹配。
        
1) 访问控制配置示例, 拒绝指定的IP访问该网站的某个location, 其他全部允许
            location /nginx_status {
                stub_status;
                deny 10.0.0.1/32;  #拒绝某个IP
                allow all;         #允许所有人
            }
2)访问控制配置示例2: 只允许指定的用户能访问该网站的某个location, 其它全部拒绝
            location /nginx_status {
                stub_status;
                allow 10.0.0.1/32;  #允许指定的人
                deny all;            # 拒绝所有人
            }

 2. 基于用户名和密码访问认证

    1.先使用htpasswd创建对应的用户和密码
    yum install httpd-tools
    htpasswd -b -c /etc/nginx/auth_conf oldboy oldboy

       -c  Create a new file.
       创建一个新的密码文
       -b  Use the password from the command line rather than prompting for it.
       采用免交互的方式输入用户的密码信息

    2.nginx的配置
    location / {
        root /code/module;
        index index.html;
        auth_basic "Message";
        auth_basic_user_file /etc/nginx/auth_conf;
    }
    3.nginx -t   检查语法
    4. systemctl reload nginx   平滑重启

 4.访问限制

访问限制
    限制连接数   tcp
    http {
        #定义连接限制
        limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    }
    server {
        limit_conn conn_zone 1;
    }

5.location匹配优先级

location匹配优先级
匹配符        匹配规则            优先级
=    精确匹配                    1
^~    以某个字符串开头                2
~    区分大小写的正则匹配            3
~*    不区分大小写的正则匹配            4
!~    区分大小写不匹配的正则            5
!~*    不区分大小写不匹配的正则        6
/    通用匹配,任何请求都会匹配到    7

        
location 应用场景
# 通用匹配,任何请求都会匹配到
location / {
    ...
}

# 严格区分大小写,匹配以.php结尾的都走这个location    
location ~ \.php$ {
    ...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location 
location ~ \.jsp$ {
    ...
}

# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
    ...
}
# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
    ...
}

 测试location的访问

 vim www.conf
server {
    listen       80;
    server_name  www.nmtui.com;
    root   html/www;
    location / {
       return 401;
    }
    location = / {
        return 402;
    }
    location /documents/ {
        return 403;
    }
    location ^~ /images/ {
        return 404;
    }
    location ~* \.(gif|jpg|jpeg)$ {
    return 500;
    }
    access_log logs/access_www.log main;
}

4.9.4.2  访问测试
根据请求不同uri的返回值验证 location的配置。
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com/documents
401
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com
402
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com/documents/ss.jpg
500
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com/documents
401
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com/documents/
403
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s  www.nmtui.com/images/1.jpg
404

 

posted @ 2020-11-15 14:19  xiaoxiaoren520  阅读(88)  评论(0编辑  收藏  举报