PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] How to serve phpMyAdmin to localhost/phpMyAdmin instead of localhost:8080 using nginx in docker

 September 01, 2022     django, docker, nginx, nginx-reverse-proxy, phpmyadmin     No comments   

Issue

In my project, I am using Django and nginx, but I want to manage my cloud databases through phpmyadmin.

Django is working fine but I can't do the same with phpmyadmin because it is running in apache at localhost:8080, when I want it to run in nginx at localhost/phpmyadmin.

here is the docker-compose.yml

version: "3.9"
   
services:

  web:
    restart: always
    build:
      context: .
    env_file:
      - .env
    volumes:
      - ./project:/project
    expose:
      - 8000
      
  nginx:
    restart: always
    build: ./nginx
    volumes:
      - ./static:/static
    ports:
      - 80:80
    depends_on:
      - web

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    restart: always
    environment:
      PMA_HOST: <host_address>
      PMA_USER: <user>
      PMA_PASSWORD: <password>
      PMA_PORT: 3306
      UPLOAD_LIMIT: 300M
    ports:
      - 8080:80

and nginx default.conf

upstream django{
    server web:8000;
}

server{
    listen 80;
    location / {
        proxy_pass http://django;
    }

    location /pma/ {               
        proxy_pass http://localhost:8080/;                                 
        proxy_buffering off;                                     
    }

    location /static/ {
        alias /static/;
    }
}

I hope somebody will be able to tell me how to make nginx work as a reverse proxy for the phpMyAdmin docker container.

If some important information is missing please let me know.


Solution

You can access another docker container with its hostname and the internal port (not the exposed one). Also a rewrite of the url is necessary.

    location  ~ \/pma {
      rewrite ^/pma(/.*)$ $1 break;
      proxy_set_header X-Real-IP  $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_pass http://phpmyadmin;
    }

I tested with this docker-compose.yml:

version: "3.9"
   
services:      
  nginx:
    image: nginx:latest
    volumes:
      - ./templates:/etc/nginx/templates
    ports:
      - 80:80

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest


Answered By - VarChar42
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing