Skip to content

Commit b3e7999

Browse files
committed
add auth_basic tutorial
1 parent 8b55165 commit b3e7999

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* [Youtube Tutorial - 透過 Nginx Log 分析 PV UV](https://youtu.be/mUyDVVX6OD4) - [文章快速連結](https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-tutorial#%E9%80%8F%E9%81%8E-nginx-log-%E5%88%86%E6%9E%90-pv-uv)
1818

19+
* [Youtube Tutorial - NGINX 教學 - auth basic](https://youtu.be/zWODI3YHb2Y) - [文章快速連結](https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-tutorial#%E8%A8%AD%E5%AE%9A-auth_basic)
20+
1921
## 簡介
2022

2123
### [Docker](https://www.docker.com/)
@@ -461,6 +463,8 @@ Nginx 負責靜態內容( html css 圖片...... ),uWSGI 負責 Python 的
461463

462464
## 即時監控 Nginx 網頁狀態
463465

466+
* [Youtube Tutorial - NGINX 教學 - auth basic](https://youtu.be/zWODI3YHb2Y)
467+
464468
打開 stub_status 模組, 請參考 [my_nginx.conf](https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-tutorial/blob/master/nginx/my_nginx.conf),
465469

466470
```conf
@@ -488,6 +492,59 @@ location /nginx/status {
488492

489493
![](https://i.imgur.com/iFZF8Yh.png)
490494

495+
### 設定 auth_basic
496+
497+
主要加入 `auth_basic``auth_basic_user_file`
498+
499+
文件可參考 [Module ngx_http_auth_basic_module](http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html)
500+
501+
```conf
502+
location /nginx/status {
503+
# 啟用 stub_status
504+
stub_status on;
505+
506+
# 關閉/啟用 log
507+
# access_log /usr/local/nginx/logs/status.log;
508+
access_log off;
509+
510+
auth_basic "NginxStatus";
511+
auth_basic_user_file /my_htpasswd/htpasswd;
512+
513+
# 限制可存取的 IP
514+
# allow 127.0.0.1;
515+
# deny all;
516+
}
517+
```
518+
519+
建立一個 htpasswd 檔案, 檔案內容如下
520+
521+
```text
522+
# comment
523+
name1:password1
524+
name2:password2:comment
525+
name3:password3
526+
```
527+
528+
注意, 密碼需要使用 openssl 產生,
529+
530+
例如, 我的密碼是 123
531+
532+
```cmd
533+
❯ openssl passwd 123
534+
8uxCGNPhjFqiw
535+
```
536+
537+
然後 htpasswd 檔案, 填入
538+
539+
```text
540+
# comment
541+
user1:8uxCGNPhjFqiw:123
542+
```
543+
544+
重新啟動 nginx, 就會發現要輸入帳密才能觀看,
545+
546+
![](https://i.imgur.com/LKFcUGz.png)
547+
491548
## `hosts` 設定檔 以及 查詢內網 ip
492549

493550
修改 `hosts` 設定檔

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
volumes:
2323
- api_data:/docker_api
2424
- ./log:/var/log/nginx
25+
- ./my_htpasswd:/my_htpasswd
2526
depends_on:
2627
- api
2728

my_htpasswd/htpasswd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
user1:8uxCGNPhjFqiw:123

nginx/my_nginx.conf

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ server {
3535
access_log off;
3636

3737
auth_basic "NginxStatus";
38+
auth_basic_user_file /my_htpasswd/htpasswd;
3839

3940
# 限制可存取的 IP
4041
# allow 127.0.0.1;

0 commit comments

Comments
 (0)