一、要求
1.通过https://ip 可以访问到网站首页
2.通过 https://ip/private/ 实现用户访问控制,仅允许已经添加的 tom,jerry 能够访问到 private 子路径的界面
3.通过 https://ip/vrit/ 实现能够访问到将系统 /nginx/virt 目录下的网页文件(/nginx/virt/index.html)
二、实验
1.通过https://ip 可以访问到网站首页
① 首先关闭防火墙以及 selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
② 启动 nginx 程序发现显示未找到 nginx.service,在下载 nginx 软件包之前需先进行编辑 yum 源配置文件以及挂载磁盘操作
[root@localhost ~]# vim /etc/yum.repos.d/base.repo
[BaseOS]
name=BaseOS
baseurl=file:///mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///mnt/AppStream
gpgcheck=0
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
③ 下载 nginx 软件包并启动
[root@localhost ~]# dnf install nginx -y
[root@localhost ~]# systemctl start nginx
④ 编辑 nginx 配置文件,添加 server 模块
[root@localhost ~]# vim /etc/nginx/conf.d/ip.conf
⑤ 按照配置文件创建资源文件
[root@localhost ~]# mkdir -pv /www
mkdir: 已创建目录 '/www'
[root@localhost ~]# echo this is www > /www/index.html # 写入内容
⑥ 使用 openssl
工具创建一个新的 RSA 私钥,并生成一个基于该私钥的自签名 X509 证书,用于加密网络通信(通常用于网站启用 HTTPS 协议)。以下信息可随意填写,注意“陕西为shaanxi”
[root@localhost ~]# openssl req -newkey rsa:4096 -nodes -keyout /etc/pki/tls/private/openlab.key -x509 -days 365 -out /etc/pki/tls/certs/openlab.crt
⑦ 重启服务,提供信息响应(加载新的配置)
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl https://192.168.96.142 -k # 加k表示在进行 HTTPS 请求时,跳过对服务器 SSL/TLS 证书的验证
2.通过 https://ip/private/ 实现用户访问控制,仅允许已经添加的 tom,jerry 能够访问到 private 子路径的界面
① 修改 nginx 配置,添加 location 模块
[root@localhost ~]# vim /etc/nginx/conf.d/ip.conf
② 按照配置文件创建资源文件
[root@localhost ~]# mkdir -pv /private
mkdir: 已创建目录 '/private'
[root@localhost ~]# echo this is private > /private/index.html
③ 先进行磁盘挂载,在进行下载操作
[root@localhost ~]# mount /dev/sr0 /mnt # 磁盘挂载
[root@localhost ~]# yum provides htpasswd # 查询htpasswd软件包提供了指定的文件
[root@localhost ~]# yum install httpd-tools # 下载httpd-tools软件包
④ 更新 HTTP 基本认证的用户密码文件
[root@localhost ~]# htpasswd -c /etc/nginx/users tom
New password:
Re-type new password:
Adding password for user tom
[root@localhost ~]# htpasswd /etc/nginx/users jerry
New password:
Re-type new password:
Adding password for user jerry
⑤ 重启程序并测试结果
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl https://192.168.96.142/private/ -u tom -k
[root@localhost ~]# curl https://192.168.96.142/private/ -u jerry -k
3.通过 https://ip/vrit/ 实现能够访问到将系统 /nginx/virt 目录下的网页文件(/nginx/virt/index.html)
① 编辑 nginx 配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/ip.conf
② 按照配置文件创建资源文件
[root@localhost ~]# mkdir -pv /nginx/virt
mkdir: 已创建目录 '/nginx/virt'
[root@localhost ~]# echo this is virt > /nginx/virt/index.html
③ 重启程序并测试结果
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl https://192.168.96.142/virt/ -k