在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ngx_http_headers_module模块 add_header From ogeek.net location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } 实例二:对js,css文件在浏览器本地缓存1小时 location ~ .*\.(js|css)$ { expires 1h; } ngx_headers_more模块 more_set_headers -s 404 -t 'text/html' 'X-Foo: Bar'; 输入头信息也可以这么修改,如: location /foo { more_set_input_headers 'Host: foo' 'User-Agent: faked'; # now $host, $http_host, $user_agent, and # $http_user_agent all have their new values. } -t选项也可以在more_set_input_headers和more_clear_input_headers指令中使用。 二. 安装ngx_headers_more wget 'http://nginx.org/download/nginx-1.5.8.tar.gz' tar -xzvf nginx-1.5.8.tar.gz cd nginx-1.5.8/ # Here we assume you would install you nginx under /opt/nginx/. ./configure --prefix=/opt/nginx \ --add-module=/path/to/headers-more-nginx-module make make install ngx_headers_more 包下载地址:http://github.com/agentzh/headers-more-nginx-module/tags more_set_headers "Server: my_server"; 具有相同名称的响应头总是覆盖。如果要添加头,可以使用标准的add_header指令代替。 more_set_headers 'Foo: bar' 'Baz: bah'; 在单一指令中,选项可以多次出现,如: more_set_headers -s 404 -s '500 503' 'Foo: bar'; 等同于: more_set_headers -s '404 500 503' 'Foo: bar'; 新的头是下面形式之一:
最后两个有效清除的头名称的值。Nginx的变量允许是头值,如: set $my_var "dog"; more_set_headers "Server: $my_var"; 注意:more_set_headers允许在location的if块中,但不允许在server的if块中。下面的配置就报语法错误: # This is NOT allowed! server { if ($args ~ 'download') { more_set_headers 'Foo: Bar'; } ... } more_clear_headers 语法:more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>... more_clear_headers -s 404 -t 'text/plain' Foo Baz; 等同于 more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: "; 或 more_clear_headers -s 404 -t 'text/plain' Foo Baz; 等同于 more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: "; 或 more_set_headers -s 404 -t 'text/plain' Foo Baz 也可以使用通配符*,如: more_clear_headers 'X-Hidden-*'; 清除开始由“X-Hidden-”任何输出头。 more_clear_input_headers -s 404 -t 'text/plain' Foo Baz; 等同于 more_set_input_headers -s 404 -t 'text/plain' "Foo: " "Baz: "; 或 more_clear_input_headers -s 404 -t 'text/plain' Foo Baz; 等同于 more_set_input_headers -s 404 -t 'text/plain' "Foo: " "Baz: "; 或 more_set_input_headers -s 404 -t 'text/plain' Foo Baz 四. ngx_headers_more局限性 # set the Server output header more_set_headers 'Server: my-server'; # set and clear output headers location /bar { more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo'; more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo'; more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar'; more_clear_headers 'Content-Type'; # your proxy_pass/memcached_pass/or any other config goes here... } # set output headers location /type { more_set_headers 'Content-Type: text/plain'; # ... } # set input headers location /foo { set $my_host 'my dog'; more_set_input_headers 'Host: $my_host'; more_set_input_headers -t 'text/plain' 'X-Foo: bah'; # now $host and $http_host have their new values... # ... } # replace input header X-Foo *only* if it already exists more_set_input_headers -r 'X-Foo: howdy'; 六. 应用ngx_headers_more more_set_headers "Server: ogeek.net Web Server"; more_clear_headers "Content-Type:"; more_clear_headers "Accept-Ranges: "; more_clear_headers "Content-Length: "; 404状态码添加header more_set_headers "Server: ogeek.net Web Server"; more_set_headers -s 404 "Error: Not found"; more_clear_headers "Content-Type:"; more_clear_headers "Accept-Ranges: "; more_clear_headers "Content-Length: "; |
请发表评论