Serving Static Content
A server that listens on the standard port 80 and
is accessible on the local machine at
http://localhost/
http {
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
Describe above config file:
In response to requests with URIs starting with
/images/, the server will send files from the
/data/images directory. For example, in response
to the http://localhost/images/example.png
request nginx will send the
/data/images/example.png file. If such file does
not exist, nginx will send a response indicating
the 404 error. Requests with URIs not starting
with /images/ will be mapped onto the /data/www
directory. For example, in response to the
http://localhost/some/example.html request nginx
will send the /data/www/some/example.html file.
Directive “root”
Syntax: root path;
Default: root html;
Sets the root directory for requests. For
example, with the following configuration
location /i/ {
root /data/w3;
}
The /data/w3/i/top.gif file will be sent in
response to the “/i/top.gif” request.
To apply the new configuration, start nginx if it
is not yet started or send the reload signal to
the nginx’s master process, by executing:
nginx -s reload
In case something does not work as expected, you
may try to find out the reason in access.log and
error.log files in the directory
/usr/local/nginx/logs or /var/log/nginx.
Generally, the configuration file may include
several server blocks distinguished by ports on
which they listen to and by server names. Once
nginx decides which server processes a request,
it tests the URI specified in the request’s
header against the parameters of the location
directives defined inside the server block.
This location block specifies the “/” prefix
compared with the URI from the request.
If there are several matching location blocks
nginx selects the one with the longest prefix.