How to send plain text response from Nginx

You can use a return statement in Nginx to send a plain text response.

In your Nginx server entry, add

location ~* "^/hello/(.*)" {
    default_type text/plain;
    return 200 "Hello $1\n";
}

Now you can access the page with the URL

Advertisement

boby@sok-01:~$ curl http://localhost/hello/World
Hello World
boby@sok-01:~$

Here is another example

location  "/hello" {
    default_type text/plain;
    return 200 "Hello World\n";
}

For any URL starting with /hello, you get the “Hello World” response.

Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement