sumarsono.com
Take it with a grain of salt


Nginx Return Custom Json

Posted on

Nginx return custom json

server {
  listen 80 default_server;
  server_name _;

  root /var/www/html;
  index index.html;

  location / {
    default_type application/json;
    return 200 '{"code":"0", "message": "I am alive"}';
  }
}

Test:

▶ curl -iL anuku.domainku.tld
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Sun, 14 Nov 2021 11:39:17 GMT
Content-Type: application/json
Content-Length: 37
Connection: keep-alive
Vary: Accept-Encoding

{"code":"0", "message": "I am alive"}%