1. 程式人生 > >利用nginx新增賬號密碼驗證

利用nginx新增賬號密碼驗證

 1 server {
 2     listen              80;
 3     server_name         xxx.com;
 4 
 5     location / {
 6         proxy_pass  http://10.10.10.10:5601;
 7         proxy_redirect     off;
 8         proxy_set_header   Host             $host;
 9         proxy_set_header   X-Real-IP        $remote_addr;
10 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 11 #allow 124.90.53.17; 12 auth_basic "The Kibana Monitor Center"; 13 auth_basic_user_file /usr/local/.passwd; 14 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
15 proxy_max_temp_file_size 0; 16 proxy_connect_timeout 60; 17 proxy_send_timeout 900; 18 proxy_read_timeout 900; 19 proxy_buffer_size 34k; 20 proxy_buffers 4 32k; 21 proxy_busy_buffers_size 64k;
22 proxy_temp_file_write_size 64k; 23 } 24 }

 

1、在location /下新增

auth_basic "The Kibana Monitor Center";
auth_basic_user_file  /usr/local/.passwd;

2、登陸http://tool.oschina.net/htpasswd 輸入使用者名稱和密碼,線上生成加密密碼
3、將生成的密碼寫入/usr/local/.passwd檔案中
echo "wuyun:$apr1$TrU5vUSC$RBuz3xG67Mr2pnAV596N5" >/usr/local/.passwd

此時重啟nginx,再登入訪問域名時就需要輸入賬號密碼了。不輸入密碼會報401 Authorization Required錯誤