wordpress home url redirects to localhost - wordpress

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.

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.

How to assign domain to a docker container

I use Docker-compose to develop the WordPress website together with Mysql and PHPMyAdmin.
Also, use Portainer to monitor them, showing the health icon, and I can access it by typing the IP address 192.168.188.80:3001, and I can curl the page from port 3001.
I try to assign the domain name for the different containers, there will have a couple of WordPress websites and I plan to host them on ports 3001, 3002, 3003...
I enable the conf file in Nginx and the conf file like
server {
listen 80;
server_name forktruck.bcsystems.nz;
location / {
proxy_pass http://0.0.0.0:3001;
}
}
The yml file is here
version: '3.8'
volumes:
wp-forktruck-data:
networks:
wp-forktruck-network:
services:
db:
image: mysql:latest
volumes:
- ./wp-forktruck-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: wordpress-forktruck
MYSQL_USER: wp-forktruck
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- 3201:3306
healthcheck:
test: curl --fail http://localhost || exit 1
interval: 300s
start_period: 5s
timeout: 10s
restart: always
networks:
- wp-forktruck-network
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
MYSQL_USER: wp-forktruck
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- 3101:80
healthcheck:
test: curl --fail http://localhost || exit 1
interval: 300s
start_period: 5s
timeout: 10s
networks:
- wp-forktruck-network
wordpress:
depends_on:
- db
image: wordpress
ports:
- 3001:80
healthcheck:
test: curl --fail http://localhost || exit 1
interval: 300s
start_period: 5s
timeout: 10s
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp-forktruck
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: wordpress-forktruck
volumes:
- ./forktruck:/var/www/
container_name: wp-forktruck
networks:
- wp-forktruck-network
but something wrong and the domain name got 520 error.
you need to add the redirect option in nginx
server {
listen 80;
server_name forktruck.bcsystems.nz;
location / {
proxy_pass http://0.0.0.0:3001;
proxy_read_timeout 90;
proxy_redirect http://0.0.0.0:3001 http://forktruck.bcsystems.nz/;
}
}
And should work, good luck
You are using Cloudflare? What is your Cloudflare DNS configs. Are you sure that the domain is pointing to public address of 192.168.188.80 (as 192.168.188.80 is private IP address and it's accessible only on your internal network but not the internet).
If you don't know your public IP address, run curl ipinfo.io on the 192.168.188.80 server to get your public IP address. And then update your Cloudflare DNS config of domain forktruck.bcsystems.nz to that address.
Note: it's easy and worked only if you are using static IP address. If not, please consider https://ngrok.com/ instead of Cloudflare.

Nginx reverse proxy deeplinks Wordpress on Docker

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...

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;

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