skip to Main Content

I have deployed WordPress on Kubernetes using Bitnami’s WordPress chart.
I wanted to host WordPress under a specific path, not on root.
To achieve that, I did the following:

  • modified wp-config.php file:
define( 'WP_HOME', 'http://example.com/wordpress/' );
define( 'WP_SITEURL', 'http://example.com/wordpress/' );
  • created Ingress resource to expose WordPress for public access:
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: my-ingress
  namespace: my-namespace
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/custom-http-errors: '404'
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  tls:
    - hosts:
        - example.com
      secretName: example-tls
  rules:
    - host: example.com
      http:
        paths:
          - path: /wordpress(/|$)(.*)
            pathType: Prefix
            backend:
              serviceName: wordpress-svc
              servicePort: http

I am able to access example.com/wordpress – I see the default WordPress site:

enter image description here

However, when I go to the Admin panel (I click the WordPress logo at the top), I am taken there, and the URL changes to "example.com/wp-admin/about.php" – normally, it should be "example.com/wordpress/wp-admin/about.php".

Even though the panel displays properly:

enter image description here

When I click on any item on the left, I am taken to the wrong site. For example, clicking on "Users" takes me to "example.com/wp-admin/users.php". I get 404. However, if I modify the URL manually and change it to "example.com/wordpress/wp-admin/users.php", the correct page loads (but the URL in my browser again changes to the wrong one, without "WordPress".

What’s the cause of that?
I don’t see any 30x in the Network tab of DevTools, so I assume that it’s some JavaScript of WordPress that modifies my address bar. How do I stop that?

One more observation – when I click on the default "Hello world" post that WordPress creates by default, it takes me to the correct URL. Only the admin panel seems to not work correctly?

2

Answers


  1. Chosen as BEST ANSWER

    The solution was to move the contents of /opts/bitnami/wordpress to /opts/bitnami/wordpress/wordpress and to remove entirely the nginx.ingress.kubernetes.io/rewrite-target annotation from the ingress.


  2. You have configured the ingress that way will will remove the WordPress from the path itself

    kind: Ingress
    apiVersion: networking.k8s.io/v1beta1
    metadata:
      name: my-ingress
      namespace: my-namespace
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/custom-http-errors: '404'
        nginx.ingress.kubernetes.io/rewrite-target: /$2
    spec:
      tls:
        - hosts:
            - example.com
          secretName: example-tls
      rules:
        - host: example.com
          http:
            paths:
              - path: /wordpress(/|$)(.*)
                pathType: Prefix
                backend:
                  serviceName: wordpress-svc
                  servicePort: HTTP
    

    You can simply try

    kind: Ingress
    apiVersion: networking.k8s.io/v1beta1
    metadata:
      name: my-ingress
      namespace: my-namespace
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/custom-http-errors: '404'
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      tls:
        - hosts:
            - example.com
          secretName: example-tls
      rules:
        - host: example.com
          http:
            paths:
              - path: /wordpress(/|$)(.*)
                pathType: Prefix
                backend:
                  serviceName: wordpress-svc
                  servicePort: http
    

    annotation

    nginx.ingress.kubernetes.io/rewrite-target: /$2
    

    which is removing the path WordPress from your ingress or URL

    You can also take reference of my WordPress YAML files : https://github.com/harsh4870/Kubernetes-wordpress-php-fpm-nginx/blob/master/ingress.yaml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search