Ngnix configuration

The difference between root and alias in Nginx

server {
    listen 80;
    autoindex off;
    server_name image.test.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
        deny all;
    }

    location /image {
        alias D:\ftp\image;
        add_header Access-Control-Allow-Origin *;
    }
}

 

Case 1. When requesting image.test.com/image/1.jpg

alias D:\ftp\image; the file

root D:\ftp\image; the file will be mapped to D:\ftp\image\image\1.jpg.

错误日志:2017/07/30 15:29:10 [error] 8276#7424: *108 CreateFile() "D:\ftpfile\image/image/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/1.jpg HTTP/1.1", host: "image.imooc.com"

Case 2. When requesting image.test.com/image/img/1.jpg

alias D:\ftp\image; the file will be mapped to D:\ftp\image\ img\1.jpg

错误日志:2017/07/30 15:17:53 [error] 9120#1920: *99 CreateFile() "D:\ftpfile\image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/img/1.jpg HTTP/1.1", host: "image.imooc.com"

root D:\ftp\image; the file will be mapped to D:\ftp\image\image\img\1.jpg.

错误日志:2017/07/30 15:25:15 [error] 7604#5164: *103 CreateFile() "D:\ftpfile\image/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /image/img/1.jpg HTTP/1.1", host: "image.imooc.com"

 

Case 3. When requesting image.test.com/img/image/img/1.jpg

alias D:\ftp\image; the file will be mapped to ${NGINX_HOME}\ html\img\image\img\1.jpg

错误日志:2017/07/30 15:19:21 [error] 9120#1920: *101 CreateFile() "D:\software\nginx-1.10.2/html/img/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /img/image/img/1.jpg HTTP/1.1", host: "image.imooc.com"

root D:\ftp\image; the file ${NGINX_HOME}\ html\img\image\img\1.jpg

错误日志:2017/07/30 15:29:51 [error] 8276#7424: *108 CreateFile() "D:\software\nginx-1.10.2/html/img/image/img/1.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: image.imooc.com, request: "GET /img/image/img/1.jpg HTTP/1.1", host: "image.imooc.com"

 

Summarize:

1. First map the URL, and if successful, redirect the path in the location. If it fails, redirect to the default root specified path, which is the html folder.

2. The location is configured as alias, and simple string replacement is performed. The location is configured as root, which only represents the root path, and actually needs to be spliced ​​with the request path.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359969&siteId=291194637