Home > Operation and Maintenance > Nginx > How to solve the pitfall of nginx configuration add_header

How to solve the pitfall of nginx configuration add_header

王林
Release: 2023-05-17 17:40:06
forward
1849 people have browsed it

Preface

add_header is an instruction defined in the headers module. As the name suggests, it is used to add http response headers. But please note that it is just "adding", not rewriting. So if a header already exists, using add_header will cause problems. Moreover, in lower versions of nginx, add_header does not support use in error pages.

This is an instruction with many pitfalls. Its processing stage is later than location processing. Although it can be written in location, if another location is rewritten, the unprocessed add_header 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;
}
Copy after login

How to solve the pitfall of nginx configuration add_header

does not have the header a 1, right? This is a pit!

Another pitfall is the repetitive problem mentioned at the beginning. For example, I want to set content-type for a content, but because there is a default_type set globally, it is repeated.

default_type 'text/plain';

location = /a {
 add_header content-type application/json;
 return 200 '"ok"';
}
Copy after login

How to solve the pitfall of nginx configuration add_header

Of course there are many solutions, such as leaving the default_type blank 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 cannot take effect on error pages, which is also clearly defined in. For example, the following example:

location = /a {
 add_header content-type application/json;
 return 404 '"not found"';
}
Copy after login

I hope to respond with a json, but since the status code is 404, the add_header here will not take effect.

How to solve the pitfall of nginx configuration add_header

Although this example can use default_type to solve the problem, what if it is other headers? For example, what to do with access-control-allow-origin? Then there is no solution except using lua or other third-party modules to solve it. Of course, nginx is also aware of this problem, so the document also says that it supports a parameter called always after version 1.7.5. Although nginx has solved this problem by itself, tengine based on 1.6.2 is about to fail.

The above is the detailed content of How to solve the pitfall of nginx configuration add_header. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template