1

RESOLVED The fix was to add proxy_set_header Host $host; to my nginx file in the location block.

___________

When I try to reach /wp-admin I am redirected to the container's name instead of my site URL but cannot see any reason why this is happening. I can log in to /wp-login.php and use the site's admin pages that way though.

I have checked site_url and home in wp_options which are both correct. Here is my docker-compose.yaml

version: '3'
services:
  nginx:
    image: "nginx"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/log/nginx:/var/log/nginx
      - /opt/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /opt/nginx/sites-enabled:/etc/nginx/sites-enabled
      - /opt/nginx/ssl:/etc/nginx/ssl
    depends_on:
      - "wpcontainer"
    networks:
      - frontend
      - backend
  wpcontainer:
    image: "wordpress"
    networks:
      - frontend
      - backend
    volumes:
      - /opt/wordpress/html:/var/www/html
    depends_on:
      - "wpdatabase"
    environment:
      - WORDPRESS_DB_USER=dbuser
      - WORDPRESS_DB_PASSWORD=dbpass
      - WORDPRESS_DB_NAME=dbname
      - WORDPRESS_DB_HOST=wpdatabase
  wpdatabase:
    image: "mariadb"
    volumes:
      - /opt/wordpress/database:/var/lib/mysql
    networks:
      - backend
    environment:
      - MYSQL_ROOT_PASSWORD=dbroot
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=dbpass
      - MYSQL_DATABASE=dbname

networks:
  frontend:
  backend:
   internal: true

The redirection from mysite/wp-admin to wpcontainer/wp-admin happens on both http and https. This is my nginx config which is saved as a file in sites-enabled

server {
 listen 80;
 server_name mywebsite.tld mywebsite.tld.tld www.mywebsite.tld www.mywebsite.tld.tld;
 access_log /var/log/nginx/mywebsite-access.log;
 error_log /var/log/nginx/mywebsite-error.log;
 return 302 https://mywebsite.tld$request_uri;
}

server {
 listen 443 ssl;
 server_name mywebsite.tld www.mywebsite.tld;
 ssl_certificate /etc/nginx/ssl/mywebsite.pem;
 ssl_certificate_key /etc/nginx/ssl/mywebsite.key;

 access_log /var/log/nginx/mywebsite-ssl-access.log;
 error_log /var/log/nginx/mywebsite-ssl-error.log;
 location / {
  proxy_pass http://wpcontainer:80;
 }
}

server {
 listen 443 ssl;
 server_name mywebsite.tld.tld www.mywebsite.tld.tld;
 ssl_certificate /etc/nginx/ssl/mywebsite.pem;
 ssl_certificate_key /etc/nginx/ssl/mywebsite.key;
 return 302 https://mywebsite.tld$request_uri;
}

My question is, how can I prevent the redirect from occurring or make it point to the correct place?

EDIT: I've discovered that visiting /wp-admin/ works while /wp-admin does not. This problem is the same for /contact/ vs /contact where the former will display a form while the latter will 301 redirect to http[s]://wpcontainer/contact/

By using https://redirectdetective.com/ I can see that the site is 301 redirecting URLs without a trailing slash to the container name with the location and a trailing slash. I believe the actual redirection is being done by Wordpress itself because I only have 302s setup in nginx.

  • So this only happens when trying to access /wp-admin/? Is the WP_HOME and/or WP_SITEURL constant set? We have most WP instances in docker environments, this is definitely not the default behaviour – kero Jan 4 at 19:44
  • I have checked site_url and home in wp_options which are both correct and have not defined WP_HOME or WP_SITEURL in wp-config.php. This is a migration from a standard setup on my old host to a Docker deployment on a new VPS so I did have to replace the old URLs in the SQL with the new domain but that all seems to work fine. It's just /wp-admin/ redirecting to the container's name that is confusing me. I've never even heard of a case where Wordpress redirects to the actual hostname instead of the configured URL before. – ADeveloper Jan 5 at 1:27
  • This seems very strange to me. And honestly, probably "too localized" for WordPress.StackExchange - but nevertheless: It redirects to https?://wpcontainer/, right? http or https or the same protocol you request it? /wp-admin/ or /wp-login.php(+ return sth.)? You did a db and file search for wpcontainer and both came back negative? When you write "I can log in ..." - you mean via container name or 127.0.0.1/local domain? – kero Jan 5 at 3:49
  • In FF it redirects to the same protocol that made the request, in Brave is points to HTTPS. You're right, it must be my setup but I don't know where I've gone wrong. I can log in by going to mywebsitedomain.tld/wp-login.php and its dashboard then uses URLs under wp-admin fine. I can also log in by going directly to mywebsitedomain.tld/wp-admin/admin.php – ADeveloper Jan 5 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.