Preface add_header is a directive defined in the headers module. As the name suggests, it is used to add http response headers. But please note that it is just "added", not rewritten. So if a header already exists, using add_header will cause problems. Moreover, in earlier versions of nginx, add_header does not support use in error pages. This is a command with many pitfalls. Its processing stage is later than location processing. Although it can be written in location, if another location is rewritten, the add_header that has not been processed in the previous location will be lost. for example: location = /a { add_header a 1; rewrite //b; } location = /b { add_header b 2; return 204; } There is no head a 1, right? This is a pit! Another pitfall is the duplication problem mentioned at the beginning. For example, I want to set Content-Type for a content, but because a default_type is set globally, it is repeated. default_type 'text/plain'; location = /a { add_header Content-Type application/json; return 200 '"OK"'; } There are of course many solutions, such as leaving the default_type empty for this location, or simply not using add_header and directly modifying the default_type for this location. The last big pitfall is that it does not work on error pages, which is also clearly defined in the document. For example, the following example: location = /a { add_header Content-Type application/json; return 404 '"Not Found"'; } I want to respond with a JSON, but since the status code is 404, add_header here does not take effect. Although this example can solve the problem with default_type, what if it is other headers? For example, what should I do with Access-Control-Allow-Origin? Then there is no solution except using Lua or other third-party modules. Of course, nginx is aware of this problem, so the document also states that a parameter called always is supported after version 1.7.5. Although nginx itself solves this problem, tengine based on 1.6.2 will have problems. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM. You may also be interested in:
|
<<: Sharing of SQL optimization experience when offset is too large during MySQL paging
>>: How to implement Svelte's Defer Transition in Vue
Standardized design solutions - markup languages ...
I haven't used mysql on my computer for a lon...
This article shares two methods of implementing t...
The usage format of the mysqladmin tool is: mysql...
Table of contents Introduction Download and insta...
Table of contents 1. What is Dockerfile? 2. Analy...
Table of contents 1. Reverse the numbers 2. Get t...
This is the effect of the Element UI loading comp...
Confluence is paid, but it can be cracked for use...
Table of contents Preface Active withdrawal Excep...
usemap is an attribute of the <img> tag, use...
Syntax: <marquee> …</marquee> Using th...
Table of contents Start Docker Stop Docker Python...
1. What is Continuous Delivery The software produ...
Preface We all know that the import and export of...