Configuring NGinx to act as a Reverse Proxy for PHPMyAdmin

In a previous post, I detailed how to Use NGinx to serve static files and Apache for dynamic as well as the minor tweaks you need to make to have it work nicely with Joomla.

One thing I didn't cover, though, is setting up PHPMyAdmin. This documentation isn't going to go into the detail of installing and configuring PHPMyAdmin as there's plenty of that available elsewhere on the web. What we will discuss, though, is the NGinx configuration changes you need to make to have the connection reverse proxied to Apache.

These steps only really apply if you've gone for a system-wide installation of PMA. If you've unpacked into a web-accessible directory then you probably don't need to make any changes!

Why not run in NGinx?

There's actually no reason you couldn't run PHPMyAdmin directly through NGinx but it does mean setting up the PHP extensions. If you're reverse proxying to Apache anyway, there's presumably a reason you've opted to have Apache handle PHP related stuff and it'd be silly to have to maintain two sets of PHP aware servers.

 

Changes needed

Assuming you followed the configuration in my earlier tutorial, most of the work is already done for you. There is likely to be one exception though: when you log into PMA you may find that none of the images load, as NGinx isn't passing on these requests and just returns a 404 instead. The same goes for if you try and access any of the documentation, because it's a static file, NGinx attempts to serve the request itself but fails.

We'll assume that Apache is configured to pass all requests to /phpmyadmin to the application, obviously if you've gone for a different directory name you'll need to adjust accordingly.

 

Implementation

Basically, we simply need to tell NGinx to proxy anything under /phpmyadmin. So within our server block, we add a location declaration

location ~phpmyadmin* {
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:8080;
}

So the following server block

server {
listen   80;
root /var/www/vhosts/example.com;
index index.php index.html index.htm;
server_name example.com;
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {}

location ~* {
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:8080;
}

location ~ /\.ht {
deny all;
}

}

Would become

server {
listen   80;
root /var/www/vhosts/example.com;
index index.php index.html index.htm;
server_name example.com;

location ~phpmyadmin* {
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:8080;
}

location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {}

location ~* {
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:8080;
}

location ~ /\.ht {
deny all;
}

}

Reload your NGinx configuration

service nginx reload

You should now find that images and documentation within PHPMyAdmin load correctly.