nginx访问控制的实现示例

关于Nginx一款高性能,轻量级web服务软件

稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高

单台物理服务器可支持30000~50000个并发请求

环境

  • 一台Linux服务器(192.168.13.128)
  • 一台win10测试机

一,在Windows上将LAMP所需压缩软件包共享出来(此处如有问题请看之前的博客相关文章)

nginx访问控制的实现示例 nginx 第1张

二,在Linux上使用远程共享获取文件并挂载到mnt目录下

  [root@localhost ~]# smbclient -L //192.168.100.3/ ##远程共享访问  Enter SAMBAroot's password:         Sharename  Type  Comment      ---------  ----  -------      LAMP-C7   Disk    [root@localhost ~]# mount.cifs //192.168.100.3/LAMP-C7 /mnt ##挂载到/mnt目录下

三,编译安装Nginx

1,解压源码包到/opt下,并查看

  [root@localhost ~]# cd /mnt ##切换到挂载点目录  [root@localhost mnt]# ls  apr-1.6.2.tar.gz     Discuz_X2.5_SC_UTF8.zip LAMP-php5.6.txt  apr-util-1.6.0.tar.gz    error.png    mysql-5.6.26.tar.gz  awstats-7.6.tar.gz    httpd-2.4.29.tar.bz2  nginx-1.12.0.tar.gz  cronolog-1.6.2-14.el7.x86_64.rpm kali.jpg     php-5.6.11.tar.bz2  [root@localhost mnt]# tar zxvf nginx-1.12.0.tar.gz -C /opt ##解压Nginx源码包到/opt下  [root@localhost mnt]# cd /opt/ ##切换到解压的目录下  [root@localhost opt]# ls  nginx-1.12.0 rh

2,安装编译需要的环境组件包

  [root@localhost opt]# yum -y install   gcc           //c语言  gcc-c++       //c++语言  pcre-devel       //pcre语言工具  zlib-devel      //数据压缩用的函式库

3,创建程序用户nginx并编译Nginx

  [root@localhost opt]# useradd -M -s /sbin/nologin nginx ##创建程序用户,安全不可登陆状态  [root@localhost opt]# id nginx  uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)  [root@localhost opt]# cd nginx-1.12.0/     ##切换到nginx目录下  [root@localhost nginx-1.12.0]# ./configure    ##配置nginx  > --prefix=/usr/local/nginx   ##安装路径  > --user=nginx        ##用户名  > --group=nginx       ##用户组  > --with-http_stub_status_module  ##状态统计模块

4,编译和安装

  [root@localhost nginx-1.12.0]# make  ##编译  ...  [root@localhost nginx-1.12.0]# make install ##安装  ...

5,优化nginx启动脚本,以便于系统识别

  [root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##创建软连接让系统识别nginx启动脚本  [root@localhost nginx]# nginx -t  ##检查配置文件的语法问题  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  [root@localhost nginx]# nginx  ##开启ngnix  [root@localhost nginx]# netstat -ntap | grep 80  ##查看端口,nginx已经开启  tcp  0  0 0.0.0.0:80    0.0.0.0:*    LISTEN  39620/nginx: master   [root@localhost nginx]# systemctl stop firewalld.service ##关闭防火墙  [root@localhost nginx]# setenforce 0 

6,安装elinks网页测试工具,并进行测试

  [root@localhost nginx]# yum install elinks -y ##安装elinks软件  [root@localhost nginx]# elinks http://localhost ##测试nginx网页

nginx访问控制的实现示例 nginx 第2张

7,服务开启重载以及关闭

  [root@localhost nginx]# killall -s QUIT nginx ##停止 或者使用killall -3 nginx  [root@localhost nginx]# killall -s HUP nginx ##重启 或者使用killall -1 nginx  [root@localhost nginx]# nginx       ##开启

8,制作管理脚本,便于使用service管理使用

  [root@localhost nginx]# killall -s QUIT nginx ##停止 或者使用killall -3 nginx  [root@localhost nginx]# killall -s HUP nginx ##重启 或者使用killall -1 nginx  [root@localhost nginx]# nginx       ##开启

Nginx的访问状态统计

启用HTTP_STUB_STATUS状态统计模块
nginx -V可以查看已安装的Nginx是否包含统计模块

一,修改nginx配置文件

  [root@localhost ~]# cd /usr/local/nginx/conf  ##切换到配置文件目录  [root@localhost conf]# vim nginx.conf    ##修改Nginx配置文件    server {   listen  80;   server_name www.kgc.com;  ##指明一个域名     charset utf-8;         ##中文字符集     #access_log logs/host.access.log main;     location / {    root html;    index index.html index.htm;   }   location /status {       ##添加状态统计   stub_status on;   access_log off;   }

二,安装DNS服务器做域名解析

1,安装bind服务

  [root@localhost conf]# yum install bind -y ##安装DNS服务

2,配置主配置文件/etc/named.conf

   [root@localhost conf]# vim /etc/named.conf ##主配置文件    options {      listen-on port 53 { any; };  ##将本机监听为所有      listen-on-v6 port 53 { ::1; };      directory  "/var/named";      dump-file  "/var/named/data/cache_dump.db";      statistics-file "/var/named/data/named_stats.txt";      memstatistics-file "/var/named/data/named_mem_stats.txt";      recursing-file "/var/named/data/named.recursing";      secroots-file "/var/named/data/named.secroots";      allow-query  { any; };  ##允许所有

3,配置区域配置文件(etc/named.rfc1912.zones)

  [root@localhost conf]# vim /etc/named.rfc1912.zones ##配置区域配置文件  zone "localhost" IN {    ##复制模板到下面          type master;          file "named.localhost";          allow-update { none; };  };    zone "kgc.com" IN {    ##修改localhost为kgc.com          type master;          file "kgc.com.zone";  ##创建区域数据配置文件          allow-update { none; };  };

4,编辑区域数据配置文件( kgc.com.zone)

  [root@localhost conf]# cd /var/named  [root@localhost named]# cp -p named.localhost kgc.com.zone   ##复制模板为kgc.com.zone  [root@localhost named]# vim kgc.com.zone ##编辑区域数据配置文件    $TTL 1D  @  IN SOA @ rname.invalid. (                    0  ; serial                    1D  ; refresh                    1H  ; retry                    1W  ; expire                    3H ) ; minimum          NS  @          A  127.0.0.1  www IN A  192.168.13.128 ##删除ipv6 添加域名解析地址为本机

5,关闭防火墙并开启服务

  [root@localhost named]# systemctl start named ##开启dns服务  [root@localhost named]# systemctl stop firewalld.service ##关闭防火墙  [root@localhost named]# setenforce 0 ##关闭增强功能

6,利用win10测试机来测试

nginx访问控制的实现示例 nginx 第3张

nginx访问控制的实现示例 nginx 第4张

基于授权的访问控制

配置步骤与Apache基本一致

生成用户密码认证文件
修改主配置文件对相应目录,添加认证配置项
重启服务,访问测试

一,修改主配置文件

  [root@localhost ~]# cd /usr/local/nginx/conf  ##切换到配置文件目录  [root@localhost conf]# vim nginx.conf    ##修改Nginx配置文件     location / {    auth_basic "secret";        ##验证类型    auth_basic_user_file /usr/local/nginx/passwd.db;  ##验证文件路径    root html;    index index.html index.htm;   }

二,安装httpd-tools工具包,设置密码认证文件

  [root@localhost conf]# yum install httpd-tools -y ##安装工具包  [root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db test ##设置密码认证文件  New password:   ##输入密码  Re-type new password:  ##确认密码  Adding password for user test  [root@localhost conf]# cat /usr/local/nginx/passwd.db  ##查看密码认证文件  test:$apr1$LqqHZeX3$24E7/HeacTVRzKA7nvSgY/  [root@localhost conf]# service nginx stop  ##关闭服务  [root@localhost conf]# service nginx start  ##开启服务

三,使用win10测试机测试

nginx访问控制的实现示例 nginx 第5张

nginx访问控制的实现示例 nginx 第6张

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支Fatmouse

参与评论