Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the add_header of the nginx configuration tutorial

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

Add_header is an instruction defined in the headers module, which, as its name implies, is used to add http response headers. But please note that he is just "adding", not rewriting. So if a header already exists, there will be a problem using add_header. And in earlier versions of nginx, add_header does not support use in error pages.

This is a command with a lot of holes. Its processing phase is later than location processing, and although it can be written in location, if the rewrite has another location, then 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;}

There is no A1 head, is there? This is a pit!

Another pit 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, I repeat it.

Default_type 'text/plain';location = / a {add_header Content-Type application/json; return 200' "OK";}

Of course, there are many solutions, such as emptying the default_type for the location, or simply modifying the default_type for the location without using the add_header at all.

The last big hole is that it does not take effect 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 to a JSON, but since the status code is 404, the add_header here doesn't work.

Although this example can be used to solve the problem with default_type, what if it is another head? What about Access-Control-Allow-Origin, for example? Then there is no solution except using lua or other third-party modules to solve the problem. Of course, nginx is aware of this problem, so the documentation also says that a parameter called always is supported after version 1.7.5. Although nginx has solved this problem on its own, tengine based on 1.6.2 is going to be ready.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report