Nginx location, alias, rewrite, root

20,342

Solution 1

You can do this; but it's slightly esoteric. Try using:

location ^~ /foo/bar {
    alias /var/www/mystatic/;
}

location / {
    proxy_pass ....;
}

These options are documented on the Wiki http://wiki.nginx.org/NginxHttpCoreModule#location

Solution 2

location = /foo/bar/baz.swf {} 

will clear clear any options set to /foo/bar/baz.swf. So you can leave it where it is as the proxy options will not be used.

Share:
20,342
Skylar Saveland
Author by

Skylar Saveland

Updated on July 09, 2022

Comments

  • Skylar Saveland
    Skylar Saveland almost 2 years

    I'm serving /foo/bar/ by way of proxypass and want to continue doing so. However, I would like to serve /foo/bar/baz.swf statically from say /var/www/mystatic/baz.swf and so forth.

    I was hoping that I could do something like

        location /foo/bar/(.*) {
          alias /var/www/mystatic/;
        }
    
        location / {
          proxy_pass ....;
          ... 
        }
    

    And /foo/bar/ would go to the application server while /foo/bar/(.*) would be served statically.

    the docs say that I can't do this and need to use a combination of root and rewrite: http://wiki.nginx.org/NginxHttpCoreModule

    Adding to the complication, I would like to continue using the ancient, unsupported 0.5.33. Any help would b greatly appreciated.

    Edit: moving forward, someone suggested using root instead of alias. But, it doesn't seem that I can use any regex on the location directive with my version? Here, /foo/bar/baz.swf is served by the proxy_pass! I have the file at /var/www/foo/bar/baz.swf.

        location /foo/bar/(.+) {
          root /var/www/;
        }