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: Or you can change the add_header line 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:
|
<<: JS canvas realizes the functions of drawing board and signature board
>>: Mysql database master-slave separation example code
1. Introduction: I think the changes after mysql8...
Table of contents Application Scenario Simply put...
What is the input type="file"? I don'...
Table of contents 1. Mutex 1. Initialization of m...
npm uninstall sudo npm uninstall npm -g If you en...
Recently the company has arranged to do some CCFA...
Introduction After compiling, installing and solv...
Preface In daily code development, there are many...
The author has been working on a micro-frontend p...
1. Embedded Software Level 1) Bootloader->Boot...
· 【Scene description】 After HTTP1.1, the HTTP pro...
What is a descending index? You may be familiar w...
This article shows you how to use CSS to create a...
environment System: Ubuntu 18.04 Software: qt5.12...
Preview: Code: Page Sections: <template> &l...