PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, June 24, 2022

[FIXED] How do I configure a reverse proxy for my dockers containers?

 June 24, 2022     docker, raspberry-pi, reverse-proxy     No comments   

Issue

I'm currently configuring my raspberry pi as my personal home server and I want to use docker. I have few different containers :

-Apache/PHP 81:80

-Nextcloud 8080:80

-Minecraft 25565:25565

I already have a dynamic domaim (http://felixbestwaifu.hopto.org)

When I try to access felixbestwaifu.hopto.org:8080, it timeouts :c

My goal

Nextcloud

  • cloud.hostname.org
  • or hostname.org/nextcloud

Website

  • Domain.org
  • Domain.org/about.php
  • Domain.org/Anime.php

I tried using NGINX by creating a nextcloud.conf in /sites-enabled/ and /sites-available/, but it didn't work at all ^^'

server {
    listen 80;
    server_name nextcloud;

    location /nextcloud {
        proxy_pass 192.168.2.150:8080/;
    }
}

Note : 192.168.2.150 is my pi static IP in my network

So here is my question : How do you set-up a reverse proxy for docker containers ?

I would really appreciate your help >//< (sorry for bad English)


Solution

Let us suppose your Apache-PHP container is called apache_php. You should have two .conf files there with similar contents (less or more is regarding what you need in your conf files).

/etc/apache2/sites-available/minecraft.conf:

<VirtualHost *:80>
        ServerName minecraft.com

        ServerAdmin webmaster@minecraft
        DocumentRoot /home/minecraft
        ProxyPreserveHost On
        ProxyPass "/" "http://minecraft:25565/"
        ProxyPassReverse "/" "http://minecraft:25565/"
        ProxyRequests Off

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

/etc/apache2/sites-available/nextcloud.conf:

<VirtualHost *:80>
        ServerName nextcloud.com

        ServerAdmin webmaster@nextcloud
        DocumentRoot /home/nextcloud
        ProxyPreserveHost On
        ProxyPass "/" "http://nextcloud:8080/"
        ProxyPassReverse "/" "http://nextcloud:8080/"
        ProxyRequests Off

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Note the trailing / in ProxyPass and ProxyPassReverse. If you do not add them, final URLs will be something like http://minecraft:25565your_page, while with trailing / it will be http://minecraft:25565/your_page.

In this case if anyone enters minecraft.com, your Apache proxies him to minecraft site and *minecraft is only accessible by your Apache container.

Note that minecraft.com and minecraft are two different websites in a network called proxy that I talk about it below

This is what I do in my container, but let me explain it up to my knowledge:

Suppose Your Apache, your Nextcloud and Minecraft are in the same network (in this case you should create a network, in my case it's called proxy network) and all of these three containers should see each other by pinging.

How your docker-compose.yml file should look like in this case:

version: '3.9'

services:
    apache_php:
        build: .
        restart: always
        ports:
           - "81:80"
    minecraft:
        build: .
        restart: always
        ports:
           - "25565:25565"
    nextcloud:
        build: .
        restart: always
        ports:
           - "8080:80"
networks:
default:
external:
  name: proxy
volumes:
db_data: {}

So in this file, I'm creating three images called apache_php, minecraft and nextcloud that is built via Dockerfile via build keyword. I also proxy ports 81 to 80, port 25565 to 25565 and port 8080 to 80.

Then you can create a Dockerfile or modify existing one with similar contents:

FROM your_os_base:tag

COPY minecraft.conf /etc/apache2/sites-available/
COPY nextcloud.conf /etc/apache2/sites-available/
COPY my_bash_commands.sh /root/
RUN chmod +x /root/my_bash_commands.sh
RUN /root/my_bash_commands.sh
ENTRYPOINT ["your_thing"]

Now what is my_bash_commands.sh in my case? As default shell is sh in most cases, I create a bash file so that I get my desired results.

The my_bash_commands.sh is similar to this:

#!/usr/bash

service apache2 start
a2ensite minecraft
a2ensite nextcloud
a2enmod proxy
a2enmod proxy_http
service apache2 restart

Now you can create them by running docker-compose up -d if you are in the path where docker-compose.yml is, else you should use docker-compose -f /absolute_patht_to/docker-compose.yml up -d.



Answered By - Saeed
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing