|
| 1 | +# ngx_security_headers |
| 2 | + |
| 3 | +This NGINX module adds security headers and removes insecure headers easily. |
| 4 | + |
| 5 | +## Synopsis |
| 6 | + |
| 7 | +``` |
| 8 | +http { |
| 9 | + security_headers on; |
| 10 | + ... |
| 11 | +} |
| 12 | +``` |
| 13 | + |
| 14 | +## Key Features |
| 15 | + |
| 16 | +* Plug-n-Play: the default set of security headers can be enabled with `security_headers on;` in your NGINX configuration |
| 17 | +* Sends `X-Content-Type-Options` only for appropriate MIME types, preserving unnecessary bits from being transferred for non-JS and non-CSS resources |
| 18 | +* Plays well with conditional `GET` requests: the security headers are not included there unnecessarily |
| 19 | +* Hides `X-Powered-By`, which often leaks PHP version information |
| 20 | +* Does not suffer the `add_header` directive's pitfalls |
| 21 | +* Hides `Server` header altogether, not just the version information |
| 22 | + |
| 23 | +## Configuration directives |
| 24 | + |
| 25 | +### `security_headers` |
| 26 | + |
| 27 | +- **syntax**: `security_headers on | off` |
| 28 | +- **default**: `off` |
| 29 | +- **context**: `http`, `server`, `location` |
| 30 | + |
| 31 | +Enables or disables applying security headers. The default set includes: |
| 32 | + |
| 33 | +* `X-Frame-Options: SAMEORIGIN` |
| 34 | +* `X-XSS-Protection: 1; mode=block` |
| 35 | +* `X-Content-Type-Options: nosniff` (for CSS and Javascript) |
| 36 | + |
| 37 | +Headers which are hidden: |
| 38 | + |
| 39 | +* `X-Powered-By` |
| 40 | +* `Server` |
| 41 | + |
| 42 | +### `security_headers_xss` |
| 43 | + |
| 44 | +- **syntax**: `security_headers off | on | block | omit` |
| 45 | +- **default**: `block` |
| 46 | +- **context**: `http`, `server`, `location` |
| 47 | + |
| 48 | +Controls `X-XSS-Protection` header. |
| 49 | +Special `omit` value will disable sending the header. |
| 50 | +The `off` value is for disabling XSS protection: `X-XSS-Protection: 0`. |
| 51 | + |
| 52 | +### `security_headers_frame` |
| 53 | + |
| 54 | +- **syntax**: `security_headers_frames sameorigin | deny | omit` |
| 55 | +- **default**: `sameorigin` |
| 56 | +- **context**: `http`, `server`, `location` |
| 57 | + |
| 58 | +Controls inclusion and value of `X-Frame-Options` header. |
| 59 | +Special `omit` value will disable sending the header. |
| 60 | + |
| 61 | +### `security_headers_nosniff_types` |
| 62 | + |
| 63 | +- **syntax**: `security_headers_nosniff_types <mime_type> [..]` |
| 64 | +- **default**: `text/css text/javascript application/javascript` |
| 65 | +- **context**: `http`, `server`, `location` |
| 66 | + |
| 67 | +Defines MIME types, for which `X-Content-Type-Options: nosniff` is sent. |
| 68 | + |
| 69 | +## Install |
| 70 | + |
| 71 | +### CentOS 7 |
| 72 | + |
| 73 | +It's easy to install the module in your stable nginx instance dynamically: |
| 74 | + |
| 75 | + yum -y install https://extras.getpagespeed.com/release-el7-latest.rpm |
| 76 | + yum -y install nginx-module-security-headers |
| 77 | + |
| 78 | +Then add it at the top of your `nginx.conf`: |
| 79 | + |
| 80 | + load_module modules/ngx_http_security_headers_module.so; |
| 81 | + |
| 82 | +### Other platforms |
| 83 | + |
| 84 | +To compile the module into NGINX, run: |
| 85 | + |
| 86 | + ./configure --add-module=../ngx_security_headers |
| 87 | + make |
| 88 | + make install |
| 89 | + |
| 90 | +Or you can compile it as dynamic module. In that case, use `--add-dynamic-module` instead, and load the module after compilation via: |
| 91 | + |
| 92 | + load_module modules/ngx_http_security_headers_module.so; |
0 commit comments