Proxy a website from Docker through Nginx without acting as localhost

How can I proxy a website from Docker through Nginx without it acting as localhost?

My current setup consists of an Ubuntu host with a couple of Docker containers with exposed ports. The Docker containers are only exposed locally. The exposure on the Internet consists of an Nginx server that proxies the Docker containers to certain subdomains.

In one Docker container I run Wordpress. I pulled it from Docker Hub using this YML file:

version: '3.1'

services:

  wordpress:
    container_name: myapp-cms
    image: wordpress
    restart: always
    ports:
      - 8087:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - /srv/wordpress/cms:/var/www/html

  db:
    container_name: myapp-db
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - /srv/wordpress/db:/var/lib/mysql

This Docker container exposes to internal port 8087 which I then proxy to mysubdomain.mydomain.com. This is the Nginx configuration file:

server {
        root /var/www/mysubdomain.mydomain.com;
        index index.html index.htm index.nginx-debian.html;

        server_name mysubdomain.mydomain.com;

        client_max_body_size 1000M;

        location / {
                # try_files $uri $uri/ =404;
                proxy_pass      http://localhost:8087;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com-0003/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0003/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = mysubdomain.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;

        server_name mysubdomain.mydomain.com;
    return 404; # managed by Certbot
}

The problem is, that the page (in this case Wordpress) still uses localhost:8087 for every link. This destroys every possibility to click on something and also destroys CSS and JavaScript library loadings.

enter image description here

EDIT 1:

I added following two lines to the wp-config.php:

define('WP_HOME','https://mysubdomain.mydomain.com');
define('WP_SITEURL','https://mysubdomain.mydomain.com');

It successfully loads the setup page right and also adapts the domain name. I can successfully setup WordPress, but as soon as I save the setup (user name, password, email), the browser then loads an empty page. Calling curl -X GET https://mysubdomain.mydomain.com returns an empty response.


Solution 1:

Try setting Site Address and WordPress Address .

  1. Go to Settings >> General
  2. Set WordPress Address (URL) and Site Address using your fqdn

enter image description here

Reference

For more oficial information: WordPress Doc