Nginx配置统计页面及访问控制(htpasswd和客户端IP)
生活随笔
收集整理的這篇文章主要介紹了
Nginx配置统计页面及访问控制(htpasswd和客户端IP)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 為何修改Nginx用戶與組?
- 啟用HTTP_STUB_STATUS狀態(tài)統(tǒng)計(jì)模塊
- 授權(quán)的訪問控制
- 基于客戶端IP實(shí)現(xiàn)訪問控制
為何修改Nginx用戶與組?
-
Nginx運(yùn)行時(shí)進(jìn)程需要有用戶與組的支持,以實(shí)現(xiàn)對(duì)網(wǎng)站文件讀取時(shí)進(jìn)行訪問控制
-
Nginx默認(rèn)使用 nobody用戶賬號(hào)與組賬號(hào),一般也要進(jìn)行修改
-
修改的方法
編譯安裝時(shí)指定用戶與組
修改配置文件時(shí)指定用戶與組
開啟和關(guān)閉Nginx
[root@localhost nginx]# cd /usr/local/nginx/ [root@localhost sbin]# nginx 開啟nginx [root@localhost sbin]# killall -s QUIT nginx 關(guān)閉進(jìn)程查看端口是否開啟
[root@localhost nginx]# netstat -ntap | grep 80 查看監(jiān)聽地址 [root@localhost nginx]# yum install -y elinks 檢查 [root@localhost sbin]# elinks http://localhost 查看是否開啟啟用HTTP_STUB_STATUS狀態(tài)統(tǒng)計(jì)模塊
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz [root@localhost ~]#cd /opt [root@localhost opt]# yum install -y gcc gcc-c++ pcre-devel zlib-devel [root@localhost nginx-1.12.2]# ./configure \ 啟動(dòng)腳本 --prefix=/usr/local/nginx \ 配置安裝路徑 --user=nginx \ 指定用戶 --group=nginx \ 指定組 --with-http_stub_status_module 開啟統(tǒng)計(jì)模塊 make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ [root@localhost nginx]# cd /usr/local/nginx/ [root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 創(chuàng)建軟鏈接讓系統(tǒng)可以識(shí)別修改nginx配置文件
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf ## 編輯配置文件添加如下內(nèi)容:location /status {stub_status on;access_log off;}開啟nginx服務(wù)
[root@localhost nginx-1.12.2]# nginx在瀏覽器中輸入地址進(jìn)行訪問統(tǒng)計(jì)頁面
http://192.168.136.88/status授權(quán)的訪問控制
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf 43 location / {44 auth_basic "secret";45 auth_basic_user_file /user/local/nginx/passwd.db;設(shè)置用戶名和密碼
[root@localhost nginx]# yum -y install httpd-tools [root@localhost nginx]# htpasswd -c /usr/local/nginx/passwd.db zhang New password: ## 輸入密碼 Re-type new password: ## 確認(rèn)密碼 Adding password for user zhang [root@localhost nginx]# cd /usr/local/nginx/ [root@localhost nginx]# nginx [root@localhost nginx]# iptables -F [root@localhost nginx]# setenforce 0 [root@localhost nginx]# systemctl stop firewalld3、修改認(rèn)證文件的屬主
[root@localhost nginx-1.12.2]# chown nginx /usr/local/nginx/passwd4、修改認(rèn)證文件權(quán)限
[root@localhost nginx-1.12.2]# chmod 400 /usr/local/nginx/passwd5、編輯nginx配置文件
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf## 在虛擬主機(jī)的location字段中添加如下兩行:auth_basic "secret"; auth_basic_user_file /usr/local/nginx/passwd;6、打開瀏覽器,清除緩存并重啟,輸入192.168.136.88進(jìn)行訪問
只要輸入之前用htpasswd工具生成的用戶名和密碼就可以登錄正常訪問頁面了
基于客戶端IP實(shí)現(xiàn)訪問控制
1、修改nginx配置文件,在虛擬主機(jī)中添加拒絕地址名單
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf ## 編輯配置文件server {server_name 192.168.50.134;location / {deny 192.168.136.100; ## 拒絕IP地址為192.168.50.136地址的主機(jī)訪問allow all; ## 允許其他所有主機(jī)訪問root /var/www/test;index index.html index.php;}}2、重啟nginx服務(wù)
[root@localhost conf]# systemctl restart nginx3、使用客戶機(jī)(IP:192.168.136.100)進(jìn)行訪問,打開瀏覽器,清空緩存后重啟,訪問192.168.50.134
從看出已經(jīng)不能訪問了
總結(jié)
以上是生活随笔為你收集整理的Nginx配置统计页面及访问控制(htpasswd和客户端IP)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存频率揭秘:为何频率越高,电脑性能越强
- 下一篇: 记忆力挑战者:我参加了高频内存游戏训练的