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

Various transformation effects of HTML web page switching

<META http-equiv="Page-Enter" CONTENT...

How to install MySQL 5.7 on Ubuntu and configure the data storage path

1. Install MySQL This article is installed via AP...

How to create a swap partition file in Linux

Introduction to Swap Swap (i.e. swap partition) i...

Ubuntu 15.04 opens mysql remote port 3306

Ubuntu 15.04 opens MySQL remote port 3306. All th...

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

Detailed explanation of the use of Vue h function

Table of contents 1. Understanding 2. Use 1. h() ...

MySQL installation tutorial under Linux centos7 environment

Detailed introduction to the steps of installing ...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Brief introduction and usage of Table and div

Web front end 1 Student ID Name gender age 01 Zha...

Use tomcat to deploy SpringBoot war package in centos environment

Prepare war package 1. Prepare the existing Sprin...

Using docker command does not require sudo

Because the docker daemon needs to bind to the ho...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

MySQL initialization password operation under Mac

A simple record of the database startup problems ...

Detailed analysis of the principles and usage of MySQL views

Preface: In MySQL, views are probably one of the ...

How to create a MySQL database and support Chinese characters

Let's first look at the MySQL official docume...