Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a directory of static files at a certain location path with nginx?

Tags:

nginx

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?

like image 467
rhodesjason Avatar asked Nov 29 '15 22:11

rhodesjason


People also ask

Where does nginx find static files?

Root Directory and Index Files mp3 or . mp4 extension, NGINX instead searches for the file in the /www/media/ directory because it is defined in the matching location block. You can list more than one filename in the index directive. NGINX searches for files in the specified order and returns the first one it finds.

How do I use nginx as a file server?

Serving static files using nginx as web server is a good option. For making the static files available you need to copy your testfolder to /usr/share/nginx/html inside the nginx image. After which you will be able to see the files on your browser on port 8080.

How do you serve a static file?

To serve static files for Go 1.12+ in the standard environment, you define the handlers in your app. yaml file using either the static_dir or static_files elements. The content in the static files or static directories are unaffected by the scaling settings in your app.


2 Answers

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;
  }
}
like image 173
Nate Avatar answered Sep 18 '22 14:09

Nate


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
  }
like image 41
awinder Avatar answered Sep 21 '22 14:09

awinder