nginx,apache的alias和认证功能

从年前电脑换成linux系统后就没写东西,最近有点懒,在这里讲述下nginx alias 功能,不是server alias .
首先看下看下apache 别名 怎么配置的:

代码如下:

<VirtualHost *:80>
DocumentRoot /www/jb51.net/www 这是虚拟主机的根目录吧,但是phpMYadmin 不在这个目录下,想访问。
ServerName www.jb51.net
ServerAlias jb51.net
Alias /sdb "/www/public/phpMyAdmin/" 就需要 别名功能,:http://www.jb51.net/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

下面是例子

二、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;
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 { 访问的名字http://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目录了,得到的结果是相同的。

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

本文出自 “学习要永恒” 博客

(0)

相关推荐

  • nginx try_files指令判断文件是否存在实例

    现在有这样一个需求,网站根目录下有静态文件,static目录下也有静态文件,static目录下的静态文件是程序批量生成的,我想让nginx在地址不变的前提下优先使用static目录里面的文件,如果不存在再使用根目录下的静态文件,比如访问首页http://example.com/index.html则nginx返回/static/index.html,如果不存在返回/index.html. 经过一番研究可以用if指令实现,关键配置如下,这条配置需要放到靠前的位置 复制代码 代码如下: if (-e

  • nginx add_header指令使用方法

    response header一般都是以key:value的形式,例如:"Content-Encoding:gzip.Cache-Control:no-store",设置的命令为: 复制代码 代码如下: add_header Cache-Control no-storeadd_header Content-Encoding gzip 但是有一个十分常用的response header比较特性,就是Content-Type,可以在它设置了类型的同时还会指定charset,例如:"

  • Nginx Gzip模块启用和配置指令详解

    Nginx的gzip模块是内置的,在http中添加如下配置: 复制代码 代码如下: gzip on;gzip_min_length  5k;gzip_buffers     4 16k;gzip_http_version 1.0;gzip_comp_level 3;gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php

  • Nginx配置指令location匹配符优先级和安全问题

    最近一直在做location 配置,遇到优先级别问题(如果配置不当可能存在安全隐患哦),以下是个人学习一点体会. 一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配符:~正则匹配符是可以使用正则表达式的匹配符.不过这里要强调的是,一般来说~是指:区分大小写的正则匹配而~*表示:不区分大小写的正则匹配但是对于一些对大小写不敏感的操作系统,这两者没有区

  • PHP(FastCGI)在Nginx的alias下出现404错误的解决方法

    本文讲述了PHP(FastCGI)在Nginx的alias下出现404错误的解决方法.分享给大家供大家参考,具体如下: 在Nginx的官方wiki中如下描述 The alias directive cannot be used inside a regex-specified location. If you need to do this you must use a combination of rewrite and root. 在实际使用中alias下面的php返回404,而html确可

  • Nginx配置中指令root和alias的区别浅析

    前言 最近在nginx上部署日志分析工具awstats时,在配置awstats分析结果可供网页浏览这步时,分析结果页面访问总是404.后来查阅了一些资料,发现是root和alias的用法区别没搞懂导致的,这里特地将这两者区别详尽道来,供大家学习参考. root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如: location /i/ { root /data/w3; } 请求 http://foofish.net/i/top.gif 这个地址时,那么在服务器

  • nginx日志配置指令详解

    日志对于统计排错来说非常有利的.本文总结了nginx日志相关的配置如access_log.log_format.open_log_file_cache.log_not_found.log_subrequest.rewrite_log.error_log. nginx有一个非常灵活的日志记录模式.每个级别的配置可以有各自独立的访问日志.日志格式通过log_format命令来定义.ngx_http_log_module是用来定义请求日志格式的. 1. access_log指令 语法: access_

  • nginx proxy_pass指令’/’使用注意事项

    1. proxy_pass配置说明 不带/ 复制代码 代码如下: location /test/ { proxy_pass http://t6:8300;  } 带/ 复制代码 代码如下: location /test/  {                  proxy_pass http://t6:8300/;   } 上面两种配置,区别只在于proxy_pass转发的路径后是否带 "/" 针对情况1,如果访问url = http://server/test/test.jsp,则被

  • nginx HTTP模块配置常用指令

    一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.配置指令 1. alias含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适用文件系统的路径.语法:alias <file-path | directory-path>缺省:N/A作用域:http.server.location示例: 复制代码 代码如下: location /i/ {    alias /home/michael/web/i/;} 如请求 /i/log

  • Nginx中的root&alias文件路径及索引目录配置详解

    root&alias文件路径配置 nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上. [root] 语法:root path 默认值:root html 配置段:http.server.location.if [alias] 语法:alias path 配置段:location 实例: loca

随机推荐