Detailed explanation of the pitfalls of add_header in nginx configuration tutorial

Detailed explanation of the pitfalls of add_header in nginx configuration tutorial

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:
  • How to add Nginx to system services in CentOS7
  • How to deploy multiple Vue projects under the same domain name using nginx and use reverse proxy
  • Python implementation example of monitoring differences in Nginx configuration files and sending email alerts
  • Detailed explanation of Nginx installation, SSL configuration and common commands under Centos7.x
  • How to configure nginx+php+mysql in docker
  • Solution to invalid Nginx cross-domain setting Access-Control-Allow-Origin
  • Example method of deploying react project on nginx
  • Use nginx.vim tool for syntax highlighting and formatting configuration nginx.conf file
  • Why should you be careful with Nginx's add_header directive?
  • Detailed steps for deploying https websites and configuring address rewriting in Nginx

<<:  Sharing of SQL optimization experience when offset is too large during MySQL paging

>>:  How to implement Svelte's Defer Transition in Vue

Recommend

Markup Language - List

Standardized design solutions - markup languages ...

How to solve the problem of forgetting the root password of Mysql on Mac

I haven't used mysql on my computer for a lon...

JavaScript to achieve fancy carousel effect

This article shares two methods of implementing t...

Summary of mysqladmin daily management commands under MySQL (must read)

The usage format of the mysqladmin tool is: mysql...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

Process parsing of reserved word instructions in Dockerfile

Table of contents 1. What is Dockerfile? 2. Analy...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

Complete steps for deploying confluence with docker

Confluence is paid, but it can be cracked for use...

Let's talk in detail about how the NodeJS process exits

Table of contents Preface Active withdrawal Excep...

Example of using UserMap in IMG

usemap is an attribute of the <img> tag, use...

Make your text dance with the marquee attribute in HTML

Syntax: <marquee> …</marquee> Using th...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...