溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nginx,apache的alias和認(rèn)證功能實(shí)例分析

發(fā)布時(shí)間:2022-05-06 10:42:00 來源:億速云 閱讀:233 作者:zzz 欄目:大數(shù)據(jù)

這篇文章主要介紹了nginx,apache的alias和認(rèn)證功能實(shí)例分析的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇nginx,apache的alias和認(rèn)證功能實(shí)例分析文章都會(huì)有所收獲,下面我們一起來看看吧。

首先看下看下apache 別名 怎么配置的:

復(fù)制代碼 代碼如下:

<virtualhost *:80>
documentroot /www/jb51.net/www 這是虛擬主機(jī)的根目錄吧,但是phpmyadmin 不在這個(gè)目錄下,想訪問。
servername www.jb51.net
serveralias jb51.net
alias /sdb "/www/public/phpmyadmin/" 就需要 別名功能,://www.jb51.net/sdb 這樣就安全多了。
<directory "/www/public/phpmyadmin/">
options indexes followsymlinks
allowoverride none
order allow,deny
allow from all
</directory>
</virtualhost>

一 .apache認(rèn)證

認(rèn)證的類型:basic
digest摘要
認(rèn)證方法:a、容器認(rèn)證: ……
b、隱藏文件認(rèn)證創(chuàng)建.htaccess文件
方法一、容器認(rèn)證
a、 進(jìn)入配置文件 vi /etc/httpd/conf/httpd.conf
b、 配置:大約在531行附近 配置如下:

allowoverride none ##不允許通過隱藏認(rèn)證,即通過容器認(rèn)證
authtype basic ##認(rèn)證類型為basic
authname “ajian” ##認(rèn)證名字為ajian
authuserfile /var/www/passwd/pass ##pass 為認(rèn)證密碼文件,指定密碼文件存放的位置。
require valid-user ##有效用戶(注意大小寫,因?yàn)閣ord的原因有些大小寫有變化)
c、 創(chuàng)建目錄 mkdir -p /var/www/passwd
進(jìn)入目錄 cd /var/www/passwd
d、創(chuàng)建apache用戶 htpasswd -c pass ajian ##pass 為密碼文件ajian為用戶
更改 把pass文件的使用權(quán)給apache: chown apache.apache pass
附:再在pass文件中添加一個(gè)用戶:htpasswd pass tt ##添加一個(gè)tt的用戶到pass文件中
e、重啟服務(wù)并測(cè)試
方法二、通過隱藏認(rèn)證
和上面差不多 不過配置不一樣
httpd主配置文件

allowoverride authconfig
創(chuàng)建隱藏文件并放到要通過認(rèn)證的目錄
eg: vi /var/www/html/mrtg
authtype basic
authname “ajian”
authuserfile /var/www/passwd/pass
require valid-user

下面是例子

nginx,apache的alias和認(rèn)證功能實(shí)例分析

nginx,apache的alias和認(rèn)證功能實(shí)例分析

nginx,apache的alias和認(rèn)證功能實(shí)例分析

二、nginx 登錄認(rèn)證

nginx 的 http auth basic 的密碼是用 crypt(3) 加密的。用 apache 的 htpasswd 可以生成密碼文件。
沒有 apache 自行安裝。我安裝的是 apache2,/usr/local/apach2。
cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回車輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。
vi nginx.conf cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回車輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。 vi nginx.conf 在 nginx.conf 文件中加入授權(quán)聲明。這里要注意 nginx 0.6.7 開始,auth_basic_user_file 的相對(duì)目錄是 nginx_home/conf,以前版本的相對(duì)目錄是 nginx_home。

復(fù)制代碼 代碼如下:


server {
listen 80;
server_name tuan.xywy.com;
root /www/tuangou;
index index.html index.htm index.php;
autoindex on;
auth_basic "input you user name and password";
auth_basic_user_file htpasswd.file;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_filename /www/tuangou$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.php;
error_page 403 /404.php;

access_log /logs/tuan_access.log main;
}



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

復(fù)制代碼 代碼如下:


server {
listen 80;
server_name tuan.xywy.com;
root /www/tuangou;
index index.html index.htm index.php;
autoindex on;
location ~ ^/admin/.* {
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_filename /www/tuangou$fastcgi_script_name;
include fastcgi_params;
}
root /www/tuangou/ ;
auth_basic "auth";
auth_basic_user_file htpasswd.file;
}

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}

access_log /logs/tuan_access.log main;
}



三.nginx alias功能配置自動(dòng)列目錄

復(fù)制代碼 代碼如下:


server {

listen www.jb51.net:88;

server_name www.jb51.net;

autoindex on; //開啟列目錄功能。

# charset gbk;
location /club { 訪問的名字//www.jb51.net:88/club
alias /www/clublog/club.xywy.com/; 這是服務(wù)器上存放日志的地方
} 這段意思 訪問www.jb51.net:88/club 就看到club目錄的東東了。
location /{
root /www/access;
這段location 也可以沒有 www.jb51.net:88 出來的是默認(rèn)nxing 頁面
# index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}



上面nginx配置意思就是: 訪問http://hou.xywy.com/:88認(rèn)證進(jìn)去是默認(rèn)訪問服務(wù)器上/www/access/里面的目錄,認(rèn)證進(jìn)去后url=http://hou.xywy.com:88/club 就出來 /www/clublog/club.xywy.com/ 里面的目錄的內(nèi)容了。,可能很繞,仔細(xì)分析就好了。

root 和 alias 的區(qū)別。
最基本的區(qū)別:alias指定的目錄是準(zhǔn)確的,root是指定目錄的上級(jí)目錄,并且該上級(jí)目錄要含有l(wèi)ocation指定名稱的同名目錄。另外,根據(jù)前文所述,使用alias標(biāo)簽的目錄塊中不能使用rewrite的break。

這樣在看這段就很清晰了,

復(fù)制代碼 代碼如下:


location /abc/ {
alias /home/html/abc/;
}


在這段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。這段配置亦可改成

復(fù)制代碼 代碼如下:


location /abc/ {
root /home/html/;
}


這樣,nginx就會(huì)去找/home/html/目錄下的abc目錄了,得到的結(jié)果是相同的。

但是,如果我把a(bǔ)lias的配置改成:

復(fù)制代碼 代碼如下:


location /abc/ {
alias /home/html/def/;
}


那么nginx將會(huì)從/home/html/def/取數(shù)據(jù),這段配置還不能直接使用root配置,如果非要配置,只有在/home/html/下建立一個(gè) def->abc的軟link(快捷方式)了。

一般情況下,在location /中配置root,在location /other中配置alias是一個(gè)好習(xí)慣。

關(guān)于“nginx,apache的alias和認(rèn)證功能實(shí)例分析”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“nginx,apache的alias和認(rèn)證功能實(shí)例分析”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI