|
| 1 | +--- |
| 2 | +title: Running Behind a Reverse Proxy |
| 3 | +--- |
| 4 | + |
| 5 | +When running pgwatch in production environments, you may want to expose it through a reverse proxy (like Apache, Nginx, or Traefik) on a different path instead of exposing its port directly. This guide shows you how to configure pgwatch for such setups. |
| 6 | + |
| 7 | +## Configuration |
| 8 | + |
| 9 | +### pgwatch Configuration |
| 10 | + |
| 11 | +Use the `--web-base-path` option to specify the base path under which pgwatch should serve its content: |
| 12 | + |
| 13 | +```bash |
| 14 | +pgwatch --web-base-path=pgwatch --web-addr=:8432 |
| 15 | +``` |
| 16 | + |
| 17 | +Or using environment variables: |
| 18 | + |
| 19 | +```bash |
| 20 | +export PW_WEBBASEPATH=pgwatch |
| 21 | +export PW_WEBADDR=:8432 |
| 22 | +pgwatch |
| 23 | +``` |
| 24 | + |
| 25 | +The web UI automatically adapts to the configured base path without requiring a rebuild. |
| 26 | + |
| 27 | +WebSockets are used for live log streaming. Make sure your reverse proxy is configured to support WebSocket connections. |
| 28 | + |
| 29 | +### Apache |
| 30 | + |
| 31 | +The `mod_proxy_wstunnel` module is required for the WebSocket proxy. |
| 32 | + |
| 33 | +```apache |
| 34 | +<VirtualHost *:443> |
| 35 | + ServerName example.com |
| 36 | +
|
| 37 | + # Other SSL and domain configuration... |
| 38 | +
|
| 39 | + ProxyPass /pgwatch/log ws://localhost:8432/pgwatch/log |
| 40 | + ProxyPassReverse /pgwatch/log ws://localhost:8432/pgwatch/log |
| 41 | +
|
| 42 | + ProxyPass /pgwatch/ http://localhost:8432/pgwatch/ |
| 43 | + ProxyPassReverse /pgwatch/ http://localhost:8432/pgwatch/ |
| 44 | + ProxyPreserveHost On |
| 45 | +
|
| 46 | + <Location /pgwatch/> |
| 47 | + # Optional: Add authentication |
| 48 | + AuthUserFile /etc/apache2/admpasswd |
| 49 | + AuthType Basic |
| 50 | + AuthName "pgwatch Administration" |
| 51 | + <RequireAll> |
| 52 | + Require valid-user |
| 53 | + </RequireAll> |
| 54 | + </Location> |
| 55 | +</VirtualHost> |
| 56 | +``` |
| 57 | + |
| 58 | +### Nginx |
| 59 | + |
| 60 | +WebSocket support is automatic with the proxy configuration shown here. Nginx will upgrade the connection when needed. |
| 61 | + |
| 62 | +```nginx |
| 63 | +server { |
| 64 | + listen 443 ssl; |
| 65 | + server_name example.com; |
| 66 | +
|
| 67 | + # Other SSL and domain configuration... |
| 68 | +
|
| 69 | + location /pgwatch/ { |
| 70 | + proxy_pass http://localhost:8432/pgwatch/; |
| 71 | + proxy_set_header Host $host; |
| 72 | + proxy_set_header X-Real-IP $remote_addr; |
| 73 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 74 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 75 | +
|
| 76 | + # Optional: Add authentication |
| 77 | + auth_basic "pgwatch Administration"; |
| 78 | + auth_basic_user_file /etc/nginx/.htpasswd; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Testing the Configuration |
| 84 | + |
| 85 | +After configuring your reverse proxy and starting pgwatch: |
| 86 | + |
| 87 | +1. Verify the web UI loads: `https://example.com/pgwatch/` |
| 88 | +2. Check that API endpoints work: `https://example.com/pgwatch/readiness` |
| 89 | +3. Test WebSocket log streaming in the UI's Logs page |
| 90 | +4. Verify that navigation between pages works correctly |
| 91 | + |
| 92 | +## Common Issues |
| 93 | + |
| 94 | +### Static Assets Not Loading |
| 95 | + |
| 96 | +Make sure: |
| 97 | + |
| 98 | +- The reverse proxy's `ProxyPass` directive includes the base path |
| 99 | +- Both backend (`--web-base-path`) and frontend use the same path (frontend reads it dynamically from backend) |
| 100 | + |
| 101 | +### WebSocket Connection Failures |
| 102 | + |
| 103 | +Ensure: |
| 104 | + |
| 105 | +- Your reverse proxy supports WebSocket upgrades |
| 106 | +- The `/log` endpoint is properly proxied |
| 107 | +- No firewall rules are blocking WebSocket connections |
| 108 | + |
| 109 | +### Authentication Issues |
| 110 | + |
| 111 | +If using both reverse proxy authentication and pgwatch's built-in authentication: |
| 112 | + |
| 113 | +- The reverse proxy authentication happens first |
| 114 | +- Users must authenticate twice (proxy, then pgwatch login) |
| 115 | +- Consider disabling pgwatch authentication if using proxy-level auth |
0 commit comments