Nginx reverse proxy deeplinks Wordpress on Docker - wordpress

I'm trying to run Wordpress on Digital Ocean through Docker. Things started out well, I had it up and running in no time configured like so:
version: '3.3'
services:
wordpress:
depends_on:
- db
image: wordpress:5.5.1-php7.2-apache
container_name: wp
restart: always
volumes:
- ./www/wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: ${DB_PWD}
ports:
- 3010:80
networks:
- network
db:
image: mysql:5.7
container_name: db
restart: always
volumes:
- ./dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${DB_PWD}
networks:
- network
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
container_name: pma
restart: always
ports:
- 3011:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${DB_PWD}
UPLOAD_LIMIT: 20000000
networks:
- network
networks:
network:
volumes:
db_data:
On the Digital Ocean droplet I configured Nginx as a reverse proxy:
server {
listen 80;
listen [::]:80;
server_name my.test.url;
location / {
rewrite /(.*) /$1 break;
proxy_pass http://localhost:3010;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}
I already had my.test.url pointing to the droplet's IP.
I updated the 'siteurl' and 'home' records in the wp_options table in the database to my.test.url
This all worked well, serving the homepage if I visit my.test.url. However when I visit a deep link, like my.test.url/example I get redirected to 127.0.0.1/example where obviously nothing is running.
I don't know how to proceed; I thought setting the rewrite rule in the nginx config would do the trick but alas..
I should mention I want to run multiple sites on the Digital Ocean droplet, each serving from their own port, so 'just' serving from port 80 is not an option.
Can anyone point me in the right direction?

As it turns out, the above configuration was correct; the problem was a DNS error combined with Wordpress permalink settings that dictated a trailing slash after the post name. This caused a DNS lookup on the server that failed, resulting in the redirect to 127.0.0.1...

Related

Setting up NGINX as reverse proxy for multiple containerized services

I developed a web app with vue and django however, I'm having problems deploying it.
I added another container to serve as reverse proxy so only port 80 would be exposed and when I finish struggling with this also port 443. I could not find exact anwser how to do it so I hope someone here will be kind enoug to give me some clues.
Here is the conf for the nginx.
The error I'm getting is on ui container.
2022/07/14 09:09:00 [emerg] 1#1: bind() to 0.0.0.0:8080 failed (98: Address already in use)
I looked it up of course, but it was always some different scenario.
BR and thanks in advance
server {
listen 0.0.0.0:80;
listen [::]:80;
location / {
proxy_pass http://0.0.0.0:3000;
}
location /predict {
proxy_pass http://0.0.0.0:5000/predict;
}
location /aggregate {
proxy_pass http://0.0.0.0:5000/aggregate;
}
location /media/pictures {
proxy_pass http://0.0.0.0:5000/media/pictures;
}
access_log /opt/bitnami/nginx/logs/anomaly_access.log;
error_log /opt/bitnami/nginx/logs/anomaly_error.log;
}
My docker-compose looks as follows.
version: '3.2'
services:
se-kpi-sim:
image: test-app:0.0.1
network_mode: "host"
restart: unless-stopped
environment:
MODEL_NAME: "model_final.pickle.dat"
se-kpi-sim-ui:
image: test-ui:0.0.3
network_mode: "host"
restart: unless-stopped
reverse-proxy:
image: test-proxy:0.0.7
network_mode: "host"
restart: unless-stopped
database:
image: postgres
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: kpi_sim_user
POSTGRES_DB: kpi_sim
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
volumes:
- database:/var/lib/postgresql/data
restart: unless-stopped
volumes:
database:
You can run containers on docker internal network and docker-compose by default creates an network for inter communication of containers. One can modify the port to expose the application to host. while you are trying to run most of the app on host network, there might be two application trying to use the same port (like port 8080 [in this case]), one port can only be used by one application in an OS . Please look at the below snippet for more information to solve this issue.
[port mapping <port on HOST>:<container port where app is exposed inside container>]
version: '3.2'
services:
se-kpi-sim:
image: test-app:0.0.1
ports:
- 5000:8080
restart: unless-stopped
environment:
MODEL_NAME: "model_final.pickle.dat"
se-kpi-sim-ui:
image: test-ui:0.0.3
ports:
- 3000:8080
restart: unless-stopped
reverse-proxy:
image: test-proxy:0.0.7
ports:
- 80:80
# this volume mount if you are using bitnami/nginx image
volumes:
- /path/to/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro
restart: unless-stopped
database:
image: postgres
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: kpi_sim_user
POSTGRES_DB: kpi_sim
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
volumes:
- database:/var/lib/postgresql/data
restart: unless-stopped
volumes:
database:
One have to specify either the IP address or DNS name of the application , in order to forward the traffic to specific application. docker-compose create Domain name for all the services defined in docker-compose.yaml file.
server {
listen 0.0.0.0:80;
listen [::]:80;
location / {
proxy_pass http://se-kpi-sim-ui:8080;
}
location /predict {
proxy_pass http://se-kpi-sim:8080/predict;
}
location /aggregate {
proxy_pass http://se-kpi-sim:8080/aggregate;
}
location /media/pictures {
proxy_pass http://se-kpi-sim:8080/media/pictures;
}
access_log /opt/bitnami/nginx/logs/anomaly_access.log;
error_log /opt/bitnami/nginx/logs/anomaly_error.log;
}
One can mount the nginx.conf like:[in bitnami/nginx image]
...
volumes:
- /path/to/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro
...
Note: all the above is an example for reference to solve the problem. entrypoint for containers might change according to one's requirements.

wordpress home url redirects to localhost

I'm using Wordpress + mysql + phpmyadmin with docker-compose and a reverse proxy with nginx in the host machine to send all requests to the wordpress container. After the wordpress installation I've set "site_url" and "home" option names to "http://example.com". Everything works fine when I'm browsing http://example.com/wp-admin but when I try with http://example.com it redirects me to localhost. Everything is in a remote server and I'm doing these requests from my browser and not from the server itself.
This is my nginx conf:
server {
listen 80;
listen [::]:80;
server_name example.com
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://localhost:8000;
}
}
and this is the docker-compose file:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- /home/wordpress/mysqldb:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpresspass
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /home/wordpress/wordpress/html:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpresspass
WORDPRESS_DB_NAME: wordpress
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: rootpass
If I set the option name with ServerIp:8000 the address http://example.com works but everything else redirects with the ServerIp and I'd like to use the domain name. Am I missing something with the wordpress options?
The problem was a simple syntax error. I used the command “service nginx restart” to restart and use a new nginx configuration but restart doesn’t show errors and falls back quietly to an old config. A simple ‘;’ was missing from the server name.

Incorrect URL on Wordpress using Docker

I've a pretty simple setup using Wordpress and Docker, with a docker-compose.yml file:
wordpress:
depends_on:
db:
condition: service_healthy
restart: on-failure
image: wordpress:latest
volumes:
- ./backend/wordpress/wp-content:/var/www/html/wp-content
- ./backend/wordpress/.htaccess:/var/www/html/.htaccess
environment:
WORDPRESS_DB_HOST: ${WORDPRESS_DB_HOST}
WORDPRESS_DB_PASSWORD: ${MYSQL_ROOT_PASSWORD}
WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA:
define('WP_ALLOW_REPAIR', true );
define('WP_HOME','${WORDPRESS_URL}:${NGINX_EXTERNAL_PORT}');
define('WP_SITEURL','${WORDPRESS_URL}:${NGINX_EXTERNAL_PORT}');
nginx:
build: ./backend/nginx
links:
- wordpress
- phpmyadmin
ports:
- ${NGINX_EXTERNAL_PORT}:80
volumes:
- "./backend/nginx/nginx.conf:/etc/nginx/nginx.conf"
And this is my nginx.conf file:
upstream docker-wordpress {
server wordpress;
}
server {
listen 80;
server_name admin.example.com;
location / {
proxy_read_timeout 3600;
proxy_pass http://docker-wordpress;
}
}
Everything seems to work correctly till I try to sort my Wordpress posts by name or slug or whatever field you want, and not only when I sort but also when I try to go to a 2nd page. Instead of getting something like:
http://admin.example.com:5000/wp-admin/edit.php?post_type=artista&orderby=title&order=asc
I get my upstream name on the links, like this:
http://docker-wordpress/wp-admin/edit.php?post_type=artista&orderby=title&order=asc
As I said, everything else works fine, and after taking a look at my site configuration I see that both Wordpress URL and Site URL are:
http://admin.example.com:5000
Which I believe it's correct, any idea what could be wrong? Thanks!
What worked finally was to adding a proxy_set_header directive to my nginx.conf file:
proxy_set_header Host $http_host;

Running multiple dev projects in docker containers with nginx-proxy

As I understand a docker-compose file, using the docker-compose up command, loads the images and starts the containers. Conversely using a Dockerfile file with the docker build command creates the image only. I think I am missing something here as things aren't working as I'd like.
Following the bitnami/wordpress instructions I got an install running fine using docker-compose up d. Can then access via localhost:81
version: '2'
services:
mariadb:
image: bitnami/mariadb:latest
volumes:
- /path/to/mariadb-persistence:/bitnami/mariadb
wordpress:
image: bitnami/wordpress:latest
depends_on:
- mariadb
ports:
- '81:80'
- '443:443'
volumes:
- ./wordpress-persistence:/bitnami/wordpress
- ./apache-persistence:/bitnami/apache
- ./php-persistence:/bitnami/php
Because I want to be able to access this as domain.com.dev, I looked at implementing nginx-proxy. Following the instructions there, and with some inspiration from Docker nginx-proxy : proxy between containers, I came up with the following:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
restart: always
ports:
- "88:80"
volumes:
- "/var/run/docker.sock:/tmp/docker.sock:ro"
mariadb:
image: bitnami/mariadb:latest
volumes:
- //c/websites/domain_com/mariadb-persistence:/bitnami/mariadb
domain.com.dev:
image: bitnami/wordpress:latest
depends_on:
- mariadb
ports:
- '81:80'
environment:
- VIRTUAL_HOST=domain.com.dev
volumes:
- //c/websites/domain_com/wordpress-persistence:/bitnami/wordpress
- //c/websites/domain_com/apache-persistence:/bitnami/apache
- //c/websites/domain_com/php-persistence:/bitnami/php
Running docker-compose up -d with this appears to complete without error. However when I access domain.com.dev in a browser, I get a default Index of / page, which suggests I somehow got partway there but not all the way. Looking at the local folders, they get created but it seems like the wordpress-persistence does not get populated, which could explain the default view in the browser.
Any thoughts on why this isn't coming up as expected? Something obvious I missed?
1) For the first approach, you need "to finish" the configuration.
If you don't have a running webserver (nginx, apache, etc.) (on port 80) - just change the port from 81 to 80:
ports:
- '80:80'
- '443:443'
and add the record "127.0.0.1 domain.com.dev" to your hosts file (/etc/hosts in linux).
P.S. you may change port from 88 to 80 at the second approach - it will work without changing hosts file
If you have a running wevserver on port 80 - then it is needed to you proxy directives at virtualhost config file. Here is an example:
server {
listen 80 default_server;
server_name _;
include expires.conf;
location / {
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-Proto $scheme;
proxy_pass http://172.17.0.1:81;
proxy_http_version 1.1;
}
}
2) The second approach is usually used with dnsmasq configuration.
Use this and this links to get more detailed information and examples of configuration.

Nginx, Wordpress, and SQL

I currently am trying to learn how to use docker and was wondering if there is a way to make a Docker stack that includes Wordpress, SQL, and Nginx.
Right now I want to have 3 containers running, 1 for each and use nginx as a reverse proxy for my wordpress app.
However, every time I attempt to get this stack up and running through a composer file, only Wordpress and SQL get linked, but not the Nginx.
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:fpm
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
nginx:
restart: always
image: nginx
ports:
- "80:80"
This is all I have in my docker-compose.yml
Your basic approach should work. I have a feeling there is a configuration issue somewhere, possibly with nginx that is preventing it from working as you intend.
You can try this similar docker-compose.yml file as a sample to see how it may differ from what you are doing:
docker-compose.yml
version: '2'
services:
php:
image: phpmyadmin/phpmyadmin
links:
- mysql:db
depends_on:
- mysql
mysql:
image: k0st/alpine-mariadb
volumes:
- ./data/mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=mydb
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
nginx:
image: nginx:stable-alpine
ports:
- "81:80"
volumes:
- ./nginx/log:/var/log/nginx
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/files:/var/www/nginx:ro
depends_on:
- php
nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile off;
server {
listen 80;
location / {
proxy_pass http://php;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
The nginx config is simplified but that should work for testing -- basically all it's doing is proxying the php app. Maps to port 81 to avoid conflicts on the host. (Note this is just a rough demo, would need to be fleshed out for any use more than that.)
Regarding linking, you can see that if you run: docker-compose exec mysql ping -c2 nginx to ping from the mysql container to the nginx container, you will succeed even though there are no links specified between these containers. Docker Compose will maintain those links in the default network for you.
If you like, you can fetch a working version from this repo here and run docker-compose up, and (assuming you don't have anything running on port 81) see results on http://localhost:81/ (or whatever your corresponding hostname/IP is).
For more info on Docker Compose networking see:
https://docs.docker.com/compose/networking/
By default Compose sets up a single network for your app. Each
container for a service joins the default network and is both
reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Links allow you to define extra aliases by which a service is
reachable from another service. They are not required to enable
services to communicate - by default, any service can reach any other
service at that service’s name.
You could go with the jwilder-nginx docker image. It is using docker-gen to detect containers, and will register them in nginx.conf.
This should work, if you add "VIRTUAL_HOST" the domain will be added to nginx.conf. Please note: You don't have to expose ports on WordPress with this Setup. jwilder-nginx will use default port to forward traffic.
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:fpm
links:
- db
- nginx
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
VIRTUAL_HOST: myblog.mydomain.de
nginx:
restart: always
image: jwilder/nginx-proxy
ports:
- "80:80"

Resources