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;
}
针对目录的认证,在一个单独的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;
}
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功能配置自动列目录
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目录了,得到的结果是相同的。








