Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| engineering:computer_science:docker:application_guides:docker_nginx_reverse_proxy [2024/08/16 15:05] – removed - external edit (Unknown date) 127.0.0.1 | engineering:computer_science:docker:application_guides:docker_nginx_reverse_proxy [2024/08/16 16:14] (current) – ↷ Links adapted because of a move operation 47.128.47.22 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Nginx as Reverse Proxy for Docker Containers ====== | ||
| + | |||
| + | **Important Notice:** | ||
| + | |||
| + | This is an example for a Reverse Proxy. I will be using Traefik to [[engineering: | ||
| + | |||
| + | ---- | ||
| + | |||
| + | My setup for the docker env. is the same as described on [[engineering: | ||
| + | |||
| + | The materials that I used to learn / troubleshoot / implement this setup: | ||
| + | |||
| + |   * [[https:// | ||
| + |   * [[https:// | ||
| + | |||
| + | ===== Why use a Reverse-Proxy? | ||
| + | |||
| + | I will be using a Nginx Server as a Reverse Proxy, so that a) I can host multiple containers which need to be available in the same ports, b) I don't have to access the containers by memorizing ports therefore being able to use carlossousa.tech/ | ||
| + | |||
| + | ===== Requirements for Reverse Proxy with Nginx in a Docker Container ===== | ||
| + | |||
| + | For Nginx as a Reverse Proxy inside Docker we only need 3 basic things: | ||
| + | |||
| + | - Mapping of the host ports to the container ports | ||
| + |   - Mapping a config file to the default Nginx config file - / | ||
| + | - A Nginx config | ||
| + | |||
| + | ===== Sample Nginx Reverse Proxy - nginx.conf - configuration ===== | ||
| + | |||
| + | A sample nginx reverse proxy config file - nginx.conf - would like something like this: | ||
| + | |||
| + | <code bash> | ||
| + | http { | ||
| + | server { | ||
| + |     server_name your.server.url; | ||
| + | |||
| + |     location / | ||
| + |       proxy_pass http:// | ||
| + |       rewrite ^/ | ||
| + | } | ||
| + | |||
| + |     location / | ||
| + |       proxy_pass http:// | ||
| + |       rewrite ^/ | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + |     server_name another.server.url; | ||
| + | |||
| + |     location / | ||
| + |       proxy_pass http:// | ||
| + |       rewrite ^/ | ||
| + | } | ||
| + | |||
| + |     location / | ||
| + |       proxy_pass http:// | ||
| + |       rewrite ^/ | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== A Nginx Reverse-Proxy sample docker-compose.yml ===== | ||
| + | |||
| + | would be something like: | ||
| + | |||
| + | <code bash> | ||
| + | version: ' | ||
| + | services: | ||
| + | nginx: | ||
| + |     image: nginx: | ||
| + |     container_name: | ||
| + | volumes: | ||
| + |       - ./ | ||
| + |       - ./ | ||
| + |       - ./ | ||
| + |       - / | ||
| + | ports: | ||
| + | - 80:80 | ||
| + | - 443:443 | ||
| + | |||
| + | your_app_1: | ||
| + |     image: your_app_1_image: | ||
| + |     container_name: | ||
| + | expose: | ||
| + |       - " | ||
| + | |||
| + | your_app_2: | ||
| + |     image: your_app_2_image: | ||
| + |     container_name: | ||
| + | expose: | ||
| + |       - " | ||
| + | |||
| + | your_app_3: | ||
| + |     image: your_app_3_image: | ||
| + |     container_name: | ||
| + | expose: | ||
| + |       - " | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Pratical Example ===== | ||
| + | |||
| + | ==== Making the Reverse Proxy ==== | ||
| + | |||
| + | Let's go ahead and create our folder and create our 2 required files, the docker-compose.yml and the nginx.conf. Navigate to your desired folder and run: | ||
| + | |||
| + | <code bash> | ||
| + | mkdir nging-reverseproxy | ||
| + | touch docker-compose.yml | ||
| + | touch nginx.conf | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | Using your favourite method, deploy your **docker-compose.yml **setup. I will be using the good old ' | ||
| + | |||
| + | My configuration would be as follows: | ||
| + | |||
| + | <code bash> | ||
| + | version: ' | ||
| + | volumes: | ||
| + |     dokuwiki_data: | ||
| + | external: true | ||
| + |     dokuwiki_conf: | ||
| + | external: true | ||
| + |     dokuwiki_lib-plugins: | ||
| + | external: true | ||
| + |     dokuwiki_lib-tpl: | ||
| + | external: true | ||
| + |     dokuwiki_logs: | ||
| + | external: true | ||
| + | |||
| + | services: | ||
| + | nginx: | ||
| + |         image: nginx: | ||
| + |         container_name: | ||
| + | volumes: | ||
| + |           - ./ | ||
| + |           - ./ | ||
| + |           - ./ | ||
| + |           - / | ||
| + | ports: | ||
| + | - 80:80 | ||
| + | - 443:443 | ||
| + | |||
| + | dokuwiki: | ||
| + |         image: ' | ||
| + |         container_name: | ||
| + | expose: | ||
| + |             - ' | ||
| + | volumes: | ||
| + |             - dokuwiki_data:/ | ||
| + |             - dokuwiki_conf:/ | ||
| + |             - dokuwiki_lib-plugins:/ | ||
| + |             - dokuwiki_lib-tpl:/ | ||
| + |             - dokuwiki_logs:/ | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | And now we edit the **nginx.conf **to point back to our DokuWiki. Note that I will already configure a sub-domain and a main domain, which at the moment would both point to the same container, but once we make [[refractor_computer_science: | ||
| + | |||
| + | <code bash> | ||
| + | events { | ||
| + | |||
| + | } | ||
| + | |||
| + | http { | ||
| + |   #error_log / | ||
| + | client_max_body_size 20m; | ||
| + | |||
| + |   proxy_cache_path / | ||
| + | |||
| + | server { | ||
| + |     server_name wiki.carlossousa.tech; | ||
| + | |||
| + | location / { | ||
| + |       include / | ||
| + |       proxy_pass http:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + |     server_name carlossousa.tech; | ||
| + | |||
| + | location / { | ||
| + |       include / | ||
| + |       proxy_pass http:// | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | And finally we will once again create a **backup_to_tar.sh **so we can backup our nginx Reverse Proxy setup: | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | SOURCE_PATH="/ | ||
| + | BACKUP_PATH="/ | ||
| + | BACKUP_NAME=" | ||
| + | tar cvf " | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||