Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Due to some terrible design decisions, we have an application unable to respond to HTTP HEAD requests (Returns 'Method Not Allowed'). Modifying the software to return HEAD requests correctly would be tricky, not impossible but extra work. The application sits behind an NGINX proxy, I was wondering if there was a way to get NGINX to convert HEAD requests it received from clients into GET requests to the back-end, then discard the response except for the headers and send it back to the client as though our application servers were able to respond to HEAD requests.

Current config (fairly standard)

upstream ourupstream{
    server unix:/var/apps/sockets/ourapp.socket.thread1
    server unix:/var/apps/sockets/ourapp.socket.thread2
    server unix:/var/apps/sockets/ourapp.socket.thread3
    [like 20 of these]
}

server {
    listen       1.2.3.4:80;
    server_name  ourapp;

    access_log  /var/apps/logs/ourapp.nginx.plog    proxy;
    error_log   /var/apps/logs/ourapp.nginx.elog    info;

    gzip on;

    gzip_types  text/plain text/html;

    proxy_intercept_errors on;
    proxy_connect_timeout 10;
    proxy_send_timeout 10;
    proxy_read_timeout 10;
    proxy_next_upstream error timeout;
    client_max_body_size 2m;

    error_page 404 /static/404.html;
    error_page 500 501 502 503 504 =500 /static/500.html;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://ourupstream/;
    }

    location /static/ {
        root /var/apps/global/;
    }
}
share|improve this question
    
How are you passing the request to the backend? proxy_pass? fastcgi_pass? Can you paste your current config? Which version of nginx are you running? –  kolbyjack Jan 7 '12 at 15:18
    
@kolbyjack proxy_pass using unix sockets, NGINX 1.1.12 and 1.0.11 in dev, 1.0.11 in production (We're moving production to 1.1.12 soon) –  sam Jan 7 '12 at 15:31
add comment

1 Answer

up vote 3 down vote accepted

Since you're using proxy_pass, I think you'll have to do use some error_page abuse (and since you're already using error_pages, you'll also need to enable recursive error pages). I think this will work for you:

upstream ourupstream{
    server unix:/var/apps/sockets/ourapp.socket.thread1
    server unix:/var/apps/sockets/ourapp.socket.thread2
    server unix:/var/apps/sockets/ourapp.socket.thread3
    [like 20 of these]
}

server {
    listen       1.2.3.4:80;
    server_name  ourapp;

    access_log  /var/apps/logs/ourapp.nginx.plog    proxy;
    error_log   /var/apps/logs/ourapp.nginx.elog    info;

    gzip on;

    gzip_types  text/plain text/html;

    proxy_intercept_errors on;
    proxy_connect_timeout 10;
    proxy_send_timeout 10;
    proxy_read_timeout 10;
    proxy_next_upstream error timeout;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 2m;

    error_page 404 /static/404.html;
    error_page 500 501 502 503 504 =500 /static/500.html;

    location @force_get {
        recursive_error_pages on;
        proxy_method GET;
        proxy_pass http://ourupstream;
    }

    location / {
        error_page 550 = @force_get;
        if ($request_method = HEAD) { return 550; }
        proxy_pass http://ourupstream;
    }

    location /static/ {
        root /var/apps/global/;
    }
}
share|improve this answer
    
This still seems to return the body to the client, even when request with head. Is there any way to strip/ignore the body content? –  sam Jan 8 '12 at 5:39
    
I was hoping it would remember that the original request was HEAD and drop the body. I can't think of another way to make that happen, sorry. –  kolbyjack Jan 8 '12 at 13:04
    
Oh well, does close enough to what we need anyway –  sam Jan 8 '12 at 13:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.