Mapping URL to Different Path In Nginx – TecAdmin

Channel: Linux
Abstract: //www.example.com/static/file.txt will point to /var/www/static/file.txt file.location /static/ { alias /var/content/static/

Sometimes we need to map a sub URL to a different directory path in the file system. The Nginx users can achieve this by using the 「location」 block in the configuration file. The location specifies a regular expression for the URL the browser requests. Under the location code block, we can specify the file system path with the ‘root’ or ‘alias’ option.

You may use the alias directive within a location block, like this:

server { server_name www.example.com; root /var/www/example.com; location /static/ { alias /var/content/static/; } }1234567server {    server_name www.example.com;    root /var/www/example.com;    location /static/ {        alias /var/content/static/;    }}

In the above configuration, the main site is configured with the /var/www/example.com directory. But the URL begins with 「/static」 will be served with 「/var/content/static」 directory. For example, A URL http://www.example.com/static/file.txt will point to /var/www/static/file.txt file.

Ref From: tecadmin

Related articles