Nginx的配置与开发学习(二)

版权声明:转载记得宣传奥~。~ https://blog.csdn.net/c_ym_ww/article/details/87254609

Nginx模块讲解

Nginx官方模块

编译选项 作用
–with-http_stub_status_module Nginx的客户端状态

http_stub_status_module配置

server {
    listen       80;  //确保端口不被占用
    server_name  localhost;

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

    #添加内容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    ......
}
  1. 检查语法是否正确
    nginx -tc /etc/nginx/nginx.conf

  2. 没问题之后重载服务
    nginx -s reload -c /etc/nginx/nginx.conf

  3. 访问网址
    ip:port/mystatus

    Active connections: 1 
    server accepts handled requests
     255(处理握手次数) 255(处理连接数) 265(总的请求数) 
    Reading: 0 Writing: 1 Waiting: 0 
    

默认模块

编译选项 作用
–with-http_random_index_ module 目录中选择一个随机主页

http_random_index_ module配置

server {
    listen       80;  //确保端口不被占用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
        root   /opt/app/code;      ##文件夹中放3个html,访问主页之后回随机访问这3个html(不能以.xx.html命名)
        random_index on;   #默认为off
        index  index.html index.htm;
    }
    ......
}
编译选项 作用
–with-http_sub_module http内容替换

–with-http_sub_module配置

server {
    listen       80;  //确保端口不被占用
    server_name  localhost;

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

    location / {
        root   /opt/app/code; ##该目录下的html文件是要替换的内容
        index  index.html index.htm;
        sub_filter '需要替换的字符串' '被替换的内容';##网页中需要替换的内容
        sub_filter_once off;   ##设置全局替换,而不是只替换一次
    }
    .......
}

Nginx的请求限制(压测工具 ab)

HTTP请求建立在一次TCP的连接基础上

一次TCP请求至少产生一次HTTP请求

    limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
    limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server {
    listen       80;  //确保端口不被占用
    server_name  localhost;

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

    #添加内容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /opt/app/code;
        #limit_conn conn_zone 1;   限制连接数
        #limit_req zone=req_zone burst=3 nodelay; 对前3个请求延迟响应,其他全部返回50X
        #limit_req zone=req_zone burst=3;对前3个请求延迟响应,其他全部等待
        #limit_req zone=req_zone;
        index  index.html index.htm;
    }
    ......
}

Nginx的访问控制

  1. 基于IP的访问控制 ---- http_access_module
server {
    listen       80;  //确保端口不被占用
    server_name  localhost;

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

    #添加内容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /opt/app/code;
        index  index.html index.htm;
    }
    
    location ~ ^/admin.html {#模式匹配
        root   /opt/app/code;
        deny  XXX.XXX.XXX.XX;
        allow all;
        index  index.html index.htm;
    }
    
    location ~ ^/admin.html {#模式匹配
        root   /opt/app/code;
        allow XXX.XXX.XXX.XX
        deny  all;
        index  index.html index.htm;
    }
    ......
}

局限性:你限制了ip1地址,但是你实际上用这个ip1去访问的时候采用了代理ip2,那么nginx认为的ip是代理的ip2,就不会限制ip1的地址

解决方法:

  • 采用别的HTTP头信息访问控制,但是头信息可以呗修改 如:http_x_forwarded_for模块,http_x_forwarded_for=Client IP,Proxy(1) IP,Proxy(2) IP…
  • 结合geo模块来解决
  • 通过HTTP自定义变量传递,在HTTP头中自定义变量,一级一级的携带到后端
  1. 基于用户的信任登录----http_auth_basic_module

    • 服务器安装htpasswd工具:yum install httpd-tools -y
    • 生成密码文件,最好在文件上一级目录/etc/nginx: htpasswd -c ./auth_conf cyj
    • 查看密码文件:more auth_conf
    server {
        listen       80;  //确保端口不被占用
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        #添加内容
        location /mystatus {
            stub_status;
        }
        
        location / {
            root   /opt/app/code;
            index  index.html index.htm;
        }
        
        location ~ ^/admin.html {#模式匹配
            root   /opt/app/code;
            auth_basic  "XXXXXXXX请输入密码";
            auth_basic_user_file /etc/nginx/auth_conf;  #存放密码文件路径
            index  index.html index.htm;
        }
        .......
    }
    

    局限性:

    • 用户信息依赖文件方式
    • 操作管理机械,效率低下

    解决方案:

    • Nginx结合LUA实现高效验证
    • Nginx和LDAP打通,利用nginx-auth-ldap模块

猜你喜欢

转载自blog.csdn.net/c_ym_ww/article/details/87254609
今日推荐