I am using CURL to test HEAD requests to my nginx server. The file being served is a simple PHP
file.
If I use GET
:
$ curl -XGET http://test.com/phpinfo.php -I
HTTP/1.1 200 OK
Date: Tue, 09 Apr 2013 00:35:35 GMT
Content-Type: text/html
Connection: keep-alive
Content-Length: 72080
However, if I use HEAD
:
$ curl -XHEAD http://test.com/phpinfo.php -I
HTTP/1.1 200 OK
Date: Tue, 09 Apr 2013 00:37:00 GMT
Content-Type: text/html
Connection: keep-alive
Why is it that if the request is HEAD
, nginx omits the Content-Length
header? The php script is very simple and is not responding to HEAD
in any special way.
Is there any option I can turn on in nginx, so that it will send the Content-Length
for HEAD
as well?
Relevant nginx info:
nginx version: nginx/1.2.8
built by gcc 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.2.8 --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-pcre --conf-path=/etc/nginx/nginx.conf --add-module=../headers-more-nginx-module-0.19rc1
Configuration:
user www-user;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 10M;
sendfile on;
keepalive_timeout 65;
more_clear_headers "Server";
gzip on;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
server {
server_name test.com;
root /www;
index index.php index.html index.htm;
listen 80 default_server;
rewrite ^/(.*)/$ /$1 permanent; #remove trailing slash
#charset koi8-r;
#access_log logs/host.access.log main;
include general/*.conf;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
One reason that
nginx
probably doesn't include theContent-Length
response header for an HTTPHEAD
request is that, by definition, the response to aHEAD
request must not contain a message body in the response (see RFC 2616 for more details).Now, an HTTP server could send
Content-Length: 0
for responses toHEAD
requests, but that's additional information over the wire that is not necessarily needed. I suspect, then, thatnginx
is simply omitting an otherwise redundant response header by not including theContent-Length
header in the response to yourHEAD
requests.Hope this helps!