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.

2 Answers 2

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.

2
  • I've added the working config - you were halfway there, thanks for your help :)
    – Brodie
    Aug 30, 2014 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. Aug 30, 2014 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;
}
5
  • This isn't working: trying /Claw/scripts/main.js is resulting in Claw/scripts/main.js/ and a 403 error.
    – Brodie
    Aug 28, 2014 at 15:27
  • Show full nginx config
    – Alexey Ten
    Aug 28, 2014 at 15:36
  • Sure: pastebin.com/B6hUM6za
    – Brodie
    Aug 28, 2014 at 15:46
  • It should work nginx.org/r/alias. What's in nginx error log?
    – Alexey Ten
    Aug 28, 2014 at 15:58
  • 2014/08/28 16:59:12 [error] 21128#0: *5 directory index of "/home/ubuntu/Etch/public/scripts/" is forbidden, client: 0.0.0.0, server: _, request: "GET /Etch/scripts/main.js/ HTTP/1.1", host: "0.0.0.0"
    – Brodie
    Aug 28, 2014 at 15:59

You must log in to answer this question.

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