一、要求
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