- How to reply with 200 from Nginx, without serving a file?
- Solution 3
- Solution 4
- Solution 5
- Related videos on Youtube
- fearoffours
- Comments
- NGINX: How to return inline html?
- Return 200 by Nginx without serving a file
- NGINX: How to return inline html?
- Nginx — how to create /status with stub_status
- Nginx post method does not work
- How to configure nginx to return text or json
How to reply with 200 from Nginx, without serving a file?
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.
Solution 3
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 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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
fearoffours
Updated on September 17, 2022
Comments
I have configured Apache to send back a 200 response without serving any file with this configuration line
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.
One could argue that if you don’t serve a «file» or any content, you should return 204 No Content, as opposed to 200.
Sure, make it a 204, how do I do it? Although I very much doubt any browser will be confused by an empty body.
an empty body is still a response, with an object, such as a blank index.html. What you asked is to provide a 200 response with no resource attached to it (no file served). As for how exactly to do it on nginx, I need to look it up myself, I’ve only once done this on apache, and I can’t remember off hand.
Just add the newline! Nginx configuration directives are not bound to single lines, they are ended by semicolon.
add_header doesn’t work for me as it adds another header instead of replacing the old ‘Content-type’. In my response I have 2 ‘Content-type’ headers:$ curl -v localhost/healthcheck/h1_pio > GET /healthcheck/h1_pio HTTP/1.1 > User-Agent: curl/7.38.0 > Host: localhost > Accept: / > < HTTP/1.1 200 OK < Date: Tue, 11 Oct 2016 13:27:53 GMT < Content-Type: application/octet-stream < Content-Length: 25 < Connection: keep-alive < Content-Type: application/json
@jmcollin92 your comment has nothing to do with the question that was asked and to which the answer was given. because you obviously have some sort of proxy_pass, fascgi_pass, whatever. but I still answer location /healthcheck/h1_pio < # proxy_pass blablabla what you need; proxy_hide_header Content-Type; add_header Content-Type application/json; >in the future, ask your question correctly and in the proper location
@jmcollin92 that can happen if you have an existing default_type declared somewhere else. You can override it by using default_type text/plain; inside of the location block in place of the add_header directive.
Hint: use add_header Content-Type text/plain always; to force plain text if you’re not returning a «successful» code.
If you try to view this through a browser, it will look like it did nothing. that’s intentional. You served nothing (204), it displays nothing. To prove you served a 204, use curl .
Exactly. Google Cloud Load Balancer requires a 200 response code to consider the instance healthy, no matter the body. Any other code (even 2xx codes) will make the health check fail.
If you have authentication enabled, take care that this doesn’t bypass it: stackoverflow.com/questions/40447376/…
This will return a unformatted text. If that is the point of the answer, ok. But if the objective is to return a formatted html page, this doesn’t work. An edit was not accepted for the answer, regarding such improvement, so I provided a different answer, with a broader scenario.
In case you need to return a custom 404 page with a custom Content-Type header but can only append part of the Nginx configuration (Kubernetes ingress-nginx controller configuration), please see my answer below.
If you have nginx-extras , openresty or similar, you don’t need default_type «»; — just use more_set_headers ‘Content-Type: text/plain’; — plus return 900 ‘blubbi’ .
NGINX: How to return inline html?
e.g. in my case curl showed me: The fix is to clear the global type and set one for a given url Solution 1: Remove: And just create a new config file with the following content: Reload Nginx config: Test the URL: New Relic config: Solution 2: For me the accepted answer did not work (although it might be correct from coding perspective) Solution: It is hard to say without context(how your entire nginx config file looks like), because of how nginx processes a request A config file like the following, should work just fine for what you are looking for: However, if your config file has other location blocks(especially if they are regex based) then you may not get the expected solution.
Return 200 by Nginx without serving a file
It is hard to say without context(how your entire nginx config file looks like), because of how nginx processes a request
A config file like the following, should work just fine for what you are looking for:
However, if your config file has other location blocks(especially if they are regex based) then you may not get the expected solution. Take an example of this config file:
Sending a request to curl localhost:80/hoge would return a http status code 418 instead of 200. This is because the regex location matched before the exact location.
So, the long answer is; it is hard to tell without the context of the whole nginx conf file that you are using. But understanding how nginx processes a request will get you to the answer.
Status code in nginx try_files directive, Is it possible to use the current status code as a parameter in try_files? For example, we try to provide a host specific 503 static response, Status code in nginx try_files directive. Ask Question Asked 10 years, 2 months ago. Modified 2 years, 4 months ago. Viewed 7k times
NGINX: How to return inline html?
Use the return directive to return HTML code. Remember to set proper content type, otherwise the browser will asume raw text and won’t render the code:
just setting the content type header seems to work on some browsers, however safari on ios stil tries to download the file.
You might have set the content type somwhere else, resulting in 2 content type headers this way.
e.g. in my case curl showed me:
The fix is to clear the global type and set one for a given url
server < server_name example.com; listen 80; location / < types <>default_type text/html; return 200 'Hello World'; > >
Http status code 302 — how to send a static file when, Browse other questions tagged nginx http-status-code-302 or ask your own question. The Overflow Blog Data …
Nginx — how to create /status with stub_status
And just create a new config file status.conf with the following content:
$ curl http://127.0.0.1/nginx_status Active connections: 6 server accepts handled requests 1285282 1285282 17041299 Reading: 0 Writing: 6 Waiting: 0
url=http://localhost/nginx_status
For me the accepted answer did not work (although it might be correct from coding perspective)
I just needed to restart nginx
sudo service nginx restart
Any sudo nginx -s reload I did would give back 404.
My config file
Use nginx to serve static files from subdirectories of a, I have several sets of static .html files on my server, Make HTML code show all pictures in a folder from FTP?-1. Nginx — static file serving confusion with root & alias. 16. nginx change root folder for specific url. 745. How to serve static files in Flask. 2.
Nginx post method does not work
Nginx responds with an HTTP 405 for POST requests that try to access a static asset.
From the Nginx release docs from a few years back:
*) Feature: now Nginx returns the 405 status code for POST method requesting a static file only if the file exists.
A way to go around it would be to add this line, which changes the response code and sends you to the requested URI:
You can find other solutions here:
Http status code 405 — nginx post method does not work, 1 Answer. Nginx responds with an HTTP 405 for POST requests that try to access a static asset. *) Feature: now Nginx returns the 405 status code for POST method requesting a static file only if the file exists. A way to go around it would be to add this line, which changes the response code and sends you to the …
How to configure nginx to return text or json
Sometimes when you request some interfaces, you need to return the specified text string or json string. If it is a simple logic or a fixed string, you can use nginx to implement it quickly, so you don’t need to write a program to respond to the request. using nginx can reduce server resource usage and make response performance very fast.
First look at how to return fixed text and json, they are configured in the nginx server segment to configure the location item to intercept, the configuration example is as follows:
Reloading the configuration after saving will take effect. Note here: default_type must be added, otherwise the browser will download it as an unrecognized file.
In addition, you can simply return different strings based on the requested URL. Here is an example:
This can simply intercept the string in the url, of course, you can use (.*) to match all, in fact, according to different needs can be defined
The above are some simple cases. In the server environment, for some simple processing, you can use nginx to save some programming work.
Also add the Chinese display problem, because Linux uses the character encoding of UTF-8. By default, our browser will render the page with GBK code if the server does not specify the encoding or the static page does not declare the encoding. So, by default, when the browser returns Chinese, the browser uses gbk to parse the utf-8 encoding. Obviously, garbled characters will appear. At this time, the header is actively added in the location block of nginx to output the correct encoding.