Home Backend Development PHP Tutorial Article 19: Adding header fields to http requests or responses in Nginx

Article 19: Adding header fields to http requests or responses in Nginx

Aug 08, 2016 am 09:19 AM
data headers http nbsp out

Welcome to reprint and reprint, please indicate the source http://blog.csdn.net/yankai0219/article/details/8270219

0. Preface 1. Add the header field in the upload module

2. Nginx for ngx_http_headers_out_t Processing of headers_out.headers member variables

1) Request: 2) Response:

3. Add head field to request or response

1) Program adds head field helloheaders content 123344

123344🎜🎜🎜🎜🎜🎜

0. Preface

Based on the needs of the project, you need to add the md5 header field of the file to the http response. As we all know, the header fields of Nginx response are all in the ngx_http_headers_out_t structure. By looking up, we found that there is no md5 header field in it. Does that mean that the http protocol does not support the md5 header field?

By consulting the article, we can find that the http protocol supports the md5 header field, but nginx does not add this header in the ngx_http_headers_out_t structure field. nginx author's explanation http://mailman.nginx.org/pipermail/nginx/2008-July/006251.html

So does Nginx really not support the md5 header field? After checking, I found that the upload module supports the content-md5 header field. So it means that nginx supports content-md5, so how do we implement it?

In summary: 1) http protocol supports md5 header field 2) Nginx supports content-md5

1. Add header field in upload module

Parse configuration file through ngx_http_upload_add_header in nginx upload module Save the header fields in ulcf->header_templates, and then add the header fields to the header of the response by calling the ngx_http_upload_add_headers function. Next we analyze how it is added to the response header.

You can see the ngx_http_upload_add_headers function in the Nginx upload module source code.

The process is: 1) Parse the header fields in ulcf->header_templates

2) In ngx_http_headers_out_t Add an element to ngx_list_t headers of headers_out.

                        3) Fill in the value for the newly added element. : r

,

ngx_http_upload_loc_conf_t*

ulcf) { / * {{{ */                                                     -> headers_out.headers); //In ngx_http_headers_out_t ngx_list_t of headers_out

                                                                                                                              t;

hash=1; h->key.len=name.len;

->

key

.data = name.data;

                h->value.len = value.len;

                h->value.data = value.data;

                ...........................

} /* }}} */

2.Nginx for ngx_http_headers_out_t Processing of headers_out.headers member variables

1) Request:

In the ngx_http_proxy_create_request function, we can see the construction of an http request (including three parts: request line, request header, and request body). We only need Just take a look at the processing of request headers.

if(plcf->upstream.pass_request_headers) {
part = &r->headers_in.headers. part;
     header = part->elts;
                                                            = 0;​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ >= part->nelts) {                                        == NULL) { Break
; } part = part-& gt; next;
header = part->elts; i = 0;
}if
(ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
                                                                                                                                                                                                                                                  header[i].
                                                         ;
                                                                                                                                   );
         *b->last++ =
' '; b->last = ngx_copy(b->last, header[i].value.data,
                                                                                                                                             header[i].value.len);
                                                                                                                                                                                                                       }

2) Response:

The processing of headers is ngx_http_header_filter in the ngx_http_header_filter_module module. The process is very similar to the header processing in the request.

3. Add header fields to the request or response

There are ngx_list_t headers member variables in the ngx_http_request_t member variables headers_in and headers_out. Its role is to store header fields. In this function, you can see the ngx_list_push(&r->headers_out.headers) function, which puts the header fields into the response.

By analogy, you can use the ngx_list_push(&r->headers_in.headers) function to put the header fields into the request.

1) Program Add header field helloHeaders content 123344 in the request

This function is located in ngx_http_proxy_handler of ngx_http_proxy_module.c

                                                              The above introduces article 19: Adding header fields to http requests or responses in Nginx, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.
void yk_add_headers_in( ngx_http_request_t*r) { /* {{{ */
                                      into yk_add_headers_in");                                *h;                                                        = ngx_string(
"helloHeaders" ); value = ngx_string(
"123344"); h = ngx_list_push(&r-> headers_in.
headers) ;                                                              ==NULL){
                                                                                                                                       = 1;                                h-> name.len;
data= name.data
; h->value
. len
= value.len;h-> value.
data;}#endifThanks to me It is to set the proxy, so all requests will enter the ngx_http_proxy_handler function, Therefore, at the beginning of the ngx_http_proxy_handler function, add
yk_add_headers_in(
r), then all request headers will have the helloHeaders header field. You can see that this is indeed the case through packet capture.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

How to Hide and Unhide Folders on Windows 11 [3 Ways] How to Hide and Unhide Folders on Windows 11 [3 Ways] Sep 23, 2023 am 08:37 AM

Hiding folders is a great way to keep your desktop organized. Maybe you want to keep your personal files or some client details away from prying eyes. Whatever it is, the ability to put them away and unhide them when necessary is a big saver. In short, these hidden files will not show up in the main menu, but they will still be accessible. It's very simple and shouldn't take you too much time. How to hide a folder in Windows 11? 1. Use File Explorer and hit the + key to open File Explorer. WindowsE Find the folder you want to hide, right-click it and select Properties. Navigate to the General tab, check the Hide box, click Apply, and then click OK. In the next dialog box, check Apply changes to this folder, sub-folder

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

See all articles