首頁 > 運維 > Nginx > 主體

nginx,apache的alias和認證功能實例分析

WBOY
發布: 2023-05-24 23:10:22
轉載
745 人瀏覽過

首先看一下看下apache 別名怎麼設定的:

複製程式碼 程式碼如下:


documentroot /www/jb51. net/www 這是虛擬主機的根目錄吧,但phpmyadmin 不在這個目錄下,想存取。
servername www.jb51.net
serveralias jb51.net
alias /sdb "/www/public/phpmyadmin/" 就需要別名功能,://www.jb51.net/sdb 這樣就安全多了。

options indexes followsymlinks
allowoverride none
order allow,deny
allow from all

一.apache認證

認證的類型:basic
digest摘要
認證方法:a、容器認證: ……
b、隱藏檔案認證建立.htaccess檔案
方法一、容器認證
a、進入設定檔vi /etc/httpd/conf/httpd.conf
b、設定:大約在531行附近配置如下:

allowoverride none ##不允許通過隱藏認證,即透過容器認證
authtype basic ##認證類型為basic
authname “ajian”#認證名字為ajian
authuserfile /var/www/passwd/pass ##pass 為認證密碼文件,指定密碼文件存放的位置。
require valid-user ##有效用戶(注意大小寫,因為word的原因有些大小寫有變化)
c、 創建目錄mkdir -p /var/www/passwd
進入目錄cd /var /www/passwd
d、建立apache用戶htpasswd -c pass ajian ##pass 為密碼檔案ajian為使用者
更改把pass檔案的使用權給apache: chown apache.apache pass
附:再在pass檔案中新增一個使用者:htpasswd pass tt ##新增一個tt的使用者到pass檔案中
e、重啟服務並測試
方法二、透過隱藏認證
和上面差不多不過設定不一樣
httpd主設定檔

allowoverride authconfig
建立隱藏檔案並放到要通過認證的目錄
eg: vi /var/www/html/mrtg
authtype basic
authname “ajian”
authuserfile /var/www/passwd/pass
require valid-user

#下面是範例

nginx,apache的alias和認證功能實例分析

nginx,apache的alias和認證功能實例分析

nginx,apache的alias和認證功能實例分析

二、nginx 登入認證

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 #Enter輸入密碼,-c 表示產生文件,-d 是以 crypt 加密。
vi nginx.conf cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #Enter輸入密碼,-c 表示產生文件,-d 是以crypt 加密。 vi nginx.conf 在 nginx.conf 檔案中加入授權聲明。這裡要注意 nginx 0.6.7 開始,auth_basic_user_file 的相對目錄是 nginx_home/conf,先前版本的相對目錄是 nginx_home。

複製程式碼 程式碼如下:


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 pagecgi_params; .php;
error_page 403 /404.php;

access_log /logs/tuan_access.log main;
}


#針對目錄的認證,在一個單獨的location中,並且在該location中嵌套一個解釋php的location,否則php檔案不會執行並且會被下載。 auth_basic在嵌套的location之後。


複製程式碼 程式碼如下:

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;
}
/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;##fastcgi_index index.php;
include fastcgi_params;
}

access_log /logs/tuan_access.log main;
}



三.nginx alias功能設定自動列目錄


複製程式碼 程式碼如下:

#


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/; 這是伺服器上存放日誌的地方
} 這段意思訪問www.jb51.net:88/club 就看到club目錄的東東了。
location /{
root /www/access;
這段location 也可以沒有www.jb51.net:88 出來的是預設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認證進去是預設存取伺服器上/www/access/裡面的目錄,認證進去後url=http: //hou.xywy.com:88/club 就出來/www/clublog/club.xywy.com/ 裡面的目錄的內容了。 ,可能很繞,仔細分析就好了。

root 和 alias 的差別。
最基本的區別:alias指定的目錄是準確的,root是指定目錄的上級目錄,並且該上級目錄要含有location指定名稱的同名目錄。另外,根據前文所述,使用alias標籤的目錄區塊中不能使用rewrite的break。

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

複製程式碼 程式碼如下:


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


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

複製程式碼 程式碼如下:


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


這樣,nginx就會去找/home/html/目錄下的abc目錄了,得到的結果是一樣的。

但是,如果我把alias的設定改成:

複製程式碼 程式碼如下:


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


那麼nginx將會從/home/html/def/取數據,這段配置還不能直接使用root配置,如果非要配置,只有在/home/html/下建立一個def->abc的軟link(捷徑)了。

一般情況下,在location /中配置root,在location /other中配置alias是一個好習慣。

以上是nginx,apache的alias和認證功能實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!