8

I got some trouble: pgadmin working perfect behind nginx in location /, but it wont work behind location /pgadmin Work great:

location / {
         proxy_http_version 1.1;
         proxy_set_header X-Real-IP  $remote_addr;
         proxy_set_header X-Forwarded-For $remote_addr;
         proxy_set_header Host $host;

         proxy_pass         http://127.0.0.1:5050;
}

Wont work:

location /pgadmin {
         proxy_http_version 1.1;
         proxy_set_header X-Real-IP  $remote_addr;
         proxy_set_header X-Forwarded-For $remote_addr;
         proxy_set_header Host $host;

         proxy_pass         http://127.0.0.1:5050;
}

May be i need some specific rewrite?

3 Answers3

5

For version pgAdmin 4 v3.0, until the issue is actually fixed, here's a quick command-line hack based on this.

cat > quickfix.txt <<THE_END
class ReverseProxied(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        script_name = environ.get("HTTP_X_SCRIPT_NAME", "")
        if script_name:
            environ["SCRIPT_NAME"] = script_name
            path_info = environ["PATH_INFO"]
            if path_info.startswith(script_name):
                environ["PATH_INFO"] = path_info[len(script_name):]
        scheme = environ.get("HTTP_X_SCHEME", "")
        if scheme:
            environ["wsgi.url_scheme"] = scheme
        return self.app(environ, start_response)
app.wsgi_app = ReverseProxied(app.wsgi_app)

THE_END

sudo sed -i '/app = create_app()/r quickfix.txt' /usr/local/lib/python3.5/dist-packages/pgadmin4/pgAdmin4.py
rm quickfix.txt

The commands above insert a piece of code into the file /usr/local/lib/python3.5/dist-packages/pgadmin4/pgAdmin4.py, right after the line app = create_app().

Also, make sure the path to pgAdmin4.py on your system is correct. You may need to adjust the snippet above.

Then, configure nginx as follows:

location /pgadmin-web/ {
        proxy_pass http://127.0.0.1:5050/;
        proxy_redirect      off;
        proxy_set_header    Host                    $host;
        proxy_set_header    X-Real-IP               $remote_addr;
        proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto       $scheme;
        proxy_set_header    X-Script-Name           /pgadmin-web;
}

For reference, also have a look at pgAdmin4.py on GitHub.

turdus-merula
  • 8,546
  • 8
  • 38
  • 50
  • 1
    Did you ever send in a patch for this dearly missed functionality? The tool is almost useless in Docker containers without proper support for subpaths. I'd expect to be able to just pass `PGADMIN_PATH_PREFIX=/pgadmin4` in an env variable.... – Niels Keurentjes Jun 28 '18 at 21:04
  • It looks like 4.4 will contain this fix to add support for SCRIPT_NAME as per https://www.postgresql.org/message-id/CA%2BOCxozKrZb%2BMMbmvVYHPRr28CZgFCSfTQ%3DN9HiySxCuxNX_HQ%40mail.gmail.com. – Shaheed Haque Mar 04 '19 at 18:31
  • 1
    With latest pgadmin4, below NGINX config solves the problem. ```proxy_set_header X-Script-Name /pgadmin-web;``` – AndyC Dec 05 '22 at 05:36
  • @AndyC can confirm, this works perfectly, thanks – rnd om Dec 27 '22 at 22:40
5

The fix seems to be not needed anymore.

According to Redmine issue, SCRIPT_NAME env var could be used (at least with latest official docker image).

Docker Compose snippet from the issue (working for me):

version: "3" 

services:

  pgadmin4:

    image: dpage/pgadmin4:latest

    environment:
      - PGADMIN_DEFAULT_EMAIL=bla@bla.com
      - PGADMIN_DEFAULT_PASSWORD=thepwd
      - SCRIPT_NAME=/pgadmin4

    volumes:
      - pgadm:/var/lib/pgadmin

    labels:
      - "traefik.enable=true" 
      - "traefik.backend=pgadmin4" 
      - "traefik.frontend.priority=600" 
      - "traefik.frontend.rule=Method:GET" 
      - "traefik.frontend.rule=PathPrefix:/pgadmin4"      

  postgis:
    image: mdillon/postgis:9.6-alpine

    volumes:
      - pgdb:/var/lib/postgresql/data

    expose:
      - 5432

    env_file:
      - pg.env

    labels:
      - "traefik.enable=false" 

volumes:
  pgdb:
  pgadm:
cypx
  • 61
  • 1
  • 1
  • 1
    Using `SCRIPT_NAME=/pgadmin` worked for me, however, for use within Rancher 1.6 I only had to set `traefik.enable=true`, `traefik.frontend.rule=Host:xxx;PathPrefix:/pgadmin` and `traefik.port=80`, everything else broke it to some extend. – mindm49907 Apr 10 '19 at 12:03
  • Yes `SCRIPT_NAME` env **works** with [latest pgadmin 4.11](https://hub.docker.com/r/dpage/pgadmin4). Easier to proxy behind Traefik. [uWSGI+Nginx setup](https://www.pgadmin.org/docs/pgadmin4/development/server_deployment.html#nginx-configuration-with-uwsgi) is an overkill just to use a subfolder. – visitsb Aug 21 '19 at 00:12
  • 1
    Adding SCRIPT_NAME like mentioned in these examples gives me a 500 internal server error. I'm also using Traefik and PgAdmin 4.11 and I already have other applications running behind Traefik. Anyone has another suggestion what could be wrong? – Kenny Sep 23 '19 at 10:19
2

This was a bug in pgAdmin4 version 1.6, It is fixed now and will be available in next release.

Ref: Link

Murtuza Z
  • 5,639
  • 1
  • 28
  • 52