0

I'm using NginX 1.4.6 on Windows, and there's a server block (the only server block) in my nginx.conf which is defined as below:

server {
    listen       8000;
    server_name  localhost;
    access_log  logs/host.access.log  main;

    location / {
        root D:\Git\SNHAutomationRuby\output\screenshots;
    }

    location ~* /img/.*$ {
        root D:\Git\SNHAutomationRuby\output\screenshots;
    }
}

I have some png screenshots in folder D:\Git\SNHAutomationRuby\output\screenshots, and with this configuration, the image
D:\Git\SNHAutomationRuby\output\screenshots\20140313-08-35-02-108466000.png
can be successfully loaded in Firefox by accessing
http://localhost:8000/20140313-08-35-02-108466000.png

However, nginx retuened a 404 when I access
http://localhost:8000/img/20140313-08-35-02-108466000.png
I assume that there's something wrong in the location block

location ~* /img/.*$ {
    root D:\Git\SNHAutomationRuby\output\screenshots;
}

Is the regular expression /img/.*$ incorrect so that it cannot match http://localhost:8000/img/20140313-08-35-02-108466000.png? Or is there any other configuration I've done incorrectly?

Bruce Sun
  • 631
  • 1
  • 10
  • 26

2 Answers2

0

After taking a look at the error.log, I see what was happening now.
The error is:
2014/03/14 09:52:03 [error] 1756#1312: *1 CreateFile() "D:\Git\SNHAutomationRuby\output\screenshots/img/20140313-08-35-02-108466000.png" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /img/20140313-08-35-02-108466000.png HTTP/1.1", host: "localhost:8000"
I realized that I should replace "\" in the root specification with "/", like:

location ~* /img/(.+)$ {
    root D:/Git/SNHAutomationRuby/output/screenshots;
}

But it still doesn't work. The error in error.log is:
2014/03/14 13:15:00 [error] 5144#3480: *1 CreateFile() "D:/Git/SNHAutomationRuby/output/screenshots/img/AccountLevelAttendeeList_ColumnHeaders-527-20140313-10-03-48-904461000.png" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /img/AccountLevelAttendeeList_ColumnHeaders-527-20140313-10-03-48-904461000.png HTTP/1.1", host: "localhost:8000"
Well, that png is located at D:/Git/SNHAutomationRuby/output/screenshots/ but not D:/Git/SNHAutomationRuby/output/screenshots/img/ so I got this error. Then I give it a simple fix with a redirect, like:

location ~* /img/(.+)$ {
    return 301 ../$1;
}

It works!

So, I didn't write an incorrect regular expression but an invalid path specification, besides, I should use a redirection rather than a root specification in the second location block.

Bruce Sun
  • 631
  • 1
  • 10
  • 26
0

You could use alias or rewrite directive instead of redirect. And there is no need for regexp. And root directive inside location is bad practice.

server {
    listen       8000;
    server_name  localhost;
    access_log  logs/host.access.log  main;
    root D:/Git/SNHAutomationRuby/output/screenshots;

    location / {
    }

    location /img/ {
        rewrite ^/img(.+) $1 last;
    }
    # or
    location /img/ {
        alias D:/Git/SNHAutomationRuby/output/screenshots;
    }
}
Alexey Ten
  • 13,794
  • 6
  • 44
  • 54
  • I try them both, the 1st one using `rewrite` works, but the second is wrong. According to [here](http://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias), I change it to `alias D:/Git/SNHAutomationRuby/output/screenshots/;` (NOTE the trailing slash) then it works. – Bruce Sun Mar 17 '14 at 05:06