案例-搭建多个网站
可以全部都在nginx.conf中集中配置,但是不建议,因为会很乱,也不方便管理
建议通过在nginx.conf中增加子配置项的方式:
include /usr/local/nginx/conf/vhost/*.conf;
多个网站的配置内容都放在该目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| cd /usr/local/nginx/conf/vhost cat >> www.conf <<EOF server { listen 80; server_name www.it.com; location / { root html/www; <-这里没加/说明html是在nginx根目录下即/usr/local/nginx的html下 index index.html; } } EOF nginx -t
重新修改nginx.conf,将其中server的部分删除(因为它已经放在子配置文件中了) cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include /usr/local/nginx/conf/vhost/*.conf; } nginx -t
创建发布目录: mkdir /usr/local/nginx/html/www -p echo "it's www.it.com" >> /usr/local/nginx/html/www/index.html nginx -s reload 测试: curl localhost:80 welcome to www.it.com
添加第二个网站,不注释了 cat >> /usr/local/nginx/conf/vhost/wangsheng.conf << EOF server { listen 80; server_name www.wangsheng.com; location / { root html/wangsheng; index index.html; } } EOF mkdir /usr/local/nginx/html/wangsheng -p echo "welcome to www.wangsheng.com" > //usr/local/nginx/html/wangsheng/index.html nginx -t nginx -s reload 测试:另一台主机 echo "192.168.10.113 www.it.com" >> /etc/hosts echo "192.168.10.113 www.wangsheng.com" >> /etc/hosts curl www.it.com welcome to www.it.com curl www.wangsheng.com welcome to www.wangsheng.com
|
案例-nginx黑白名单
针对单个网站做的安全选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 对conf文件进行修改 vim vhost/wangsheng.conf server { listen 80; server_name www.wangsheng.com; location / { allow 192.168.10.102/32; deny all; root html/wangsheng; index index.html; } } 只允许192.168.10.102单个测试机访问 nginx -s reload
本机测试访问——报403 echo "192.168.10.113 www.wangsheng.com" >> /etc/hosts curl www.wangsheng.com <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.18.0</center> </body> </html>
测试机测试访问——成功 curl www.wangsheng.com welcome to www.wangsheng.com
|
案例-Nginx搭建http文件服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 还是使用上面例子中的wangsheng域名
使索引文件失效,不然它还是会显示index mv /usr/local/nginx/html/wangsheng/index.html /usr/local/nginx/html/wangsheng/index.html.bak
修改配置文件/usr/local/nginx/conf/vhost/wangsheng.conf server { listen 80; server_name www.wangsheng.com; location / { root html/wangsheng; autoindex on; <-开启站带你目录索引功能 charset utf-8; <-指定字符集,防止出现中文乱码 } } nginx -s reload nginx -t
此时再打开,发现打开了/usr/local/nginx/html/wangsheng 即www.wangsheng.com的发布目录
|

案例-网站加密
使用httpd-tools工具,httpd-tools 是一个包含多种实用工具的包,这些工具主要用于与 Apache HTTP 服务器交互或进行诊断
htpasswd是http基本认证。用于创建和更新存储用户名和密码的认证文件,通过 htpasswd,管理员可以添加或删除用户,以及更改用户的密码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| yum -y install httpd-tools mkdir /usr/local/nginx/passwd -p cd /usr/local/nginx/passwd htpasswd -c /usr/local/nginx/passwd/passwd sheng 为账户sheng设置密码,并将其放在/usr/local/nginx/passwd/passwd中
修改配置文件wangsheng.conf server { listen 80; server_name www.wangsheng.com; location / { root html/wangsheng; index index.html; auth_basic "输入密码:"; <-这是一个提示信息 auth_basic_user_file /usr/local/nginx/passwd/passwd; } } nginx -t nginx -s reload
|

案例-使用nginx自带的监控模块
http_stub_status_module模块提供了一个特殊的页面,该页面以纯文本格式显示Nginx的当前状态信息,包括活动连接数、接收和处理的请求数、读取和写入的字节数等
在与Prometheus、zabbix等监控软件集成时,也需要打开该模块。比如如果要使用Prometheus的nginx-Prometheus-exporter,就需要启用http_stub_status选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 启用方法: 编译时加入该模块 即 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \ --with-http_stub_status_module
使用其配置: 方法(1) cat /usr/local/nginx/conf/vhost/wangsheng.conf server { listen 80; server_name www.wangsheng.com; location / { root html/wangsheng; autoindex on; } location =/status { <-这个页面是自定义的,随便写 stub_status; } }
方法(2) 定义一个新的配置文件与域名,将该域名作为页面 cat >> /usr/local/nginx/conf/vhost/s.wangsheng.conf << EOF server { listen 80; server_name www.s.wangsheng.com; stub_status; } EOF
nginx -t nginx -s reload
方法(1)测试 curl www.wangsheng.com/status Active connections: 1 server accepts handled requests 34 34 68 Reading: 0 Writing: 1 Waiting: 0
方法(2)测试 curl www.s.wangsheng.com
|

监控参数说明
1 2 3 4 5 6 7 8 9 10
| Active connections: 1 <—— 正在连接的用户量,连接数 server accepts 37 <—— 接受连接数 handled 37 <—— 处理连接数 requests 71 <—— 请求连接数 Reading: 0 <—— 请求报文数、没人在看 Writing: 1 <—— 相应报文,现在就我自己在看 Waiting: 0 <—— 有多少人在等待队列中
一般看连接数、请求报文、等待队列等
|