277

I have configured Apache to send back a 200 response without serving any file with this configuration line

Redirect 200 /hello

Can I do this with Nginx? I don't want to serve a file, I just want the server to respond with a 200 (I'm just logging the request).

I know I can add an index file and achieve the same thing, but doing it in the config means there's one less thing that can go wrong.

Theo
  • 3,025

6 Answers6

512

Yes, you can

location / {
    return 200 'gangnam style!';
    # because default content-type is application/octet-stream,
    # browser will offer to "save the file"...
    # if you want to see reply in browser, uncomment next line 
    # add_header Content-Type text/plain;
}
cadmi
  • 7,787
60

If you want to return a formatted HTML text, without serving a HTML file:

location / {
    default_type text/html;
    return 200 "<!DOCTYPE html><h2>gangnam style!</h2>\n";
}

If you want to return a text without html format, as the answer points:

location / {
    add_header Content-Type text/plain;
    return 200 'gangnam style!';
}

And if you just simply wants to return 200:

location / {
    return 200;
}

Just to remember: location blocks go inside server blocks. Here's a doc for more information about the topic.

P.S.: I have similar configuration (formatted html) running on plenty of servers.

ivanleoncz
  • 1,861
33

You do need to use a 204 as Nginx will not allow a 200 with no response body. To send a 204 you simply use the return directive to return 204; in the appropriate location.

9

To complete @Martin Fjordval's answer, be careful if you're using such a configuration to do a healthcheck.

While a 204 HTTP code is semantically perfect for a healthcheck (success indication with no content), some services do not consider it a success.

Namely, I had the issue with Google Cloud Load-balancers.

toadjaune
  • 581
6

As per status code definitions, I believe you want it to be a 204, and not 200. 200's need to be with a resource in the response, or I'd suspect most sane browsers would get confused by this. The other one you can use is 304, which is for cached content.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

sandroid
  • 1,724
3

Expanding on the @cadmi answer for a specific case of Kubernetes ingress-nginx controller configuration.

In my case, I could only append the small part of the Nginx configuration inside the location directive.

I wanted to add a custom 404 error JSON message for a specific if directive. Since default_type is not allowed inside the if directive I came up with adding the "blank" default_type outside the if directive and add_header inside the if directive:

http {
    default_type text/html; # can't change this
    ...
    server {
        ...
        location / {
            ...
            # This part of configuration is something I can change (generated by ingress-nginx controller `ingress.kubernetes.io/configuration-snippet` annotation)
            default_type "";
            # Custom 404 JSON page returned for the call to my.specific.host
            if ($host = my.specific.host) {
                return 404 '{\n  "status": "404",\n  "message": "Not Found",\n  "details": "Please call other hosts."\n}\n';
                add_header Content-Type "application/json" always;
            }
        }
    }
}

Without the default_type ""; an add_header directive was adding the second Content-Type header instead of changing the default one.