Nginx——安装、访问统计及访问控制

关于Nginx

一款高性能、轻量级Web服务软件

  • 稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高
    • 单台物理服务器可支持30000~ 50000个并发请求

Nginx编译安装

#首先先挂关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

#安装语言支持包

[root@localhost ~]# yum -y install pcre-devel zlib-devel

#挂载解压安装包

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.17.1/LNMP /abc
Password for root@//192.168.17.1/LNMP:  
[root@localhost ~]# cd /abc
[root@localhost abc]# ls
Discuz_X3.4_SC_UTF8.zip    php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  php-7.1.20.tar.bz2
ncurses-5.6.tar.gz         php-7.1.20.tar.gz
nginx-1.12.2.tar.gz        zend-loader-php5.6-linux-x86_64_update1.tar.gz
php-5.6.11.tar.bz2
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt

#添加程序用户

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

#配置编译安装

[root@localhost abc]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \               //安装路径
> --user=nginx \                           //指定用户
> --group=nginx \                            //指定组
> --with-http_stub_status_module               //开启统计模块

[root@localhost nginx-1.12.2]# make && make install        //编译安装

#安装完成可以查看/usr/local/nginx

sbin目录 命令文件

logs 目录  日志文件

html目录,两个文件

​    50X.html   错误页面

​    index.html   欢迎首页页面

conf目录  配置文件

#建立连接便于管理

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/                    //nginx命令执行路径优化

#验证语法,检查配置文件

[root@localhost nginx-1.12.2]# 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
----检查、启动、重启、停止------
nginx- t		//检查
nginx		//启动
killall -s HUP nginx	//重启,或killall -1 nginx
killall -s QUIT nginx	//停止,或killall -3 nginx

#制作service管理脚本

[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"           //变量指向命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"       //变量指向nginx进程号   
case "$1" in
     start)
	$PROG
	;;
    stop)
	kill -s QUIT $(cat $PIDF)             //QUIT信号表示退出进程
	;;
    restart) 
	$0 stop
	$0 start
	;;
    reload)
	kill -s HUP $(cat $PIDF)           //HUP信号表示重载配置
	;;
    *)
	echo "Usage: $0 {start |stoplrestart |reload}"
	exit 1
esac
exit 0

[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx      //添加执行权限
[root@localhost nginx-1.12.2]# chkconfig --add nginx           //添加系统识别命令
[root@localhost nginx-1.12.2]# chkconfig --level 35 nginx on    //设置开机自启

#启动服务

[root@localhost nginx-1.12.2]# service nginx start 
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8216/nginx: master  

#进入网页进行验证,安装成功

在这里插入图片描述

Nginx的访问状态统计

启用HTTP_STUB_STATUS状态统计模块

  • 配置编译参数时添加–with-http_stub_status_module

  • nginx -V 查看已安装的Nginx是否包含HTTP_STUB_STATUS模块

#进入主配置文件

vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

在这里插入图片描述

#状态统计比较好配置,最主要的就是最后添加的访问位置及打开统计功能

现在就可以重启服务并进行访问

在这里插入图片描述

基于授权的访问控制

1、生成用户密码认证文件

2、修改主配置文件对应目录,添加认证配置项

3、重启服务,访问测试

#首先我们先去创建用户,由于tools工具是Apache自带的,所以我们需要先安装工具

[root@localhost conf]# yum install -y httpd-tools              //安装工具
[root@localhost conf]# htpasswd -c /usr/local/nginx/pwd.db stu     //生成文件路径并创建用户
New password: 
Re-type new password: 
Adding password for user stu
[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  fastcgi_temp  logs        pwd.db  scgi_temp
conf              html          proxy_temp  sbin    uwsgi_temp
[root@localhost nginx]# cat pwd.db 
stu:$apr1$oWaiUzZH$gvOrRgEyVdZOejyL.leqG.

#因为需要进行域名访问及访问控制,我们来配置域名解析

[root@localhost nginx]# yum install -y bind
[root@localhost nginx]# vim /etc/named.conf 
…………省略
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; };
…………省略

[root@localhost nginx]# vim /etc/named.rfc1912.zones 
…………省略
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
…………省略
[root@localhost nginx]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost 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.17.144
root@localhost named]# systemctl start named

#下面我们来对nginx主配置文件进行修改

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述
权限条目是自上而下,逐条匹配的,一旦匹配到,下面的将不去匹配!

#访问权限配置完成后重启服务,现在我们就可以来执行验证了

首先,我们先用其他主机进行访问,可以看到是需要进行密码验证的

在这里插入图片描述
然后我们在使用192.168.17.129主机来进行访问,可以看到访问报错403,权限问题无法访问

在这里插入图片描述

发布了72 篇原创文章 · 获赞 44 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ML908/article/details/103653927