linux配置nginx虚拟目录

转自:https://blog.csdn.net/whatday/article/details/50649461

今天配置awstats,awstats创建出的文件目录在/home/awstats下,在nginx中加入配置后狂报404,发现还是忽略了root和alias的区别,特将修改配置记录如下:

1.失败:server {

        server_name  test.com;

        charset utf-8,GB2312;

        index  index.html;

        location / {

        root html;

        access_log logs/access.log main;

         }

        location ~ ^/awstats/ {

        root  /home/awstats/;

        index  index.html;

        access_log off;

        error_log off;

        charset gb2312;

        }

2.失败: server {

        server_name  test.com;

        charset utf-8,GB2312;

        index  index.html;

        location / {

        root html;

        access_log logs/access.log main;

         }

        location ~ ^/awstats/ {

        alias  /home/;

        index  index.html;

        access_log off;

        error_log off;

        charset gb2312;

        }

3.成功: server {

        server_name  test.com;

        charset utf-8,GB2312;

        index  index.html;

        location / {

        root html;

        access_log logs/access.log main;

         }

        location ~ ^/awstats/ {

        alias  /home/awstats/;

        index  index.html;

        access_log off;

        error_log off;

        charset gb2312;

        }


4.成功: server {

        server_name  test.com;

        charset utf-8,GB2312;

        index  index.html;

        location / {

        root html;

        access_log logs/access.log main;

         }

        location ~ ^/awstats/ {

        root  /home/;

        index  index.html;

        access_log off;

        error_log off;

        charset gb2312;

        }

从以上例子很明显看出,还是对root和alias的概念搞混了~

1.      location ~ ^/awstats/ {

        root  /home/awstats/;

访问:http://test.com/awstats/ 实际访问的是/home/awstats/awstats/

2.      location ~ ^/awstats/ {

        alias  /home/

访问:http://test.com/awstats/ 实际访问的是/home/

3.      location ~ ^/awstats/ {                        #使用alias时目录名后面一定要加“/”

        alias  /home/awstats/;

访问:http://test.com/awstats/ 实际访问的是/home/awstats/

4.      location ~ ^/awstats/ {

        root  /home/;

访问:http://test.com/awstats/ 实际访问的是/home/awstats/

借用ayou老师的一句话:

一般情况下,在location /中配置root,在location /other中配置alias是一个好习惯

====================================================================

我的需求是这样的,系统有一个专门的文件夹用于存放图片,css,js或者附件,如:

http://www.test.com/resources/images/a.jpg

http://www.test.com/resources/css/a.css

http://www.test.com/resources/js/a.js

http://www.test.com/resources/attach/a.doc

这样的配置对于apache来说那相当容易,

需要通过location uri规则匹配访问到该文件夹,我使用如下配置:

location ^~ /resources/ {

root d:/www/;

}

试了N多次都能访问不到,一直报404,无比杯具!最后拜读了上面提供的blog才解决,发现跟原博主一样,没有真正搞清楚,location中root和alias的区别,最后修改成:

location ^~ /resources/ {

    alias d:/www/;

}

成功实现了我的需求。

原贴如下:

niginx 似乎没有虚拟目录的说法,但是可以指定请求路径时nginx访问的路径,也算是一个解决办法。

server {

listen       80 default;

server_name  _;

location / {

root   html;

index  403.html;

}

location ~ //.ht {

deny  all;

}

location /phpadmin/ {

alias   /opt/www/phpadmin/;

index   index.php;

}

location ~ /.php$ {

include httpd.conf;

}

}

要注意的是, location /phpadmin/ {} 和 location /phpadmin {} 是完全不同的。

前者可以访问到目录,而后者将被重定向到服务器,如:http://127.0.0.1/phpadmin,将被重定向到http://_/phpadmin

下面这个配置和上面基本类似,唯一的不同是,所有对 /phpadmin/的访问将正确解析,而其他访问则返回页面不存在(404)的信息。

server {

listen       80 default;

server_name  _;

location / {

root   html;

#index  403.html;

return 404;

}

location ~ //.ht {

deny  all;

}

location /phpadmin/ {

alias   /opt/www/phpadmin/;

index   index.php;

}

location ~ /.php$ {

include httpd.conf;

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个location配置块使用了多个 Nginx 模块...
    SkTj阅读 7,829评论 0 12
  • I/O模型: 阻塞型、非阻塞型、复用型、信号驱动型、异步 同步/异步:关注消息通知机制 消息通知:同步:等待对方返...
    Net夜风阅读 2,038评论 0 1
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,090评论 0 9
  • 1.ngnix介绍 ngnix www服务软件 俄罗斯人开发 开源 性能很高 本身是一款静态WWW软件 静态小文件...
    逗比punk阅读 2,131评论 1 6
  • 四个力量拔除缺少责任心的坏种子(10月7日) 今天在店里,看到有个员工总是在看手机,我心里很不满。看到一大堆图纸需...
    正娟_d019阅读 186评论 1 1