Generating a nonce for your application does not involve recompiling
nginx anymore, thanks to dynamic modules!
Compile Dynamic Modules
-
Get needed nginx modules
Check the current version of nginx you’re using, then download the sources here: https://nginx.org/en/download.html
Download the [Nginx Development
Kit](https://github.com/simpl/ngx_devel_kit/releases) and the
[set-misc-module](https://github.com/openresty/set-misc-nginx-module/releases).
Extract the archives so that your folder structure looks like this:
-
Configure Arguments
Grab the configure arguments of the nginx version currently
installed on your server:
The output should look like this:
Save the arguments somewhere and add the references to
ngx_devel_kit
and set-misc-nginx-module
to it:
Run ./configure
with your arguments.
-
Compile the modules
It’s time to compile now!
If compilation was successful, check the objs
folder for these
files:
ngx_http_set_misc_module.so
ndk_http_module.so
bash
Load modules in Nginx
Upload the .so
files to your server and put them in
/usr/lib/nginx/modules
.
Now edit your nginx.conf
and load them before the http
block:
load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_set_misc_module.so;
Generate and Replace Nonce
Open your virtual host file and put the following lines in the server
block, replacing <NONCE_PLACEHOLDER>
with a string of your choice:
> Use a unique value for <NONCE_PLACEHOLDER>
and keep it safe!
Update your CSP Header to include your nonce:
Add Nonce to your application
Add the following meta tag to the head of your html, replacing
<NONCE_PLACEHOLDER>
with your nonce-string:
The nonce-string will be replaced by nginx with a unique nonce everytime
the page is loaded.
Sources