Skip to content

Instantly share code, notes, and snippets.

@symbioquine
Created April 21, 2022 14:31
Show Gist options
  • Save symbioquine/80f0eb62d94bc52af8509991445c7a6d to your computer and use it in GitHub Desktop.
Save symbioquine/80f0eb62d94bc52af8509991445c7a6d to your computer and use it in GitHub Desktop.
Running farmOS 2.x as a subpath behind a reverse proxy

Demonstrates a simple strategy to host farmOS in a subpath/subdirectory behind a reverse proxy.

Assumes that the farmos.test domain is pointing at localhost or the IP of the server where this example is being run.

The main "tricky" thing that's going on in the example is the creation of a symlink in the web root from ./farm0/ back to the web root itself. That was the simplest strategy I could find to make Drupal understand that it should serve itself from the subdirectory without more complex configuration or file moving.

That strategy is roughly informed by the discussion at https://www.drupal.org/project/drupal/issues/2753591

version: '3.7'
services:
rootsite:
image: nginxdemos/hello:plain-text
expose:
- '80'
db:
image: postgres:12
volumes:
- './db:/var/lib/postgresql/data'
expose:
- '5432'
environment:
POSTGRES_USER: farm
POSTGRES_PASSWORD: farm
POSTGRES_DB: farm
farm0:
depends_on:
- db
image: farmos/farmos:2.x-dev
entrypoint: /bin/bash
command:
- -c
- |
set -ex
wait_db_ready() {
while { ! exec 3<>/dev/tcp/db/5432; } > /dev/null 2>&1; do sleep 0.1; done
}
if [ -d /opt/drupal ] && ! [ "$$(ls -A /opt/drupal/composer.json)" ]; then
echo "farmOS codebase not detected. Copying from pre-built files in the Docker image."
cp -rp /var/farmOS/. /opt/drupal
wait_db_ready
su www-data -s /bin/bash -c 'drush site-install farm --locale=en --db-url=pgsql://farm:farm@db/farm --site-name=Test0 --account-name=root --account-pass=test'
echo "
\$$settings['reverse_proxy'] = TRUE;
\$$settings['reverse_proxy_addresses'] = [\$$_SERVER['REMOTE_ADDR']];
\$$settings['reverse_proxy_trusted_headers'] = \\Symfony\\Component\\HttpFoundation\\Request::HEADER_X_FORWARDED_ALL;
\$$settings['file_private_path'] = '/opt/drupal/web/sites/default/private/files';
" >> /opt/drupal/web/sites/default/settings.php
mkdir -p /opt/drupal/web/sites/default/private/files
chown -R www-data:www-data /opt/drupal/web/sites/default/private/files
ln -s /opt/drupal/web /opt/drupal/web/farm0
chmod 755 /opt/drupal/web/farm0
fi
wait_db_ready
exec docker-entrypoint.sh apache2-foreground
volumes:
- './www:/opt/drupal'
expose:
- '80'
proxy:
image: nginx:latest
depends_on:
- rootsite
- farm0
user: 1337:1337
volumes:
- './nginx.conf:/etc/nginx/nginx.conf'
ports:
- '80:8080'
- '443:8443'
pid /tmp/nginx.pid;
events {
}
http {
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
server {
listen 8080;
server_name farmos.test;
location / {
proxy_pass http://rootsite:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host:443;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Proto https;
}
location /farm0/ {
proxy_pass http://farm0:80/farm0/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host:80;
proxy_set_header X-Forwarded-Port 80;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Proto http;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment