NGINX convert HEAD to GET requests

nginx

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/;
    }
}

Best Answer

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/;
    }
}