手机版
你好,游客 登录 注册
背景:
阅读新闻

Nginx与Apache的Alias和认证功能

[日期:2012-03-30] 来源:Linux社区  作者:hzcsky [字体:大 中 小]

二、Nginx 登录认证

nginx 的 http auth basic 的密码是用 crypt(3) 加密的。用 apache 的 htpasswd 可以生成密码文件。

没有 apache 自行安装。我安装的是 apache2,/usr/local/apach2。

  1. cd /usr/local/nginx/conf   
  2. /usr/local/apache2/bin/htpasswd -c -d pass_file user_name   
  3. #回车输入密码,-c 表示生成文件,-d 是以 crypt 加密。  
  4. 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;
    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.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 问题,希望大家成功。出现问题可以向我咨询大家共同进步!

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款