Procedure for Building Nginx for CSP
Procedure for Building Nginx for CSP
-
Working in a MSYS2 shell, create the working directory structure suggested in the Nginx documentation:
/opt/
-
Working in /opt, check-out the Nginx source code using the following command:
hg clone http://hg.nginx.org/nginx
This places the Nginx source code under: /opt/nginx/
-
Create a directory for the CSP extension:
mkdir /opt/nginx/objs/lib/csp/
-
Copy the module source code (ngx_http_csp_module.c) to the directory created in the previous step.
-
In the same directory, create a configuration file called config. This file should contain the following lines:
ngx_addon_name=ngx_http_csp_module HTTP_MODULES="$HTTP_MODULES ngx_http_csp_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_csp_module.c"
-
Working in /opt/nginx/, configure the Nginx build environment:
auto/configure --with-cc=cl --builddir=objs --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --without-http_rewrite_module --without-http_gzip_module --with-select_module --with-ipv6 --add-module=objs/lib/csp
Note the final line containing the instructions to include the CSP module.
-
Compile Nginx. This can be done in either the current MSYS2 shell or a Visual Studio developer command prompt.
To use the MSYS2 shell, locate the vcvarsall.bat script corresponding to your desired Visual Studio build environment and compile Nginx.
cd /c/path/to/vcvarsall vcvarsall.bat cd - nmake -f objs/Makefile
Alternatively, if you do not know where to find vcvarsall.bat, you can open a Visual Studio developer command prompt, which will set up the build environment for you. First, convert the MSYS2 path to an equivalent Windows path in the current MSYS2 shell.
cygpath –m $(pwd)
Then, open a Visual Studio command prompt for your desired build environment and navigate to that Windows path. Compile Nginx.
nmake -f objs/Makefile
If successful, you should find the server (nginx.exe) in: /opt/nginx/objs/
-
Install Nginx: The easiest way to do this is to first download and install a pre-built version of Nginx for Windows to obtain the directory structure (usually under C:\nginx\) then replace the nginx.exe file in that installation with the one created locally.
The typical directory structure for an operational Nginx installation is as follows:
Directory of C:\nginx 03/07/2017 09:09 <DIR> . 03/07/2017 09:09 <DIR> .. 26/06/2017 10:14 <DIR> conf 26/06/2017 10:14 <DIR> contrib 10/05/2018 12:53 <DIR> csp 26/06/2017 10:14 <DIR> docs 26/06/2017 10:14 <DIR> html 10/05/2018 15:57 <DIR> logs 04/07/2017 15:52 715,264 nginx.exe 26/06/2017 10:17 <DIR> scgi_temp 26/06/2017 10:17 <DIR> temp 26/06/2017 10:17 <DIR> uwsgi_temp
Replace the copy of nginx.exe in this directory with the version created by the build procedure.