一、環境準備
1. 軟件版本
-
CentOS7.4
-
httpd-tools.x86_64??????? 2.4.6-67.el7.centos.6
-
nginx.x86_64????????????????? 1:1.12.2-2.el7
2. 軟件安裝
-
nginx的安裝需要epel源,沒有配置epel可以直接yum安裝一下:
[root@wind ~]# yum install epel-release.noarch -y [root@wind ~]# yum clean all && yum makecache
-
安裝nginx
[root@wind ~]# yum install nginx -y
-
httpd-tools?
CentOS7.4已經預裝了httpd服務,同時也默認安裝了此工具。沒有的話也可以yum裝一下。
3. 實驗環境檢查
-
因為系統里已經安裝了httpd服務,所以要提前檢查80端口是否被占用。
[root@wind ~]# ss -tnl |grep 80 LISTEN ? ? 0 ? ? ?128 ? ?192.168.7.138:80 ? ? ? ? ? ? ? ? ? ? ? *:*
[root@wind ~]# systemctl stop httpd [root@wind ~]# systemctl disable httpd Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
-
確定一下yum安裝的nginx配置文件路徑
[root@wind ~]# rpm -ql nginx |grep nginx.conf /etc/nginx/nginx.conf /etc/nginx/nginx.conf.default
-
先啟動nginx服務測試下是否正常
[root@wind ~]# systemctl start nginx [root@wind ~]# ss -tnl |grep 80 LISTEN ? ? 0 ? ? ?128 ? ? ? ? *:80 ? ? ? ? ? ? ? ? ? ? ? *:* ? ? ? ? ? ? ? ? ? LISTEN ? ? 0 ? ? ?128 ? ? ? ? :::80 ? ? ? ? ? ? ? ? ? ? :::* #默認同時監聽了ipv6 [root@wind ~]# curl -I -m 10 -o /dev/null -s -w %{http_code} http://localhost 200 ? ? ? ? ? ? ? ? ? ? ? ? ? #此方法常用于腳本中測試web服務 [root@wind ~]# curl -I -s -l http://localhost |sed -n '1p' HTTP/1.1 200 OK [root@wind ~]# curl -I -s -l http://localhost |grep 200 HTTP/1.1 200 OK
二、測試認證功能
1. 使用htpasswd配置認證訪問的用戶
-
添加認證用戶jerry,注意此用戶與系統內的用戶無關。
[root@wind ~]# htpasswd -c /etc/nginx/.ngxpasswd jerry New password: Re-type new password: Adding password for user jerry
2. 修改nginx配置文件啟用認證訪問。
location /secret/ { ? ? ? ? auth_basic "It's a secret!"; ? ? ? ? auth_basic_user_file /etc/nginx/.ngxpasswd; ? ? ? ? }
3. 在nginx目錄下添加測試頁面。并重載配置文件
[root@wind ~]# rpm -ql nginx |grep index /usr/share/nginx/html/index.html [root@wind ~]# mkdir /usr/share/nginx/html/secret [root@wind ~]# echo "I don't have any secret." > /usr/share/nginx/html/secret/main.html [root@wind ~]# /usr/sbin/nginx -s reload
4. 測試效果
[root@wind ~]# curl -u jerry http://192.168.7.138/secret/main.html Enter host password for user 'jerry': I don't have any secret.
三、添加狀態監測頁面并開啟認證
1.修改nginx配置文件
location = /status { ? ? ? allow 192.168.7.0/24; ? #為了安全可以設定ip范圍 ? ? ? deny all; ? ? ? auth_basic ""; #不想提示為何認證留空就好 ? ? ? auth_basic_user_file /etc/nginx/.ngxpasswd; ? ? ? stub_status; ? ? ? }
2. 查看效果
[root@wind ~]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@wind ~]# /usr/sbin/nginx -s reload [root@wind ~]# curl -u jerry http://192.168.7.138/status Enter host password for user 'jerry': Active connections: 1 server accepts handled requests 72 72 83 Reading: 0 Writing: 1 Waiting: 0
3. stub參數說明
?Active connections: 活動狀態的連接數;???????????????????
accepts:已經接受的客戶端請求的總數;???????????????????
handled:已經處理完成的客戶端請求的總數;???????????????????
requests:客戶端發來的總的請求數;???????????????????
Reading:處于讀取客戶端請求報文首部的連接的連接數;???????????????????
Writing:處于向客戶端發送響應報文過程中的連接數;???????????????????
Waiting:處于等待客戶端發出請求的空閑連接數;
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/101940