2

I am trying, without luck (getting 404s), to get nginx to serve static files for requests under a certain subdirectory, while all other requests are reversed-proxied. For example, I want requests to http://example.com/project/static/index.html, and http://example.com/project/static/subdir/file2.html to map to /var/www/example.com/htdocs/index.html and /var/www/example.com/htdocs/subdir/file2.html respectively.

All other requests should be reversed-proxied. For example, http://example.com/project/thisWillBeProxied.html and http://example.com/project/subdir/soWillThis.html

Here is my active nginx profile

server {
    listen   8080;
    server_name  example.com;
    access_log  /var/www/example.com/log/nginx.access.log;
    error_log  /var/www/example.com/log/nginx_error.log debug;

    location / {
            proxy_pass         http://reverse-proxy-host/;
    }

    location ^~ /project/static/ {
            alias   /var/www/example.com/htdocs;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root   /var/www/nginx-default;
    }
}

2 Answers 2

1

The error log, helps explain what is going on.

Your config:

location ^~ /project/static/ {
    alias   /var/www/example.com/htdocs;
}

it does exactly what you have wrote. It replace /project/static/ in your URI to the file path /var/www/example.com/htdocs. So, if request looks like http://example.com/project/static/index.html then nginx will try to open /var/www/example.com/htdocsindex.html. I assume that you don't want to serve /var/www/example.com/htdocsindex.html but you want to serve /var/www/example.com/htdocs/index.html then you should write:

location ^~ /project/static/ {
    alias   /var/www/example.com/htdocs/;
}

Documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm going to try to use this: http://wiki.nginx.org/HttpCoreModule#error_page

Read from "If there is no need to change URI during redirection it is possible to redirect processing of error pages into a named location"

I won't have to use a static directory this way. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.