Web 基础——Nginx(二)
一、Nginx 基础配置
1.Nginx 配置文件
2.Nginx 日志配置
3.Nginx 下载站点
4.Nginx 访问控制
1)第一种方式
2)第二种方式
5.Nginx 虚拟主机
一、Nginx 基础配置
1.Nginx 配置文件
- Nginx 主配置文件是一个纯文本类型的文件,整个配置文件是以区块的形式组成的。一般每个区块以一对 {} 大括号。
// 全局配置: user # 配置 Nginx 服务的系统使⽤⽤户 worker_processes # ⼯作进程. 配置和 CPU 个数保持⼀致 error_log # 错误⽇志. 后⾯接⼊的是路径 pid # Nginx 服务启动时的 PID // Events 事件模块: events { # 事件模块 worker_connections # 每个 Worker 进程⽀持的最⼤连接数 use # 内核模型. select | poll | epoll // HTTP 配置: http { ... server { # 第一个虚拟主机 listen 80; # 监听端⼝. 默认 80 server_name localhost; # 提供服务的域名或主机名 'location' / { # 控制⽹站访问路径 root /usr/share/nginx/html; # 存放⽹站路径 index index.html index.htm; # 默认访问首页⽂件 } error_page 500 502 503 504 /50x.html; # 指定错误代码. 统⼀定义错误⻚⾯. 错误代码重定向到新的 Locaiton 'location' = /50x.html { root html; } } ... server { # 第⼆个虚拟主机配置 ... }
2.Nginx 日志配置
开启 Nginx 日志配置只需要将配置文件中以下内容将 # 去掉即可。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 13 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 14 # '$status $body_bytes_sent "$http_referer" ' 15 # '"$http_user_agent" "$http_x_forwarded_for"'; 16 #access_log logs/access.log main;
Nginx 日志变量:
- $remote_addr:表示客户端地址。
- $remote_user:http 客户端请求 Nginx 认证用户名。
- $time_local:Nginx 的本地时间。
- $request:Request 请求行,GET 等方法、http 协议版本。
- $status:respose 返回的状态码。
- $body_bytes_sent:从服务端响应给客户端 body 信息大小。
- $http_referer:http 上一级页面,防盗链、用户行为分析。
- $http_user_agent:http 头部信息,客户端访问设备。
- $http_x_forwarded_for:http 请求携带的 http。
3.Nginx 下载站点
- Nginx 默认是不允许列出整个目录浏览下载。
语法格式:autoindex on | off 可在 http | server | location 区域中添加
配置目录浏览功能
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location / { root html; index index.html index.htm; autoindex on; # 开启目录浏览 autoindex_exact_size off; # 修改为 off 即可显示出文件的大概大小. 单位是 KB | MB | GB [root@localhost ~]# systemctl restart nginx # 重启 Nginx 服务 [root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# ls index.html [root@localhost html]# rm -rf index.html # 删除默认网页 [root@localhost html]# mkdir zhangsan [root@localhost html]# mv /root/nginx-1.18.0.tar.gz . [root@localhost html]# ls nginx-1.18.0.tar.gz zhangsan
验证:
4.Nginx 访问控制
1)第一种方式
- limit_conn_module:限制 Nginx 服务器所承载的单个客户端单个 IP 地址在单一时间所发起的连接数量(防爬虫)
- limit_req_module:限制 Nginx 服务器所承载的单个客户端单个 IP 地址在单一时间所发起的请求数量。
查看 Nginx 默认安装的模块,在解压 Nginx 源代码目录 下运行以下命令
[root@localhost ~]# cd /usr/src/nginx-1.18.0/ [root@localhost nginx-1.18.0]# cat auto/options | grep 'YES' # 查看 Nginx 默认安装的所有模块
查看是否有 limit_conn 和 limit_req 这两个模块
[root@localhost nginx-1.18.0]# cat auto/options | grep 'HTTP_LIMIT_CONN=YES' HTTP_LIMIT_CONN=YES [root@localhost nginx-1.18.0]# cat auto/options | grep 'HTTP_LIMIT_REQ=YES' HTTP_LIMIT_REQ=YES
配置 Nginx 连接限制
语法格式: limit_conn_zone key zone=name:size # 需要在 http 区域中配置 limit_conn zone number # 可以在 http | server | location 区域中配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf http { ... limit_conn_zone $binary_remote_addr zone=conn_zone:10m; server { ... location / { ... limit_conn conn_zone 1; # 同一时刻只允许一个客户端 IP 地址 [root@localhost ~]# systemctl restart nginx
验证:
[root@localhost ~]# yum -y install httpd-tools # 安装 AB 压力测试工具 [root@localhost ~]# echo "<h1>Hello</h1>" > /usr/local/nginx/html/index.html # 编写测试页面 [root@localhost ~]# ab -n 200 -c 20 http://127.0.0.1/
配置 Nginx 请求限制
语法格式: limit_req_zone key zone=name:size rate=rate # 需要在 http 区域中配置 limit_req zone number [burst=number] [nodelay] # 可以在 http | server | location 区域中配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf http { ... limit_req_zone $binary_remote_addr zone=req_zone:10m rate=30r/m; # rate 限制速率. 限制一秒钟最多一个 IP 请求 server { ... location / { ... limit_req zone=req_zone; [root@localhost ~]# systemctl restart nginx
验证:
[root@localhost ~]# ab -n 200 -c 20 http://127.0.0.1/
2)第二种方式
- 基于 IP 的访问控制 http_access_module
- 基于用户名登录认证 http_auth_basic_module
配置基于 IP 的访问控制
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location / { ... deny 192.168.1.250; # 拒绝单个IP. 可以在 http | server | location 区域中配置 allow all; # 允许所有 [root@localhost ~]# systemctl restart nginx # 重启 Ngix 服务
验证:
基于用户登录认证
[root@localhost ~]# htpasswd -c /usr/local/nginx/conf/auth_conf zhangsan # 创建测试用户 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location / { ... auth_basic "Auth access Blog Input your Passwd ~"; # 提示语. 可以在 在 http | server | location 区域中配置 auth_basic_user_file /usr/local/nginx/conf/auth_conf; # 限制用户文件. 可以在 http | server | location 区域配置 [root@localhost ~]# systemctl restart nginx # 重启 Ngix 服务
验证:
5.Nginx 虚拟主机
所谓虚拟主机,就是在 Web 服务器里是一个独立的网站站点,这个站点对应独立的域名(也有可能是 IP 或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。
1)创建 Web 站点目录
[root@localhost ~]# mkdir /usr/local/nginx/html/Coco/ [root@localhost ~]# mkdir /usr/local/nginx/html/Zozo/ [root@localhost ~]# echo "Hello Coco" > /usr/local/nginx/html/Coco/index.html [root@localhost ~]# echo "Hello Zozo" > /usr/local/nginx/html/Zozo/index.html
2)配置基于域名的虚拟主机
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf http { ... server { listen 80; server_name www.Coco.com; location / { root html/Coco; index index.html index.htm; } } server { listen 80; server_name www.Zozo.com; location / { root html/Zozo; index index.html index.htm; } } } [root@localhost ~]# systemctl restart nginx #重启 Nginx 服务 [root@localhost ~]# cat <<END >> /etc/hosts #配置 Hosts 文件解析 192.168.1.1 www.Coco.com 192.168.1.1 www.Zozo.com END
验证:
[root@localhost ~]# curl http://www.Coco.com [root@localhost ~]# curl http://www.Zozo.com
配置虚拟主机别名
- 所谓虚拟主机别名,就是虚拟主机设置除了主域名以外的一个域名,实现用户访问的多个域名对应同一个。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf http { ... server { listen 80; server_name www.Coco.com Coco.com; location / { root html/Coco; index index.html index.htm; } } server { listen 80; server_name www.Zozo.com; location / { root html/Zozo; index index.html index.htm; } } } [root@localhost ~]# cat <<END >> /etc/hosts 192.168.1.1 Coco.com END [root@localhost ~]# curl http://Coco.com [root@localhost ~]# curl http://www.Coco.com