How to configure nginx to return text or json

How to configure nginx to return text or json

Sometimes when requesting certain interfaces, you need to return a specified text string or json string. If the logic is very simple or simply a fixed string, you can use nginx to quickly implement it, so you don't have to write a program to respond to the request, which can reduce server resource usage and have very fast response performance.

First, let's look at returning fixed text and JSON. All you need to do is configure location interception in the server. The configuration example is as follows:

Fixed text:

location ~ ^/get_text {
  default_type text/html;
  return 200 'This is text!'; 
}

Fixed json:

location ~ ^/get_json {
  default_type application/json;
  return 200 '{"status":"success","result":"nginx json"}';
}

After saving, reload the configuration to take effect. Note: default_type must be added, otherwise the browser will download it as an unrecognized file.

Alternatively, you can simply return different strings based on the requested URL, as shown below:

location ~ ^/get_text/article/(.*)_(\d+).html$ {
  default_type text/html;
  set $s $1;
  set $d $2;
  return 200 str:$s$d;
}

This way you can simply intercept the string in the URL. Of course, you can also use (.*) to match all of them. In practice, you can define it according to different needs.

The above are some simple cases. For simple processing in the server environment, making full use of nginx can save some programming work.

In addition, let me add the problem of Chinese display. Because Linux uses utf-8 character encoding, by default, our browser will render the page in GBK encoding when the server does not specify the encoding or the static page does not declare the encoding. So by default, if Chinese is returned, the browser will use gbk to parse utf-8 encoding, which will obviously result in garbled characters. At this time, you need to actively add a header in the nginx location block to output the correct encoding. The added content is: add_header Content-Type 'text/html; charset=utf-8' ; In this way, the browser knows which encoding we are using, as shown in the following figure:

Or you can change the add_header line charset utf-8 ;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Nginx directly returns the Json instance
  • How to change Nginx log to JSON format
  • How to return json or text format in nginx
  • Configuration example of logging in JSON format in nginx

<<:  JS canvas realizes the functions of drawing board and signature board

>>:  Mysql database master-slave separation example code

Recommend

Steps for installing MySQL 8.0.16 on Windows and solutions to errors

1. Introduction: I think the changes after mysql8...

Vue custom v-has instruction, steps for button permission judgment

Table of contents Application Scenario Simply put...

A very detailed explanation of Linux C++ multi-thread synchronization

Table of contents 1. Mutex 1. Initialization of m...

How to completely uninstall node and npm on mac

npm uninstall sudo npm uninstall npm -g If you en...

HTML realizes real-time monitoring function of Hikvision camera

Recently the company has arranged to do some CCFA...

How to add Nginx to system services in CentOS7

Introduction After compiling, installing and solv...

Basic usage and pitfalls of JavaScript array sort() method

Preface In daily code development, there are many...

Use scripts to package and upload Docker images with one click

The author has been working on a micro-frontend p...

How to maintain a long connection when using nginx reverse proxy

· 【Scene description】 After HTTP1.1, the HTTP pro...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...