nginx serve subdirectory as domain root

Say you have the following file structure:

1
2
3
4
5
site.com/
  main_site/
  site_a/
  site_b/
  site_c/

If you want:

  • http://site.com to show /main_site/’s files
  • all remaining sites to be served as usual (example: http://site.com/site_b/)

All you have to do is to edit site.com.conf’s file as follows:

1
2
3
4
5
6
7
8
9
10
11
  location / {
    try_files $uri $uri/ @missing; # try to get site.com/requested_file, if does not exist jump to @missing
  }

  location /main_site {
    # do nothing
  }

  location @missing { # if file is not present on the root, serve the /main_site
    rewrite ^ /main_site$request_uri?;
  }

Source

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>