0
votes

I am making transition from apache to nginx, and i can not get the alias to serve images. Reading nginx documentation, diving in google and what not have not yielded any acceptable results for me ;(

The images is located C:/xampp/htdocs/content/XYZ/thumbn/image.jpg

the XYZ part is dynamic and changes.

Nginx is set up to serve content from root C:/nginx/public_html/domain.dev/

In the apache i had simply made Alias /CDN "C:\xampp\htdocs\content"

So that way when ever i request www.domain.dev/CDN/XYZ/thumbn/image.jpg i would get the image from the alias directory.

So im trying to replicate the same in nginx

location /CDN {
alias C:/xampp/htdocs/content;
autoindex on; # just there to see if it works.
}

Unfortunately when i access the image www.domain.dev/CDN/XYZ/thumbn/image.jpg i get 404. In the Nginx logs i see following record :

2017/07/04 16:43:32 [error] 10532#6488: *4 CreateFile() "C:/nginx/public_html/domain.dev/CDN/XYZ/thumbn/two.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: www.domain.dev, request: "GET /CDN/XYZ/thumbn/two.jpg HTTP/1.1", host: "www.domain.dev"

it is how ever listing the directory contents.

Now if i change the location block to this

    location ^~ /CDN/ {
        alias C:/xampp/htdocs/content/;
    } 

I get the same error in log, only way i managed to get it working (but not in the way i need) was when i use this location block

    location ^~ /CDN/ {
        alias C:/xampp/htdocs/content/XYZ/thumbn/;
    } 

Then it is showing the image, but the url for the image is different and not valid for my project.

Now the question: how do i serve only images from multiple static directories under same alias? the images are located in following structure

C:/xampp/htdocs/content/one/thumbn/image.jpg
C:/xampp/htdocs/content/two/thumbn/cats.jpg
C:/xampp/htdocs/content/three/thumbn/dogs.jpg
C:/xampp/htdocs/content/another_random_direcotry/thumbn/random.jpg

And they should be accessed by URL as follows

www.domain.dev/CDN/one/thumbn/image.jpg
www.domain.dev/CDN/two/thumbn/cats.jpg
www.domain.dev/CDN/three/thumbn/dogs.jpg
www.domain.dev/CDN/another_random_direcotry/thumbn/random.jpg

Is this even possible with Nginx? so how do i achieve this? Is regex required in this occasion?

full config file here

2
forgot to mention im running win 10 (production site will be on windows 2012 r2) and nginx-1.13.1.zip - Ugis

2 Answers

0
votes

try this

location /CDN/ {
        alias C:/xampp/htdocs/content/;
} 
0
votes

The problem was caused by

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 7d;
    }

When i comment out this location directive i can access the images from alias directory.

This how ever removes expiration header for images and with this enabled i can not access the images.