通过nginx访问静态文件配置 - panda-star的博客 - CSDN博客

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinabestchina/article/details/73556785

通过nginx访问静态文件配置,均是在server模块中配置,有两种方式:

1、alias

通过alias关键字,重定义路径,如

server{
    listen 7001;
    server_name 127.0.0.1;
    location /file/ {
        alias /home/china/areas/;
    }
}

此时,通过浏览器访问http://127.0.0.1:7001/file/t.txt,则访问服务器的文件是/home/china/areas/t.txt

alias可以使用正则表达式,如

location ~ ^/test/(\w+).(\w+)$ {

alise /home/china/$2/$1.$2;

}

访问/test/t.conf,则实际访问的是/home/china/conf/t.conf

2、root

通过root关键字,重定义路径,如

server{
    listen 7002;
    server_name 127.0.0.1;
    location /test/ {
        root /home/china/areas/;
    }
}

此时,通过浏览器访问http://127.0.0.1:7001/test/t.txt,则访问服务器的文件是/home/china/areas/test/t.txt

上述两种方法均可达到目的,区别是它们对路径的解析方式不同,alas会把指定路径当作文件路径,

而root会把指定路径拼接到文件路径后,再进行访问。


Original url: Access
Created at: 2019-04-24 11:21:33
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论