zl程序教程

您现在的位置是:首页 >  系统

当前栏目

nginx模块

2023-09-14 09:15:48 时间

目录

搭建域名虚拟主机

nginx索引

状态索引

访问控制

 基于ip限制

基于用户限制


配置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

搭建域名虚拟主机

        安装nginx并通过修改配置文件来指定网站存放目录为/data。

[root@node1 ~]# yum -y install nginx

[root@node1 ~]# mkdir /data

[root@node1 ~]# vim /etc/nginx/conf.d/www.conf

server {

        listen 80;

        server_name www.aaa.com;

        location / {

        root /data;

        index index.html index.htm;

        }

}

[root@node1 ~]# systemctl start nginx

[root@node1 ~]# cd /data/

[root@node1 data]# echo "192.168.1.10" > index.html

        此时可以看到网页存放在/data下,访问浏览器看到内容即可。

nginx索引

        在/data网站下,创建download下载目录,启用索引显示。

[root@node1 ~]# mkdir -p /data/download

[root@node1 ~]# cp -rp /media/nginx-rpm/* /data/download/ //复制网页下载内容到download目录下,文件或安装包都可。

[root@node1 ~]# vim /etc/nginx/conf.d/data.conf //编辑配置文件

server {

        location /download {

        root /data;

        autoindex on; //启用索引显示

        charset utf-8,gbk; //字符编码为中文

        autoindex_exact_size on; //显示文件大小

        autoindex_localtime on; //显示文件创建时间

        }

}

[root@node1 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@node1 ~]# systemctl restart nginx

[root@node1 ~]# chmod -R 707 /data/download/

状态索引

        针对网站启用状态化追踪。(在server字段下添加status模块)

[root@node1 ~]# vim /etc/nginx/conf.d/data.conf 

server {

//省略部分内容

        location /status {

        stub_status; //启用状态化追踪

        access_log off; //关闭status的日志记录

        }

}

[root@node1 ~]# systemctl restart nginx

        访问http://192.168.1.10/status,可以看到下图所示内容。

Active connections: 2 当前活跃的连接数

server  accepts 2  当前的总tcp连接数

handled 2 成功的连接数

requests 4 总HTTP请求数

        下面可以通过客户端使用压力测试工具对nginx服务器进行压测。

[root@client ~]# yum -y install httpd-tools

[root@client ~]# ab -c 1000 -n 1000 http://192.168.1.10/status

在次刷新后可以看到相关数据

访问控制

        基于ip限制

        启用access模块,仅允许对内部网段或vpn访问status。在status模块中添加两行内容,测试时可以仅允许一个主机访问或划分子网。

[root@node1 ~]# vim /etc/nginx/conf.d/data.conf

//省略部分内容

location /status {

         stub_status;

         access_log off;

         allow 192.168.1.100; //仅允许1.100主机访问

         deny all; //拒绝所有主机访问

}

[root@node1 ~]# systemctl restart nginx

        测试使用1.100主机访问刷新几次显示正常,再用1.4客户端访问为Forbidden说明配置成功。

  

        基于用户限制

        启用auth模块,设置访问/status用户需要密码验证。

[root@node1 ~]# yum -y install httpd-tools

[root@node1 ~]# htpasswd -b -c /etc/nginx/.auth_conf lisi 123 //用户lisi密码123

Adding password for user lisi

[root@node1 ~]# vim /etc/nginx/conf.d/data.conf

//省略部分内容

location /status {

stub_status;

access_log off;

allow 192.168.1.100;

deny all;

auth_basic "input your passwd:"; //用户登录描述信息

auth_basic_user_file /etc/nginx/.auth_conf; //用户验证文件路径

}

[root@node1 ~]# systemctl restart nginx

        根据下图所示输入用户密码后才能查看status内容,上面因为启用了IP限制,所以只能使用1.100主机访问。