I am going to build a website with docker-compose.
When user enter the root directory, it goes to the web app. When the user enter the http://example.com/blog, it should redirect to the wordpress.
In order to do so, i have config the docker-compose like this
version: "3"
services:
nginx:
image: nginx:latest
depends_on:
- web-app
- wordpress
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
mysql:
image: mysql:5.7.29
container_name: mysql
restart: always
volumes:
- ./data/mysql/data:/var/lib/mysql
wordpress:
depends_on:
- mysql
image: wordpress:latest
ports:
- 8000:80
restart: always
volumes:
- ./data/blog:/var/www/html/wp-content
- ./blog/wp-config.php:/var/www/html/wp-config.php
web-app:
build: ./app
depends_on:
- mysql
restart: always
command: npm start
environment:
- TZ=UTC
- NODE_ENV=production
networks:
default:
external:
name: common
And this is my nginx config
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '[$time_local] $remote_addr - $request request_time: $request_time';
access_log /var/log/nginx/access.log main;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x$
# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
gzip_buffers 16 8k;
server {
listen 80;
charset utf-8;
location /blog/ {
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_pass http://wordpress;
}
location / {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://web-app:3000;
}
}
}
And I have updated the wp-config.php and set the website url.
define('WP_SITE_URL', 'https://example.com/blog');
define('WP_SITEURL', 'https://example.com/blog');
define('WP_HOME', 'https://example.com/blog');
However, when I enter to https://example.com/blog, it return the ERR_TOO_MANY_REDIRECTS
response.
I have searched a lot articles and some of that say it is because I am using cloudflare. How can I solve the above problem?
3
Answers
In your
config.php
you do this :If you don’t version your wordpress resources, use directly the docker compose variables like this, instead of using the
config.php
file :And in the file
docker-compose.yml
you put the following environment variables :To initialize WordPress in a subdirectory like
/blog
, you can use theworking_dir
attribute.The default WordPress image uses
/var/www/html
as root, and you can move to/var/www/html/blog
to initialize at this point.Now, you can navigate to http://localhost/blog.
Full example
Following is a full
docker-compose.yml
example:Notes about syntax:
&wordpress
is a block name, to use it as a common value.<<: *wordpress
is to import fullwordpress
defined variable content at this point.x-
prefix atx-wordpress: &wordpress
. Fordocker-compose.yml
, it is necessary to make it work!Finally, the code like following:
Will be processed as following:
What actually worked for me is this combo :
nginx :
wp-config.php
:Somehow the following config in
docker-compose.yml
didn’t work for me, the$_SERVER
wouldn’t go through, but the first defines did