Josiah
Josiah

Reputation: 423

Nginx mapping for static files and reverse proxy

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;
    }
}

Upvotes: 2

Views: 5976

Answers (2)

VBart
VBart

Reputation: 15110

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.

Upvotes: 1

user988346
user988346

Reputation: 1888

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. :)

Upvotes: 0

Related Questions