Nginx之alias path 与root配置段的区别

alias path 与root配置段的区别html

alias 适用于:locationnginx

定义路径别名,文档映射的一种机制。web


在httpd中的vim

alias /bbs/ /lufei/root/ 示例curl

访问:http://www.lufei.com/bbs/index.htmlide

实际访问:http://www.lufei.com/lufei/root/index.htmlui

这个是以/bbs/为根。url

在nginx中的示例:spa

location /bbs/ {server

alias /lufei/root/;

}


访问:http://www.lufei.com/bbs/index.html

实际访问:http://www.lufei.com/lufei/root/index.html

这个是以/bbs/为根。

实际是将/bbs/ 换为/web/forum/,其中/web/forum/为系统中真实存在的目录,/bbs/为虚拟的目录


另外一种配置root的方法:

location /bbs/ {

root /lufei/root/;

}

访问:http://www.lufei.com/bbs/index.html

实际访问:http://www.lufei.com/lufei/root/bbs/index.html

这个是以root为根。

注意:bbs/index.html 为uri。

alias指令:给定的路径对应于location中的/uri/后面的右侧的/;

root指令:给定的路径对应于location中的/uri/左侧的/;

示例演示:

root路径方法:

0一、mkdir -p /lufei/root/bbs/    建立网页目录

0二、vim index.html 建立一个html文件

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-bbs-root</h2>

<h3>www.lufei-root.com</h3>

</body>  

</html>

0三、在/etc/nginx/conf.d/目录下建立一个conf配置文件

vim root.conf

server {

listen 80;

server_name www.lufei.com;

location /bbs/ {

root    /lufei/root/;

#root yuming;

# 默认跳转到index.html页面    

index index.html;

}

}

0四、nginx -s reload

0五、访问:

[root@localhost bbs]# curl www.lufei.com/bbs/

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-bbs-root</h2>

<h3>www.lufei-root.com</h3>

</body>  

</html>

alias路径方法:

0一、mkdir -p /nginx/bbs/    建立网页目录

0二、vim index.html 建立一个html文件

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-bbs-alias</h2>

<h3>www.lufei-alias.com</h3>

</body>  

</html>

0三、在/etc/nginx/conf.d/目录下建立一个conf配置文件

vim alias.conf

server {

listen 80;

server_name www.lufei.com;

location /bbs/ {

alias    /nginx/bbs/;

# 默认跳转到index.html页面    

index index.html;

}

}

0四、nginx -s reload

0五、访问:

[root@localhost bbs]# curl www.lufei.com/bbs/

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-bbs-alias</h2>

<h3>www.lufei-alias.com</h3>

</body>  

</html>

相关文章
相关标签/搜索