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.
Yes, you can
If you want to return a formatted HTML text, without serving a HTML file:
If you want to return a text without html format, as the answer points:
And if you just simply wants to return 200:
Just to remember:
location
blocks go insideserver
blocks. Here's a doc for more information about the topic.P.S.: I have similar configuration (formatted html) running on plenty of servers.
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.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.
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
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. Sincedefault_type
is not allowed inside theif
directive I came up with adding the "blank"default_type
outside theif
directive andadd_header
inside theif
directive:Without the
default_type "";
anadd_header
directive was adding the secondContent-Type
header instead of changing the default one.