Reverse Proxy with HTTPS
Reverse Proxy with HTTPS
Imagine you have started an application within the PHP container that creates a listening port (e.g.: NodeJS). This will now only listen on the PHP container and you would have to adjust the docker-compose.yml definition in order to have that port available outside to your host OS.
Alternatively, there is a simple way to reverse proxy it to the already running web server and even make use of the available HTTPS feature.
Read more about how to autostart applications:
custom-scripts-per-php-version(individually for different PHP versions)custom-scripts-globally(equal for all PHP versions)
Walkthrough
Assumption
Let’s imagine you have started an application in the PHP container with the following settings:
env_TLD_SUFFIX:locgetting-started-directory-overview-project-directoryinside PHP container:/shared/httpd/my-appenv-httpd-datadiron the host:./data/wwwenv-httpd-template-dir:.devilbox- Listening port:
8081 - Listening host:
php(any of the PHP container) - Resulting vhost name:
my-app.loc
Copy vhost-gen templates
The reverse vhost-gen templates are available in cfg/vhost-gen:
host> tree -L 1 cfg/vhost-gen/
cfg/vhost-gen/├── apache22.yml-example-rproxy├── apache22.yml-example-vhost├── apache24.yml-example-rproxy├── apache24.yml-example-vhost├── nginx.yml-example-rproxy├── nginx.yml-example-vhost└── README.md
0 directories, 7 filesFor this example we will copy all *-example-rproxy files into
/shared/httpd/my-app/.devilbox to ensure this will work with all web
servers.
host> cd /path/to/devilboxhost> cp cfg/vhost-gen/apache22.yml-example-rproxy data/www/my-app/.devilbox/apache22.ymlhost> cp cfg/vhost-gen/apache24.yml-example-rproxy data/www/my-app/.devilbox/apache24.ymlhost> cp cfg/vhost-gen/nginx.yml-example-rproxy data/www/my-app/.devilbox/nginx.ymlAdjust port
By default, all vhost-gen templates will forward requests to port 8000
into the PHP container. Our current example however uses port 8081, so
we must change that accordingly for all three templates.
Adjust Apache 2.2 template
Open the apache22.yml vhost-gen template in your project:
host> cd /path/to/devilboxhost> vi data/www/my-app/.devilbox/apache22.ymlFind the two lines with ProxyPass and ProxyPassReverse and change
the port from 8000 to 8081
# ... more lines above ... #
###### Basic vHost skeleton###vhost: | <VirtualHost __DEFAULT_VHOST__:__PORT__> ServerName __VHOST_NAME__
CustomLog "__ACCESS_LOG__" combined ErrorLog "__ERROR_LOG__"
# Reverse Proxy definition (Ensure to adjust the port, currently '8000') ProxyRequests On ProxyPreserveHost On ProxyPass / http://php:8081/ ProxyPassReverse / http://php:8081/
# ... more lines below ... #Adjust Apache 2.4 template
Open the apache24.yml vhost-gen template in your project:
host> cd /path/to/devilboxhost> vi data/www/my-app/.devilbox/apache24.ymlFind the two lines with ProxyPass and ProxyPassReverse and change
the port from 8000 to 8081
# ... more lines above ... #
###### Basic vHost skeleton###vhost: | <VirtualHost __DEFAULT_VHOST__:__PORT__> ServerName __VHOST_NAME__
CustomLog "__ACCESS_LOG__" combined ErrorLog "__ERROR_LOG__"
# Reverse Proxy definition (Ensure to adjust the port, currently '8000') ProxyRequests On ProxyPreserveHost On ProxyPass / http://php:8081/ ProxyPassReverse / http://php:8081/
# ... more lines below ... #Adjust Nginx template
Open the nginx.yml vhost-gen template in your project:
host> cd /path/to/devilboxhost> vi data/www/my-app/.devilbox/nginx.ymlFind the lines with proxy-pass and change the port from 8000 to
8081
# ... more lines above ... #
###### Basic vHost skeleton###vhost: | server { listen __PORT____DEFAULT_VHOST__; server_name __VHOST_NAME__;
access_log "__ACCESS_LOG__" combined; error_log "__ERROR_LOG__" warn;
# Reverse Proxy definition (Ensure to adjust the port, currently '8000') location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://php:8081; }
# ... more lines below ... #Restart the Devilbox
Now for the changes to take affect, simply restart the Devilbox (or start if not yet running):
host> cd /path/to/devilbox
# Stop the Devilboxhost> docker-compose stophost> docker-compose rm -f
# Start the Devilbox (PHP and HTTPD container only)host> docker-compose up -d php httpd bindStart your application
Enter the PHP container and start your application which creates the
listening port in port 8081.
This can also be automated to happen automatically during
docker-compose up via:
custom-scripts-per-php-version(individually for different PHP versions)custom-scripts-globally(equal for all PHP versions)- Example:
autostarting-nodejs-apps
Visit your project
That’s it, your service application will now be available via:
It will also be available on HTTPS. This is by default and automatically:
setup-valid-https
And is even shown as a project in the Devilbox intranet: