Nginx Header,實現對HTTP/S請求、響應進行添加、修改、刪除等操做javascript
文檔地址:php
http://nginx.org/en/docs/http/ngx_http_headers_module.htmlcss
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_headerhtml
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_hide_headerjava
[可選]支持容許下劃線Headernginx
underscores_in_headers on;git
來源庫:http_headers_modulegithub
add_header 'Key' 'values';跨域
Syntax: | add_header |
---|---|
Default: | — |
Context: | http , server , location , if in location |
例如:add_header 'Content-Type' 'text/html;charset=utf-8';服務器
關於 add_header 無效不起做用,通常是沒有在最後一條匹配規則中進行操做,須要將其加入到最深層的匹配規則中,例如location 比 server 深,if 比 location 深。
來源庫:ngx_http_fastcgi_module、ngx_http_proxy_module
fastcgi_hide_header 'Key';
Syntax: | fastcgi_hide_header |
---|---|
Default: | — |
Context: | http , server , location |
proxy_hide_header 'Key';
Syntax: | proxy_hide_header |
---|---|
Default: | — |
Context: | http , server , location |
例如:反向代理和fastcgi區分不一樣的場景使用。
fastcgi_hide_header X-Powered-By;
proxy_hide_header X-Powered-By;
經過內置的操做,修改header分爲兩步,先將其刪除再增長。
例如:
fastcgi_hide_header Content-Type;
proxy_hide_header Content-Type;
add_header 'Content-Type' 'text/css';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
fastcgi_param 'HTTP-X-Forwarded-For' $remote_addr;
proxy_set_header X-Forwarded-For '';
fastcgi_param 'HTTP-X-Forwarded-For' '';
headers-more-nginx-module
Github:https://github.com/openresty/headers-more-nginx-module
syntax: more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>... default: no context: http, server, location, location if phase: output-header-filter
more_set_headers "Server: yunjiasu-nginx";
syntax: more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>... default: no context: http, server, location, location if phase: output-header-filter
more_clear_headers -s 404 -t 'text/plain' Foo Baz;
more_clear_headers 'X-Hidden-*';
syntax: more_set_input_headers [-r] [-t <content-type list>]... <new-header>... default: no context: http, server, location, location if phase: rewrite tail
syntax: more_clear_input_headers [-t <content-type list>]... <new-header>... default: no context: http, server, location, location if phase: rewrite tail
例子:
more_clear_input_headers -t 'text/plain' Foo Baz;
more_clear_input_headers "Foo" "Baz";
more_clear_input_headers 'X-Hidden-*';
例如:
# 根據請求文件名,返回對應的 Content-Type if ( $request_uri ~ .*\.(css)$ ) { add_header 'Content-Type' 'text/css'; } if ( $request_uri ~ .*\.(html|htm|php|php5)$ ) { add_header 'Content-Type' 'text/html;charset=utf-8'; } if ( $request_uri ~ .*\.(js)$ ) { add_header 'Content-Type' 'application/javascript;application/x-javascript'; } # 隱藏腳本版本及服務器版本信息 fastcgi_hide_header X-Powered-By; fastcgi_hide_header Server; # 僞造服務器應用版本信息 more_set_headers 'Server: yunjiasu-nginx/1.0'; # 容許跨域請求 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS;