18

I have files at /Users/me/myproject and I want to serve them at http://stuff.dev/something using a simple nginx config. In the /Users/me/myproject folder is something like:

index.html
scripts/
    app.js
styles/
    style.css

So I really want to be able to access http://stuff.dev/something, http://stuff.dev/something/scripts/app.js etc.

In my nginx conf I have this:

server {
  listen      80;
  server_name stuff.dev;

  location /something {
    index  index.html;
    root /Users/me/myproject;
  }
}

This doesn't work (I get a 404 if I try to go to those above URLs), however if I try the exact same set up but using location / { instead of location /something {, it works fine. How can I serve this directory of files statically but at a path instead of at the location root? Do I have to have the files in a folder called "something" like /Users/me/myproject/something for this to work? If so is there a way around that?

rhodesjason
  • 4,904
  • 9
  • 43
  • 59

2 Answers2

23

I tried this out on my VPS, and using the alias command worked for me:

server {
  listen 80;
  server_name   something.nateeagle.com;

  location /something {
    alias /home/neagle/something;
    index index.html index.htm;
  }
}
Nate
  • 4,718
  • 2
  • 25
  • 26
  • 1
    That did it! Still confused why `root` works differently here, but this is what I need. Thanks Nate! – rhodesjason Nov 30 '15 at 18:51
  • 3
    Not totally sure, I was just going by documentation: http://nginx.org/en/docs/http/ngx_http_core_module.html#root "A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used." Glad this helped! – Nate Nov 30 '15 at 18:54
  • 1
    Hmm, that actually does make sense now that I think about it. Thanks! – rhodesjason Nov 30 '15 at 19:27
  • 1
    For further reference: https://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias – shlgug Sep 04 '17 at 22:10
  • exactly what I was looking for! – Cas Dec 16 '17 at 23:14
3

I believe you can accomplish this with the try_files directive, something like this should work:

server {
  listen      80;
  server_name stuff.dev;

  location /something {
    root /Users/me/myproject;
    index  index.html;

    try_files /Users/me/myproject/$uri /Users/me/myproject/$uri/ =404
  }
}

That might require you to shove files under /Users/me/myproject/something, and if you can't get away with that, you'll need to try something like the following for the location:

  location ~ /something(.*) {
    root /Users/me/myproject;
    index  index.html;

    try_files /Users/me/myproject$1 /Users/me/myproject$1/ =404
  }
awinder
  • 513
  • 4
  • 4
  • Interesting... I'll try this in a minute, curious what the point of "root" is if you have to mention the whole path anyway... I assumed try_files would be relative to the scoped root value... – rhodesjason Nov 29 '15 at 23:30
  • Looking at this more, it might be a problem with openresty + os x el capitan and how it's all set up. :( – rhodesjason Nov 30 '15 at 00:28