nginx,apache的alias和認(rèn)證功能
首先看下看下apache 別名 怎么配置的:
<VirtualHost *:80>
DocumentRoot /www/jb51.net/www 這是虛擬主機(jī)的根目錄吧,但是phpMYadmin 不在這個(gè)目錄下,想訪問(wèn)。
ServerName www.dbjr.com.cn
ServerAlias jb51.net
Alias /sdb "/www/public/phpMyAdmin/" 就需要 別名功能,:http://www.dbjr.com.cn/sdb 這樣就安全多了。
<Directory "/www/public/phpMyAdmin/">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
一 .Apache認(rèn)證
認(rèn)證的類(lèi)型: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 ##不允許通過(guò)隱藏認(rèn)證,即通過(guò)容器認(rèn)證
AuthType Basic ##認(rèn)證類(lèi)型為Basic
AuthName “ajian” ##認(rèn)證名字為Ajian
AuthUserFile /var/www/passwd/pass ##pass 為認(rèn)證密碼文件,指定密碼文件存放的位置。
Require valid-user ##有效用戶(注意大小寫(xiě),因?yàn)閃ord的原因有些大小寫(xiě)有變化)
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è)試
方法二、通過(guò)隱藏認(rèn)證
和上面差不多 不過(guò)配置不一樣
Httpd主配置文件
AllowOverride AuthConfig
創(chuàng)建隱藏文件并放到要通過(guò)認(rèn)證的目錄
Eg: vi /var/www/html/mrtg
AuthType Basic
AuthName “Ajian”
AuthUserFile /var/www/passwd/pass
Require valid-user
下面是例子
二、Nginx 登錄認(rèn)證
nginx 的 http auth basic 的密碼是用 crypt(3) 加密的。用 apache 的 htpasswd 可以生成密碼文件。
沒(méi)有 apache 自行安裝。我安裝的是 apache2,/usr/local/apach2。
cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回車(chē)輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。
vi nginx.conf cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回車(chē)輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。 vi nginx.conf 在 nginx.conf 文件中加入授權(quán)聲明。這里要注意 nginx 0.6.7 開(kāi)始,auth_basic_user_file 的相對(duì)目錄是 nginx_home/conf,以前版本的相對(duì)目錄是 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 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之后。
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)列目錄
server {
listen www.dbjr.com.cn:88;
server_name www.dbjr.com.cn;
autoindex on; //開(kāi)啟列目錄功能。
# charset gbk;
location /club { 訪問(wèn)的名字http://www.dbjr.com.cn:88/club
alias /www/clublog/club.xywy.com/; 這是服務(wù)器上存放日志的地方
} 這段意思 訪問(wèn)www.dbjr.com.cn:88/club 就看到club目錄的東東了。
location /{
root /www/access;
這段location 也可以沒(méi)有 www.dbjr.com.cn:88 出來(lái)的是默認(rèn)nxing 頁(yè)面
# index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
上面nginx配置意思就是: 訪問(wèn)http://hou.xywy.com/:88認(rèn)證進(jìn)去是默認(rèn)訪問(wèn)服務(wù)器上/www/access/里面的目錄,認(rèn)證進(jìn)去后url=http://hou.xywy.com:88/club 就出來(lái) /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。
這樣在看這段就很清晰了,
location /abc/ {
alias /home/html/abc/;
}
在這段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。這段配置亦可改成
location /abc/ {
root /home/html/;
}
這樣,nginx就會(huì)去找/home/html/目錄下的abc目錄了,得到的結(jié)果是相同的。
但是,如果我把a(bǔ)lias的配置改成:
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í)慣。
至于alias和root的區(qū)別,我估計(jì)還沒(méi)有說(shuō)完全,如果在配置時(shí)發(fā)現(xiàn)奇異問(wèn)題,不妨把這兩者換換試試。
剛開(kāi)始我也搞來(lái)高去搞了很久包括認(rèn)證單獨(dú)一個(gè)目錄 CGI 問(wèn)題,希望大家成功。出現(xiàn)問(wèn)題可以向我咨詢大家共同進(jìn)步!
本文出自 “學(xué)習(xí)要永恒” 博客
相關(guān)文章
nginx proxy_pass反向代理配置中url后加不加/的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于nginx proxy_pass反向代理配置中url后加不加/的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11filebeat收集多個(gè)域名網(wǎng)站日志并存儲(chǔ)到不同es索引庫(kù)過(guò)程
這篇文章主要為大家介紹了filebeat收集多個(gè)域名網(wǎng)站日志并存儲(chǔ)到不同es索引庫(kù)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Ubuntu?22.04.1?LTS?編譯安裝?nginx-1.22.1的配置過(guò)程
Ubuntu安裝Nginx有兩種方式,一種是通過(guò)命令的方式,這種方式安裝的Nginx版本低,之前漏掃掃出來(lái)Nginx版本低,需要升級(jí)所以現(xiàn)在用編譯的方式安裝版本高點(diǎn)的,本文介紹Ubuntu22.04.1?LTS編譯安裝nginx1.22.1的配置過(guò)程,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01zabbix自定義監(jiān)控nginx狀態(tài)實(shí)現(xiàn)過(guò)程
這篇文章主要為大家介紹了zabbix如何自定義監(jiān)控nginx狀態(tài)的實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10Nginx限制某個(gè)IP同一時(shí)間段的訪問(wèn)次數(shù)和請(qǐng)求數(shù)示例代碼
nginx可以通過(guò)ngx_http_limit_conn_module和ngx_http_limit_req_module配置來(lái)限制ip在同一時(shí)間段的訪問(wèn)次數(shù).具體示例代碼大家參考下本文2017-08-08Mac中使用Nginx實(shí)現(xiàn)80端口轉(zhuǎn)發(fā)8080端口
端口轉(zhuǎn)發(fā)(Port forwarding),有時(shí)被叫做隧道,是安全殼(SSH) 為網(wǎng)絡(luò)安全通信使用的一種方法。端口轉(zhuǎn)發(fā)是轉(zhuǎn)發(fā)一個(gè)網(wǎng)絡(luò)端口從一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)到另一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)的行為,其使一個(gè)外部用戶從外部經(jīng)過(guò)一個(gè)被激活的NAT路由器到達(dá)一個(gè)在私有內(nèi)部IP地址(局域網(wǎng)內(nèi)部)上的一個(gè)端口2017-09-09Nginx搭建rtmp直播服務(wù)器實(shí)現(xiàn)代碼
這篇文章主要介紹了Nginx搭建rtmp直播服務(wù)器實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11