二、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 - #回车输入密码,-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 文件中加入授权声明。这里要注意 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 问题,希望大家成功。出现问题可以向我咨询大家共同进步!
