Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm pretty new to nginx configs. I'm having some difficulty with a pretty basic problem. I'd like to host some static files at /doc (index.html, some images, etc). The files are located in a directory called /sites/mysite/proj/doc/.

The problem is, that with the nginx config below, nginx tries to look for a directory called "/sites/mysite/proj/doc/doc". Perhaps this can be fixed by setting the root to /sites/mysite/proj/, but I don't want to potentially expose other (non-static) assets in the proj/ directory. And for various reasons, I can't really move the doc/ directory from where it is.

I think there is a way to use a Rewrite rule to solve this situation, but I don't really understand all the parts, so having some difficulty formulating the rule.

rewrite ^/doc/(.*)$ /$1 permanent;

I've also included a working example of hosting files out of a /sites/mysite/htdocs/static/ directory.

> vim locations.conf

location /static {
    root   /sites/mysite/htdocs/;
    access_log   off;
    autoindex  on;

location /doc {
    root   /sites/mysite/proj/doc/;
    access_log   on;
    autoindex  on;

2011/11/19 23:49:00 [error] 2314#0: *42 open() "/sites/mysite/proj/doc/doc" failed (2: No such file or directory), client:, server: , request: "GET /doc HTTP/1.1", host: ""

Does anyone have any ideas how I might go about serving this static content?

share|improve this question
up vote 17 down vote accepted

You could use alias, just like this:

location /doc {
    alias   /sites/mysite/proj/doc/;

share|improve this answer
It's better to use root instead according to the doc you referred to (see "When location matches the last part of the directive’s value") – Rafael Xavier May 9 at 18:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.