安装nginx及部分常用模块
(1).可以配置本地yum源
[root@nginx ~]# 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
(2).可以安装epel源进行安装nginx
[root@nginx ~]# yum -y install epel-release
nginx新版本的配置文件
全局配置文件:/etc/nginx/nginx.conf
虚拟主机配置:/etc/nginx/conf.d/*.conf
我们在配置虚拟的配置文件时可通过自己重新创建或者在默认的default.conf文件中进行修改,一般用自己新建的。
在这里先将默认的default移到其他目录下
复制网页代码到/www目录下
[root@nginx ~]# mv /etc/nginx/conf.d/default.conf /tmp/default.conf
然后创建自己的配置文件,并且在其中添加仅供可以访问nginx的基本模块
[root@nginx ~]# vim /etc/nginx/conf.d/www.conf
server {
listen 80; ##端口默认http80
server_name www.yundong.com; #本地dns域名解析
location / {
root /www; #网站根目录
index index.html index.htm; #网页文件格式
}
}
启动服务
[root@nginx ~]# systemctl restart nginx
可通过client本地/etc/hosts中添加即可通过域名访问
[root@client ~]# vim /etc/hosts
192.168.10.10 www.yundong.com
nginx目录索引(autoindex自动索引模块),nginx默认不起用目录索引,更不允许列出网站目录提供下载。
Syntax: autoindex on | off; 索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录
举例:在www网站下,创建download下载目录,索引显示
[root@nginx ~]# mkdir /www/download
[root@nginx ~]# echo aaaa > /www/download/aaa.txt
[root@nginx ~]# vim /etc/nginx/conf.d/www.conf
在server字段中添加以下
location /download {
root /www;
autoindex on; #启用索引显示
charset utf-8,gbk; #字符编码为中文
autoindex_exact_size on; #显示文件大小
autoindex_localtime on; #显示文件创建时间
}
重启服务在client中测试
http://www.yundong.com/download
会下载aaa.txt这个文件
nginx状态监控(status模块)
Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context: server, location 场景:
举例:针对www网站,启用状态化追踪
在server字段中添加以下
[root@nginx ~]# vim /etc/nginx/conf.d/www.conf
location /status {
stub_status; #启用状态化追踪
access_log off; #关闭status的日志记录
}
重启服务
客户端访问http://www.yundong.com/status
会显示
active connections _活跃的连接数量
server accepts handled requests —— 总共处理的连接数、成功创建的握手次数、总共处理的请求次数
handled,requests——总共处理了几次请求。
nginx基于ip的访问控制(access模块)
例子:仅允许内部网段或访问status
vim /etc/nginx/conf.d/www.conf
修改location
location /status {
stub_status;
access_log off;
allow 192.168.1.0/24; #仅允许1.0网段访问
deny all; #拒绝其他所有
}
nginx基于用户的访问控制(auth模块)
例子:设置访问/status,用户密码验证
先安装httpd工具,然后创建用户,修改配置文件
[root@nginx ~]# yum -y install httpd-tools
[root@nginx ~]# htpasswd -b -c /etc/nginx/.auth_conf webadmin 123456
#创建用户webadmin 密码123456 用户验证文件路径为 /etc/nginx/.auth_conf
[root@nginx ~]# vim /etc/nginx/conf.d/www.conf
location /status {
stub_status;
access_log off;
auth_basic "input your passwd:"; #用户验证启用描述
auth_basic_user_file /etc/nginx/.auth_conf; #用户验证文件路径
}
重启服务通过测试发现在进入网页时会弹出输入用户名密码的框,输入刚才创建的用户名和密码后即可访问网站
limit_conn_module 连接频率限制
例子:限制ip,限制速率
再配置文件中添加
http {
limit_conn_zone $binary_remote_addr zone=addr:10m; #创建zone区域名为addr,大小10m,保存客户端的二进制ip
server {
location /download/ {
limit_conn addr 1; #一个ip同一时间点只允许建立一个连接
}
}
}
limit_req_module 请求频率限制
再配置文件中添加
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
server {
location /download {
limit_req zone=one burst=5; #调用请求速率区域,另外设置额外突发5次
}
}
}
等等还有好多模块,可通过官方文档进行查询
https://nginx.org/en/docs/