nginx、别名和位置
nginx, alias and location
我在 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;
}
所以,当我这样做时(缺少图像的额外 headers):
$ 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
但是,如果我这样做(包括图像的额外 headers):
$ 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
如何在不将额外 headers 的副本放入别名位置部分的情况下解决此问题?
非常感谢!
一个解决方案是用 rewrite
指令替换 alias
指令:
location ^~ /images/ {
rewrite ^/images(.*)$ /img;
}
我在 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;
}
所以,当我这样做时(缺少图像的额外 headers):
$ 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
但是,如果我这样做(包括图像的额外 headers):
$ 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
如何在不将额外 headers 的副本放入别名位置部分的情况下解决此问题?
非常感谢!
一个解决方案是用 rewrite
指令替换 alias
指令:
location ^~ /images/ {
rewrite ^/images(.*)$ /img;
}