Nginx http_accept 无法映射到用于内容协商的变量
Nginx http_accept cannot be mapped to variable for content-negotiation
目标是使用 -
将 Content-Type
值映射到文件类型
# nginx.conf
map $http_accept $suffix {
"~*turtle" ".ttl";
default "DEFAULT";
}
由 -
处理
# sites-available/site
location /ontologies {
root /folder;
add_header Vary Accept;
add_header X-debug-accept "$http_accept";
add_header X-debug-suffix "$suffix";
try_files $uri$suffix $uri =404;
}
然而,响应总是给出-
# curl -I -H "Content-type: text/turtle" URL
http_accept = */*
suffix = DEFAULT, which is actually (blank)
您的问题是访问错误header。您应该使用 $http_content_type
而不是 $http_accept
# curl -v -I -H "Content-type: text/turtle" URL
* Trying ::1...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: text/turtle
如您所见,您通过了 Content-Type
而不是 Accept
。因此,您需要根据 header 使用正确的变量。 $http_content_type
或 $http_accept
目标是使用 -
将Content-Type
值映射到文件类型
# nginx.conf
map $http_accept $suffix {
"~*turtle" ".ttl";
default "DEFAULT";
}
由 -
处理# sites-available/site
location /ontologies {
root /folder;
add_header Vary Accept;
add_header X-debug-accept "$http_accept";
add_header X-debug-suffix "$suffix";
try_files $uri$suffix $uri =404;
}
然而,响应总是给出-
# curl -I -H "Content-type: text/turtle" URL
http_accept = */*
suffix = DEFAULT, which is actually (blank)
您的问题是访问错误header。您应该使用 $http_content_type
而不是 $http_accept
# curl -v -I -H "Content-type: text/turtle" URL
* Trying ::1...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: text/turtle
如您所见,您通过了 Content-Type
而不是 Accept
。因此,您需要根据 header 使用正确的变量。 $http_content_type
或 $http_accept