When BP_WEB_SERVER=nginx
in the build environment, the Paketo NGINX Buildpack
will generate an nginx.conf
at build time and use that configuration file to set up
an NGINX server that starts as PID 1 in the app container.
pack build nginx-no-config --path app \
--builder paketobuildpacks/builder-jammy-base:latest \
--env BP_WEB_SERVER=nginx
docker run --tty --rm --env PORT=8080 --publish 8080:8080 nginx-no-config
curl -s localhost:8080
That address can also be viewed in your browser.
By default, the Paketo NGINX Buildpack generates an nginx.conf
that has
public
as the server root directory. This can be configured by setting
BP_WEB_SERVER_ROOT
to either an absolute path or a path relative to the app
directory. To see this in action, this sample app contains two directories with
different sets of static files. You can set BP_WEB_SERVER_ROOT
to alternate
and see that the server serves the other version of index.html
.
pack build nginx-custom-root --path app \
--builder paketobuildpacks/builder-jammy-base:latest \
--env BP_WEB_SERVER=nginx \
--env BP_WEB_SERVER_ROOT=alternate \
docker run --tty --rm --env PORT=8080 --publish 8080:8080 nginx-custom-root
curl -s localhost:8080
See that the version of index.html
that is in /alternate
is served.
The Paketo NGINX Buildpack allows you to set up push state routing for your
application. This means that regardless of the route that is requested,
index.html
will always be served. This comes in handy if you are serving a
Javascript frontend app where the route exists within the app but not on the
static file structure.
pack build nginx-push-state --path app \
--builder paketobuildpacks/builder-jammy-base:latest \
--env BP_WEB_SERVER=nginx \
--env BP_WEB_SERVER_ENABLE_PUSH_STATE=true
docker run --tty --rm --env PORT=8080 --publish 8080:8080 nginx-push-state
curl -s localhost:8080/test
You should see the contents of index.html
regardless of what route you visit
The Paketo NGINX Buildpack allows you to force any HTTP request made to the server to be redirected using the HTTPS protocol.
pack build nginx-force-https --path app \
--builder paketobuildpacks/builder-jammy-base:latest \
--env BP_WEB_SERVER=nginx \
--env BP_WEB_SERVER_FORCE_HTTPS=true
docker run --tty --rm --env PORT=8080 --publish 8080:8080 nginx-force-https
curl http://localhost:8080
You should see a HTTP status code 301 and the redirect address should be using the HTTPS protocol.
The Paketo NGINX Buildpack allows you to set up basic authentication
through a service binding of type
htpasswd
. The file structure for the
binding looks as follows:
binding
├── type
└── .htpasswd
The contents of ./binding/.htpasswd
were generated using the htpasswd
utility on MacOS:
htpasswd -nb user password
The username is user
, password is password
.
pack build nginx-basic-auth --path app \
--builder paketobuildpacks/builder-jammy-base:latest \
--volume "$(pwd)/binding:/bindings/auth" \
--env BP_WEB_SERVER=nginx \
--env SERVICE_BINDING_ROOT=/bindings
docker run --tty --rm \
--env PORT=8080 \
--env SERVICE_BINDING_ROOT=/bindings \
--publish 8080:8080 \
--volume "$(pwd)/binding:/bindings/auth" \
nginx-basic-auth
curl -s localhost:8080
See that an unauthenticated request results in a HTTP status code 401.
curl -u user:password localhost:8080
See that a request with the correct username and password succeeds.