從年前電腦換成linux系統後就沒寫東西,最近有點懶,在這裏講述下nginx alias 功能,不是server alias .php
首先看下看下apache 別名 怎麼配置的:html
<VirtualHost *:80> linux
DocumentRoot /www/hou.net/www 這是虛擬主機的根目錄吧,可是phpMYadmin 不在這個目錄下,想訪問。nginx
ServerName www.hou.net shell
ServerAlias hou.net apache
Alias /sdb "/www/public/phpMyAdmin/" 就須要 別名功能,:http://www.hou.com/sdb 這樣就安全多了。安全
<Directory "/www/public/phpMyAdmin/"> 服務器
Options Indexes FollowSymLinks ide
AllowOverride None 測試
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
一 .Apache認證
認證的類型:Basic
Digest摘要
認證方法:A、容器認證:
B、隱藏文件認證建立.htaccess文件
方法1、容器認證
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、重啓服務並測試
方法2、經過隱藏認證
和上面差很少 不過配置不同
Httpd主配置文件
AllowOverride AuthConfig
建立隱藏文件並放到要經過認證的目錄
Eg: vi /var/www/html/mrtg
AuthType Basic
AuthName 「Ajian」
AuthUserFile /var/www/passwd/pass
Require valid-user
下面是例子
2、Nginx 登陸認證
nginx 的 http auth basic 的密碼是用 crypt(3) 加密的。用 apache 的 htpasswd 能夠生成密碼文件。
沒有 apache 自行安裝。我安裝的是 apache2,/usr/local/apach2。
在 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;
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;
}
針對目錄的認證,在一個單獨的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;
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;
}
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功能配置自動列目錄
server {
listen www.hou.com:88;
server_name www.hou.com;
autoindex on; //開啓列目錄功能。
# charset gbk;
location /club { 訪問的名字http://www.hou.com:88/club
alias /www/clublog/club.xywy.com/; 這是服務器上存放日誌的地方
} 這段意思 訪問www.hou.com:88/club 就看到club目錄的東東了。
location /{
root /www/access;
這段location 也能夠沒有 www.hou.com: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是一個好習慣。
至於alias和root的區別,我估計尚未說徹底,若是在配置時發現奇異問題,不妨把這二者換換試試。
剛開始我也搞來高去搞了好久包括認證單獨一個目錄 CGI 問題,但願你們成功。出現問題能夠向我諮詢你們共同進步!