0

I'm trying to serve static content from an endpoint that differs in hierarchy structure than the actual root directory structure. This is the result I'm trying to achieve:

0.0.0.0/Claw/scripts/main.js -> /home/ubuntu/Claw/public/scripts/main.js

I'm using the following Nginx configuration:

location ~ ^/Claw/(images/|styles/|scripts/) {
    root /home/ubuntu/Claw/public;
    access_log off;
    expires max;
}

This is failing as /home/ubuntu/Claw/public/Claw/scripts/main.js doesn't exist. Therefore I need to remove the prefixed Claw from the location internally. How can I go about doing this?

I want this structure so I can host multiple Node apps from different endpoints on the same domain.

Brodie
  • 105
  • 1
  • 7

2 Answers2

1

Try this:

location ~ ^/Claw/(?<subdir>images|styles|scripts)/(?<file>.*) {
    alias /home/ubuntu/Claw/public/$subdir/$file;
    access_log off;
    expires max;
}

It seems that nginx doesn't pass variables captured into numbered variables from location directive to inside the block. When the variables are given names, then they work.

Brodie
  • 105
  • 1
  • 7
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • I've added the working config - you were halfway there, thanks for your help :) – Brodie Aug 30 '14 at 10:49
  • It could have also worked without the slashes in the end of the $subdir parts, because nginx appends the rest of the URI anyway after the `alias` directive. Good that it works now anyway. – Tero Kilkanen Aug 30 '14 at 12:58
0

You could use alias directive for this.

location ~ ^/Claw/(images|styles|scripts)/ {
    alias /home/ubuntu/Claw/public/$1/;
    access_log off;
    expires max;
}
Alexey Ten
  • 8,435
  • 1
  • 34
  • 36