1. 程式人生 > >詳解Nginx兩種方式實現訪問控制

詳解Nginx兩種方式實現訪問控制

vfk 用戶 用戶輸入 bfd pcre 效果 sys ado 密碼認證

簡介

基於用戶的訪問控制就是對網頁目錄進行認證配置,用戶輸入用戶名密碼之後才能訪問網頁
基於IP的訪問控制即使可以通過配置基於ip的訪問控制,達到讓某些ip能夠訪問,限制哪些ip不能訪問的效果

實驗環境

  • 系統環境:CentOS7.4
  • 服務器IP地址:192.168.100.71
  • 客戶端IP地址:192.168.100.72
  • yum掛載目錄:/mnt/sr0
  • 相關源碼包下載地址:百度雲下載 ??密碼:uhzm

搭建步驟

一、準備工作

1、關閉防火墻及selinux

[root@localhost ~]# systemctl stop firewalld.service #關閉防火墻
[root@localhost ~]# systemctl disable firewalld.service #隨開機自動關閉

[root@Init5 ~]# vim /etc/sysconfig/selinux
技術分享圖片

[root@localhost ~]# reboot #重啟Linux生效

二、搭建Nginx服務

1、安裝相應的工具包以及C語言編譯器

[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++

2、創建Nginx進程用戶

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

3、配置、編譯及編譯安裝Nginx源碼包

[root@localhost ~]# tar xvf nginx-1.12.0.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

參數解析:

prefix #指定安裝位置
user=nginx #指定nginx系統用戶
group=nginx #指定組

[root@localhost nginx-1.12.0]# make && make install

4、優化路徑

[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

三、設置Nginx運行控制

1、檢測語法

[root@localhost ~]# nginx -t #測試配置是否有語法錯誤
技術分享圖片

2、啟動Nginx服務

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf #修改主配置文件
技術分享圖片

[root@localhost ~]# nginx #啟動Nginx
[root@localhost ~]# netstat -anpt | grep ":80" #檢測服務是否啟動
[root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid #查看pid
技術分享圖片

3、停止Nginx服務

[root@localhost ~]# kill -1 3809 #平滑重啟Nginx服務。相當於killall -s HUP nginx;nginx -s reload
[root@localhost ~]# kill -3 1514 #關閉Nginx服務。相當於killall -s QUIT nginx;nginx -s quit
[root@localhost ~]# nginx #重啟服務
[root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid #查看pid號
技術分享圖片

4、編輯Nginx服務腳本

[root@localhost ~]# vim /lib/systemd/system/nginx.service

[Unit]

Description=Nginx Server Control Script #說明
After=network.target #描述服務類別

[Service]

Type=forking #後臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx #啟動服務
ExecReload=/usr/bin/kill -s HUP $PIDFile #重載服務
ExecStop=/usr/bin/kill -s QUIT $PIDFile #停止服務

[Install]

WanteBy=multi-user.target

[root@localhost ~]# systemctl daemon-reload #重新加載服務單元
[root@localhost ~]# systemctl enable nginx.service #設置開機自啟動

[root@localhost ~]# systemctl stop nginx.service#停止服務
[root@localhost ~]# systemctl start nginx.service#開啟服務
[root@localhost ~]# systemctl reload nginx.service#平滑重啟服務
[root@localhost ~]# systemctl restart nginx.service#重啟服務

四、設置基於授權的訪問控制

1、生成用戶密碼認證文件

[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db tom

htpasswd在最開始編譯安裝的時候已經安裝好htpasswd,用來創建授權用戶數據文件,並維護其中的用戶賬號;
c代表create創建密碼認證文件;
tom是認證文件中的用戶名

[root@localhost ~]# cat /usr/local/nginx/passwd.db #查看密碼文件
技術分享圖片

2、修改密碼認證文件權限以及所有者

[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db

3、修改主配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
技術分享圖片

4、重啟Nginx服務

[root@localhost ~]# nginx -t
技術分享圖片

[root@localhost ~]# systemctl restart nginx.service #重啟服務

5、訪問測試

技術分享圖片

技術分享圖片

五、設置基於客戶端的訪問控制

1、修改主配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
技術分享圖片

2、重啟Nginx服務

[root@localhost ~]# nginx -t
技術分享圖片

[root@localhost ~]# systemctl restart nginx.service

3、訪問測試

技術分享圖片

詳解Nginx兩種方式實現訪問控制