nginx,别名和位置

时间:2015-11-10 05:47:16

标签: image nginx location alias

我在nginx中有一个像

这样的配置文件
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;
}

所以,当我这样做时(缺少图像的额外标题):

$ 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

但是,如果我这样做(包括图像的额外标题):

$ 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

如何在不在别名内添加额外标题的副本的情况下解决此问题。位置部分?

非常感谢!

1 个答案:

答案 0 :(得分:1)

一种解决方案是用alias指令替换rewrite指令:

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