Home  >  Q&A  >  body text

rewrite - What is the specific use of nginx alias?

I’ve been looking at nginx recently, but I’m not sure what the specific use of alias is. Both root and rewrite can achieve it, why use alias?

root /data/mydomain/main/web/;
location ~* ^/app(|/)$ {
        #alias /data/mydomain/main/public/app/;
        rewrite  ^/app(|/)$ /public/app/ last;
        expires 30s;
}

In the above configuration, rewrite takes effect and meets my expectations. But there is a problem with alias.

黄舟黄舟2647 days ago430

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 17:17:38

    This is a matter of your own understanding. First of all, rewrite has nothing to do with root and alias.
    rewrite is for http requests. The path in it is the address in the URL, which is the conversion between addresses.
    The root and alias are the settings of the system file path.
    The root in the area is used to set the root directory, and alias is used to reset the directory of the current file.

    location /img/ {
        alias /var/www/image/;
    }
    #若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
    location /img/ {
        root /var/www/image;
    }
    #若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]

    reply
    0
  • Cancelreply