NGINX的配置二
1.1 Nginx的四層訪問控制
該功能需要基于模塊ngx_http_access_moudle實作
可以通過匹配客戶端源IP進行訪問的限制,
location /abc {
alias /data/nginx/html/pc;
index index.html;
#deny 192.168.33.1;
allow 192.168.33.0/24;
deny all;
# 允許小部分一般放前面
}
# 一般在防火墻上實作這個功能,
1.2 Nginx賬戶認證功能
htpasswd
htpasswd指令用來創建和更新用于基本認證的用戶認證密碼檔案,htpasswd指令必須對密碼檔案有讀寫權限,否則會回傳錯誤碼,
語法
htpasswd [ -c ] [ -m ] [ -D ] passwdfile username
htpasswd -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password
htpasswd -n [ -m | -d | -s | -p ] username
htpasswd -nb [ -m | -d | -s | -p ] username password
引數串列
| 選項 | 說明 |
|---|---|
| -b | 使用批處理方式,直接從命令列獲取密碼,不提示用戶輸入 |
| -c | 創建密碼檔案,如果檔案存在,那么內容被清空重寫 |
| -n | 將結果送到標準輸出 |
| -m | 使用MD5加密 |
| -s | 使用crypt()加密 |
| -p | 使用文本密碼 |
| -D | 從認證檔案中洗掉用戶記錄 |
[root@localhost@~]->yum install httpd-tools -y
# htpasswd 命令在此包中
#
# 創建密碼檔案及創建用戶密碼
[root@localhost@~]->htpasswd -cbm /apps/nginx/conf/.htpasswd user1 123123
Adding password for user user1
[root@localhost@~]->htpasswd -bm /apps/nginx/conf/.htpasswd user2 123123
Adding password for user user2
#
[root@localhost@~]->cat /apps/nginx/conf/.htpasswd
user1:$apr1$aHa5x2we$Z8F.j6ikPQAt7KiK5AHZ10
user2:$apr1$7tMlfpt6$ewI3e7hkjHQA8nJVZMu37/
#
# 在pc.conf新增location
#
location /login {
root /data/nginx/html;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
#
# 建立檔案目錄/data/nginx/html/login
#
[root@localhost@~]->mkdir /data/nginx/html/login
[root@localhost@~]->echo "YOU ARE HERE" > /data/nginx/html/login/index.html
#
# 測驗

在輸入user1 123123后進入該界面

同樣的這個功能適用內部訪問
1.3 自定義錯誤界面
# 在目錄 /apps/nginx/conf/nginx.conf中有錯誤界面的相關配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 嘗試自定義
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root html;
}
# 編輯界面
#
[root@localhost@conf]->vi ../html/50x.html
[root@localhost@conf]->nginx -s reload
#

在www.flamenca.net中

1.4 自定義訪問日志
訪問日志分為:
- access.log
- error.log
# 在pc.conf中定義
access_log /data/nginx/logs/www.flamenca.net_access.log;
error_log /data/nginx/logs/www.flamenca.net_error.log;
#
# 新建檔案夾
[root@localhost@conf]->mkdir /data/nginx/logs
[root@localhost@conf]->nginx -s reload
# 開始測驗
# access.log
[root@localhost@~]->tail -fv /data/nginx/logs/www.flamenca.net_access.log
==> /data/nginx/logs/www.flamenca.net_access.log <==
192.168.33.1 - user1 [12/Nov/2020:14:20:13 +0800] "GET /login/ HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36"
# error.log
[root@localhost@~]->tail -fv /data/nginx/logs/www.flamenca.net_error.log
==> /data/nginx/logs/www.flamenca.net_error.log <==
2020/11/12 14:21:32 [error] 6034#0: *20 "/data/nginx/html/flamenca/l1213n/index.html" is not found (2: No such file or directory), client: 192.168.33.1, server: www.flamenca.net, request: "GET /l1213n/ HTTP/1.1", host: "www.flamenca.net"
2020/11/12 14:21:36 [error] 6034#0: *20 "/data/nginx/html/flamenca/l12231n/index.html" is not found (2: No such file or directory), client: 192.168.33.1, server: www.flamenca.net, request: "GET /l12231n/ HTTP/1.1", host: "www.flamenca.net"
1.5 檢測檔案是否存在
try_flies 會按順序檢測檔案是否存在,如果找不到,會進行一個內部重定向,通過URI來指向,最后的URI必須存在,否則會回傳500錯誤;
location /hello {
root /data/nginx/html;
index index.html;
try_files $uri $uri/index.html $uri.html =666;
#try_files $uri $uri/index.html $uri.html /hello/index.html;
}
# try
# 新建檔案夾/data/nginx/html/hello
#
[root@localhost@conf]->mkdir /data/nginx/html/hello
#
[root@localhost@conf]->cat /data/nginx/html/hello/index.html
<h1> Defult Index Page </h1>
# 測驗訪問 www.flamenca.net/hello/xxx.html
####
# 測驗2
location /hello {
root /data/nginx/html;
index index.html;
#try_files $uri $uri/index.html $uri.html =666;
try_files $uri $uri/index.html $uri.html /hello/index.html;
}

測驗2


關鍵點1:按指定的file順序查找存在的檔案,并使用第一個找到的檔案進行請求處理
關鍵點2:查找路徑是按照給定的root或alias為根路徑來查找的
關鍵點3:如果給出的file都沒有匹配到,則重新請求最后一個引數給定的uri,就是新的location匹配
關鍵點4:如果是格式2,如果最后一個引數是 = 404 ,若給出的file都沒有匹配到,則最后回傳404的回應碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/216205.html
標籤:其他
