nginx,别名和位置
Posted
技术标签:
【中文标题】nginx,别名和位置【英文标题】:nginx, alias and location 【发布时间】:2016-02-10 23:03:25 【问题描述】:我在 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】:一种解决方案是将alias
指令替换为rewrite
指令:
location ^~ /images/
rewrite ^/images(.*)$ /img$1;
【讨论】:
它就像一个魅力。你能解释一下什么时候使用别名好,什么时候重写是合适的? @kai 我发现alias
的范围非常有限。你的例子是教科书,除了额外的标题约束。以上是关于nginx,别名和位置的主要内容,如果未能解决你的问题,请参考以下文章