1

I'm having a heck of a time getting this (hopefully simple) solution implemented.

We have a site at site.com and served from /home/site/public. We need a specific SUBDIRECTORY of site.com (site.com/gps) to be served from a DIFFERENT document root to avoid security implications. IE site.com/gps should be served from /home/sitegps/public. I have implemented the following location block, but it just results in an http 500 due to infinite redirects. I'm hoping that someone has done this before and knows where I'm going wrong...

# Send all /gps requests to new root
location ~ ^/gps(?:/(.*))?$ {
    alias /home/sitegps/public;
    try_files $uri $uri/ /gps/index.php?$uri&$args;
}
4
  • If PHP files are in this alternate root, you should use a nested location. See my answer here Oct 29, 2018 at 18:11
  • @RichardSmith there will eventually be PHP files here and I was planning on adding a nested location to accommodate them. Right now I'm just trying to get the second docroot to work with a static HTML file...
    – Kevin
    Oct 29, 2018 at 18:23
  • If you must use a regular expression location with alias, then you need to construct the full path to the file. See the manual for details. Oct 29, 2018 at 19:06
  • Why are you even using a regex location at all? It doesn't look like you're using the capture anywhere. Oct 29, 2018 at 19:49

1 Answer 1

1

Instead of alias, use can use the root directive inside the location block.

I think you can simplify your location match by just using /gps as well.

location /gps {
    root /home/sitegps/public;
    try_files $uri $uri/ /gps/index.php?$uri&$args;
}

This will not rewrite the request and will expect to match files in a directory called gps like so: /home/sitegps/public/gps/. I wasn't sure if this was required.

Update

Working with the assumption that you do not want to have the gps directory in the /home/sitespg/public directory, I tested out using alias and came up with this config:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html;

    server_name _;

    location / {
            try_files $uri $uri/ =404;
    }

    location /gps/ {
            alias /var/www/gps;
            try_files $uri $uri/ =404;
    }
}

I believe the alias will do what you want but either they regex location match is causing problems or there is something else amiss.

With the trailing slash on /gps/ you will avoid matching paths like /gpsport, but you will need to either rewrite /gps to /gps/ or match /gps exactly with a location block.

rewrite:

rewrite ^/gps$ /gps/

location:

location = /gps {
    alias /var/www/gps;
    try_files $uri $uri/ =404;
}

No doubt there are more variations that will also work.

6
  • Yes, that's what I started with and I was still experiencing the infinite redirects/500 errors. Do you have a block example that will match site.com/gps, site.com/gps/, and site.com/gps/* and will serve up files from /home/sitegps/public? If so, I'd love to try it out to see if it works in our situation.
    – Kevin
    Oct 29, 2018 at 19:02
  • Actually, just changing the location to /gps like I have above and keeping the alias like you have might work.
    – virullius
    Oct 29, 2018 at 20:10
  • Updated with tested config.
    – virullius
    Oct 29, 2018 at 20:40
  • Both the location and alias should end with a slash. Otherwise they will match unwanted paths, such as /gps2. Oct 30, 2018 at 0:22
  • As Michael Hampton said, the location and alias both need a trailing slash or the location will match URLs that it shouldn't. Otherwise, this implementation works great. No idea why mine wasn't working, I feel like my first attempt was eerily similar to this one. Update your answer to include the trailing slashes and I'll accept it. Thanks for your help!
    – Kevin
    Oct 30, 2018 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .