kai
kai

Reputation: 68

nginx, alias and location

I have in nginx a config file like

server {
    root /var/www/releaser/site/web/;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    location ^~ /images/ {
        alias /var/www/releaser/site/web/img/;
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    include snippets/nginx-basics.conf;

    error_log  /var/log/nginx/site-error.log;
    access_log /var/log/nginx/site-access.log;

    server_name site.test;
}

So, when I do this (the extra headers for images are missing):

$ curl -I site.test/images/one.jpg
HTTP/1.1 200 OK
Server: nginx/1.9.3 (Ubuntu)
Date: Tue, 10 Nov 2015 05:29:59 GMT
Content-Type: image/jpeg
Content-Length: 185547
Last-Modified: Fri, 24 Jul 2015 22:12:20 GMT
Connection: keep-alive
Accept-Ranges: bytes

But, if I do this (the extra headers for images are included):

$ curl -I site.test/img/one.jpg
HTTP/1.1 200 OK
Server: nginx/1.9.3 (Ubuntu)
Date: Tue, 10 Nov 2015 05:29:36 GMT
Content-Type: image/jpeg
Content-Length: 185547
Last-Modified: Fri, 24 Jul 2015 22:12:20 GMT
Connection: keep-alive
Expires: Thu, 10 Dec 2015 05:29:36 GMT
Cache-Control: max-age=2592000
Cache-Control: public
Accept-Ranges: bytes

How can I fix this without putting a copy of the extra headers inside the alias' location section?

Thanks a lot!

Upvotes: 1

Views: 1623

Answers (1)

Richard Smith
Richard Smith

Reputation: 49742

One solution is to replace the alias directive with a rewrite directive:

location ^~ /images/ {
  rewrite ^/images(.*)$ /img$1;
}

Upvotes: 2

Related Questions