国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx服務器中為網(wǎng)站或目錄添加認證密碼的配置詳解

2024-08-30 12:26:19
字體:
供稿:網(wǎng)友

nginx可以為網(wǎng)站或目錄甚至特定的文件設置密碼認證。密碼必須是crypt加密的。可以用apache的htpasswd來創(chuàng)建密碼。

格式為:

htpasswd -b -c site_pass username password

site_pass為密碼文件。放在同nginx配置文件同一目錄下,當然你也可以放在其它目錄下,那在nginx的配置文件中就要寫明絕對地址或相對當前目錄的地址。

如果你輸入htpasswd命令提示沒有找到命令時,你需要安裝httpd。如果是centos可以執(zhí)行如下來安裝,

yum install httpd

如果你不想安裝httpd的話,可以使用perl腳本來實現(xiàn)(代碼如下:)

#! /usr/bin/perl -w  #filename: add_ftp_user.pl  use strict;  #  print "#example: user:passwd/n";  while (<STDIN>) {    exit if ($_ =~/^/n/);    chomp;    (my $user, my $pass) = split /:/, $_, 2;    my $crypt = crypt $pass, '$1$' . gensalt(8);    print "$user:$crypt/n";  }  sub gensalt {    my $count = shift;    my @salt = ('.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z');    my $s;    $s .= $salt[rand @salt] for (1 .. $count);    return $s;  } 
為腳本賦予可執(zhí)行權限:
chmod o+x add_user.pl

腳本使用方法:

./add_user.pluser:password

把生成的用戶名密碼粘貼到/usr/local/nginx/conf/vhost/nginx_passwd文件中即可

如果是為了給網(wǎng)站加上認證,可以直接將認證語句寫在nginx的配置server段中。

如果是為了給目錄加上認證,就需要寫成目錄形式了。同時,還要在目錄中加上php的執(zhí)行,否則php就會被下載而不執(zhí)行了。

例如:基于整個網(wǎng)站的認證,auth_basic在php解釋之前。

server  {    listen 80;    server_name m.survivalescaperooms.com Vevb.com;    root /www/Vevb.com;    index index.html index.htm index.php;    auth_basic "input you user name and password";    auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;    location ~ .php$    {      fastcgi_pass 127.0.0.1:9000;      fastcgi_index index.php;      include fastcgi_params;    }    location ~ //.ht    {      deny all;    }    access_log /logs/Vevb.com_access.log main;  } 

針對目錄的認證,在一個單獨的location中,并且在該location中嵌套一個解釋php的location,否則php文件不會執(zhí)行并且會被下載。auth_basic在嵌套的location之后。

server  {    listen 80;    server_name m.survivalescaperooms.com Vevb.com;    root /www/Vevb.com;    index index.html index.htm index.php;    location ~ ^/admin/.*    {    location ~ /.php$    {      fastcgi_pass 127.0.0.1:9000;      fastcgi_index index.php;      include fastcgi_params;    }    auth_basic "auth";    auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;    }    location ~ .php$    {      fastcgi_pass 127.0.0.1:9000;      fastcgi_index index.php;      include fastcgi_params;    }    location ~ //.ht    {      deny all;    }    access_log /logs/Vevb.com_access.log main;  }             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汤阴县| 咸丰县| 故城县| 湖州市| 兴安盟| 隆化县| 镇雄县| 钟祥市| 观塘区| 什邡市| 稷山县| 浠水县| 鹤峰县| 江津市| 宁津县| 西平县| 阿合奇县| 海晏县| 巴青县| 望都县| 襄汾县| 东海县| 丹阳市| 麟游县| 江阴市| 拉孜县| 白山市| 方城县| 仪陇县| 沅陵县| 喀喇| 拉萨市| 西昌市| 罗源县| 仙居县| 沧州市| 盘锦市| 常山县| 乐陵市| 金坛市| 玉龙|