Fixing Nginx Redirect Issue: phpMyAdmin Login Redirects to Root Instead of /phpmyadmin


30 views

When accessing phpMyAdmin through Nginx, users frequently encounter an unexpected redirect behavior. After successful authentication at /phpmyadmin, the system incorrectly redirects to the root location (/index.php) instead of maintaining the /phpmyadmin path prefix.

The issue stems from two configuration problems in the Nginx setup:

  1. Incorrect root path specification in the phpMyAdmin location block
  2. Missing proper SCRIPT_FILENAME fastcgi parameter for subdirectory installations

Here's the corrected server configuration for phpMyAdmin:

server {
    listen 80;
    server_name _;

    location / {
        root /usr/share/nginx/html;
        index index.php index.html index.htm;
    }

    location /phpmyadmin {
        alias /usr/share/phpmyadmin/;
        index index.php;
        
        location ~ ^/phpmyadmin/(.+\.php)$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }

        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            access_log off;
            expires 30d;
        }
    }
}
  • Replaced root with alias for the phpMyAdmin location to handle subdirectory paths correctly
  • Simplified the static files handling block
  • Ensured consistent fastcgi_param SCRIPT_FILENAME using $request_filename
  • Removed redundant root declarations in nested locations

For enhanced security and performance:

location /phpmyadmin {
    # Restrict access by IP
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    deny all;

    # Security headers
    add_header X-Frame-Options "DENY";
    add_header X-Content-Type-Options "nosniff";
    
    # Cache control for static assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        access_log off;
    }
}

After making changes, always verify your configuration:

nginx -t
systemctl restart nginx

The login redirect should now properly maintain the /phpmyadmin path prefix, and all subsequent navigation within phpMyAdmin will work correctly.


When configuring phpMyAdmin with Nginx, a common issue occurs where successful login redirects users to the root path (/index.php) instead of maintaining the /phpmyadmin path in the URL. This happens due to incorrect path handling in the Nginx configuration.

The core issue lies in how Nginx processes the FastCGI parameters and document root paths. Let's examine the problematic parts:

location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /usr/share/;
        fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }
}

We need to modify both the root path handling and FastCGI parameters. Here's the corrected configuration:

location /phpmyadmin {
    alias /usr/share/phpmyadmin/;
    index index.php;
    
    location ~ ^/phpmyadmin/(.*\.php)$ {
        alias /usr/share/phpmyadmin/$1;
        fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
        
        # Critical fix for redirects
        fastcgi_param SCRIPT_NAME /phpmyadmin$fastcgi_script_name;
        fastcgi_param REQUEST_URI /phpmyadmin$uri;
    }
    
    location ~* ^/phpmyadmin/(.*\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        alias /usr/share/phpmyadmin/$1;
    }
}

The solution involves several important modifications:

  • Replaced root with alias for precise path mapping
  • Added explicit SCRIPT_NAME and REQUEST_URI FastCGI parameters
  • Fixed the path concatenation for PHP files
  • Ensured static assets are properly served

For enhanced security and functionality, consider adding these to your Nginx config:

# Deny access to sensitive files
location ~ /phpmyadmin/(config|tmp|setup|vendor) {
    deny all;
}

# Allow larger file uploads
client_max_body_size 100M;
client_body_buffer_size 128k;

After implementing these changes:

  1. Restart Nginx: sudo systemctl restart nginx
  2. Clear browser cache and cookies
  3. Verify the login redirect now maintains the /phpmyadmin path
  4. Check all phpMyAdmin functionality works correctly

If issues persist:

# Check error logs
tail -f /var/log/nginx/error.log

# Verify PHP-FPM is running
systemctl status php-fpm

# Test FastCGI connection
SCRIPT_NAME=/phpmyadmin/index.php \
SCRIPT_FILENAME=/usr/share/phpmyadmin/index.php \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect /tmp/php5-fpm.sock