20

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?

2 Answers 2

25

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; } } 
Sign up to request clarification or add additional context in comments.

5 Comments

That did it! Still confused why root works differently here, but this is what I need. Thanks Nate!
Not totally sure, I was just going by documentation: 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!
Hmm, that actually does make sense now that I think about it. Thanks!
exactly what I was looking for!
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 } 

2 Comments

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...
Looking at this more, it might be a problem with openresty + os x el capitan and how it's all set up. :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.