---
title: "nginx 获取请求header"
author: "TimerBin"
date: "2018-02-01T10:49:11Z"
tags: []
categories: ["开源软件"]
slug: "nginx-header"
draft: false
lastmod: "2018-02-01T10:49:11Z"
id: 2409931
url: "https://timerbin.iteye.com/blog/2409931"
---

在nginx的location配置中，在获取header配置时，需要在header名称前面加上固定前缀“http\_“，并将header名称中的“-”中划线变为下划线，举例说明：

例如：

       自定义的header名称为X-TimerLocal，那在nginx中使用$http\_x\_timerlocal 来获取到X-TimerLocal的值。

       if ($http\_x\_timerlocal  = 'BIN') {

              rewrite ^(.\*/timerbin/.\*)$ https://$host$1 permanent;

        }

包含X-TimerLocal=BIN的header请求进行判断，如果发现路径中包含/timerbin/路径时，对请求URL进行重写，重新跳转，以上可用做HTTPS转换使用