在一个server中,如果需要根据前缀访问不同文件夹下的文件,则可以使用alias来设置。
如下代码:

    server {
        listen 9085;
        server_name 127.0.0.1;
        absolute_redirect off;
        #server_name_in_redirect off;
        
        
        #root D:/dotnet/netframework/siteserver_install/;
        index index.html;
        
        location = / {
            proxy_pass http://127.0.0.1:8084/index.html;
        }
        
        location / {
            proxy_pass http://127.0.0.1:8085/;    
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }        
        
        location /eduinfo/ {
            alias D:/dotnet/netframework/Test/;            
            expires 24h;
        }
        
        location /mytest {
            alias D:/dotnet/mytestfolder;
            expires 24h;            
        }    
    }
    
在这个server配置中,使用index指定了目录下的默认访问文件,如下图:

 即访问的URL若没有指向具体文件,则会访问URL目录下的index.html文件。 

 

接下来让我们看看配置时,目录后面带/斜杠和不带/斜杠的区别。
1、location目录结尾带斜杠时,如: location /eduinfo/ 这种配置时:
访问 localhost:9085/eduinfo时的结果:

 

访问localhost:9085/eduinfo/时的结果:

 从结果来看,证明location配置目录时,结尾带斜杠的需要精确匹配到/斜杠才行。

 

2、location目录结尾没有带/斜杠,如:localhost:9085/mytest这种配置时:
访问 localhost:9085/mytest时的结果:

   

访问localhost:9085/mytest/时的结果:

从结果来看,使用location /mytest 这种结尾不带斜杠的配置时,nginx会自动301跳转到带上一个/斜杠的URL,并访问其目录下的index..html文件。

 

 

 posted on 2020-10-28 15:46  F风  阅读(2261)  评论(0编辑  收藏  举报