0

I'm trying to pass all traffic at location ^/[a-z0-9]{24}$to index.html at a different root directory. I have my config set up like so:

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {

    listen 443 ssl;

    server_name example.com;
    root /Users/me/sites/store_front/static;
    index index.html;

    try_files $uri $uri/ $uri.html =404;

    location ~ "^/[a-z0-9]{24}$" {
        alias /Users/me/web_app/static;
        try_files $uri index.html;
    }
}

For some reason when I curl this url I get a 404:

$ curl -I https://example.com/55c898e7faa137f42409d403

HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Content-Type: text/html; charset=UTF-8
Content-Length: 168
Connection: keep-alive

Does anyone know how to get this alias working?

update:

One caveat to this is that I need all the relative urls in index.html to load correctly:

<link href="/styles/main.css" rel="stylesheet" type="text/css"
<script src="/javascript/main.js"></script>

These files actually exist in the web_app directory but nginx tries to load them from store_front

Thanks

2

Pointing ^/[a-z0-9]{24}$ to index.html is easy. The resource files /styles/main.css and /javascript/main.js need to have unique URIs otherwise nginx does not know which files to serve.

If the /styles and /javascript prefix is also used by the store_front, you will need to isolate the individual files:

location = /styles/main.css {
    root /Users/me/web_app/static;
}
location = /javascript/main.js {
    root /Users/me/web_app/static;
}
location ~ "^/[a-z0-9]{24}" {
    root /Users/me/web_app/static;
    rewrite ^ /index.html break;
}

If the /styles, /javascript and ^/[a-z0-9]{24}$ URIs are all unique, you might combine the above into a single location:

location ~ "^/([a-z0-9]{24}|styles|javascript)" {
    root /Users/me/web_app/static;
    rewrite "^/[a-z0-9]{24}$" /index.html break;
}

Or even:

location ~ "^/([a-z0-9]{24}|styles/main.css|javascript/main.js)" {
    root /Users/me/web_app/static;
    rewrite "^/[a-z0-9]{24}$" /index.html break;
}
  • Is it a good idea to use multiple roots? I thought alias was best practice in a nested locations. – jwerre Jun 9 '16 at 16:29
  • That seems to work, index.html load, but none of my scripts or styles for index.html load since they relative to the index page. – jwerre Jun 9 '16 at 16:36
  • Answer updated: the terminating $ on the location regex has been removed to match the URI of any relative resource files. And another rewrite has been added to handle these resource files. – Richard Smith Jun 9 '16 at 17:02
  • This is so close, thanks for your help with this. Unfortunately, my static files aren't loading still. Wouldn't something like this: rewrite "^/(styles|javascript)(/.*)" $2 break; be more appropriate since index.html is being is located at https://example.com/55c898e7faa137f42409d403 – jwerre Jun 9 '16 at 17:30
  • 1
    URLs need to be unique. – Richard Smith Jun 9 '16 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.