2014-02-22 15:55:34
来 源
ITJS.CN
Nginx
本文介绍如何配置Nginx,Apache服务器的alias和密码认证,希望对于初学Apache服务器相关的朋友有帮助,更多Apache安装、配置、报错处理等资源请本站内搜索。

从年前电脑换成linux系统后就没写东西,最近有点懒,在这里讲述下nginx alias 功能,不是server alias .

首先看下看下apache 别名 怎么配置的:

<VirtualHost *:80>  

DocumentRoot /www/hou.net/www   这是虚拟主机的根目录吧,但是phpMYadmin 不在这个目录下,想访问。

ServerName  www.hou.net  

ServerAlias hou.net     

Alias /sdb "/www/public/phpMyAdmin/"   就需要 别名功能,:http://www.hou.com/sdb 这样就安全多了。

<Directory "/www/public/phpMyAdmin/">    

Options Indexes FollowSymLinks    

AllowOverride None     

Order allow,deny       

Allow from all   

</Directory>    

</VirtualHost>

一 .Apache认证

认证的类型:Basic

Digest摘要

认证方法:A、容器认证: ……

B、隐藏文件认证创建.htaccess文件

方法一、容器认证

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、重启服务并测试

方法二、通过隐藏认证

和上面差不多 不过配置不一样

Httpd主配置文件

AllowOverride AuthConfig

创建隐藏文件并放到要通过认证的目录

Eg: vi /var/www/html/mrtg

AuthType Basic

AuthName “Ajian”

AuthUserFile /var/www/passwd/pass

Require valid-user

下面是例子

5.nginx,apache 的alias 和认证 功能 - zhuzhu - 五事九思 5.nginx,apache 的alias 和认证 功能 - zhuzhu - 五事九思 5.nginx,apache 的alias 和认证 功能 - zhuzhu - 五事九思

二、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-dpass_fileuser_name

#回车输入密码,-c表示生成文件,-d是以crypt加密。

vinginx.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 问题,希望大家成功。出现问题可以向我咨询大家共同进步!

--------------------------------

Nginx对某个目录或整个网站进行登录认证

一、创建密码文件

touch /root/passconf/nginxpwd

此文件的书写格式是

用户名:密码

每行一个账户

并且 密码必须使用函数 crypt(3) 加密

创建密码文件的方法:

1、利用 apache的htpasswd 工具,

apt-get install apache2-utils

htpasswd -c /root/passconf/nginxpwd devops

按照提示输入密码即可

2、利用perl脚本

#!/usr/bin/perl

use strict;

my $pw=$ARGV[0] ;

print crypt($pw,$pw).”n”;

然后执行

chmod +x pw.pl

./pw.pl password

输出 papAq5PwY/QQM 这就是 password 对应的加密密码

将输出的字符串 复制 然后编译密码文件

格式:

用户名:密码

例如

devops:papAq5PwY/QQM

二、编辑nginx的站点配置文件

添加以下内容

auth_basic “Devops-Login!”;

auth_basic_user_file /root/passconf/nginxpwd;

针对目录的就添加在locaction区域里

针对整个站点的就加在server段中

三、重启nginx服务,使配置生效

/etc/init.d/nginx restart

-----------------------------

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。

例如:基于整个网站的认证,auth_basic在php解释之前。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

server {

    listen       80;

    server_name www.akii.org akii.org;

    root  /www/akii;

    index index.html index.htm index.php;

    auth_basic "input you user name and  password";

    auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;

    location ~ .php$ {

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        include fastcgi_params;

    }

    location ~ /.ht {

         deny  all;

    }

    access_log /logs/akii.org_access.log main;

}

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

server {

    listen       80;

    server_name www.akii.org akii.org;

    root  /www/akii;

    index index.html index.htm index.php;

    location ~ ^/admin/.* {

        location ~ .php$ {

            fastcgi_pass  127.0.0.1:9000;

            fastcgi_index index.php;

            include fastcgi_params;

        }

        auth_basic "auth";

        auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;

    }

    location ~ .php$ {

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        include fastcgi_params;

    }

    location ~ /.ht {

         deny  all;

    }

    access_log /logs/akii.org_access.log main;

}

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。