登录后带有phpmyadmin的Nginx错误重定向

2022-01-05 00:00:00 nginx php phpmyadmin

我正在使用 nginx 设置 phpMyAdmin.我可以通过 http://localhost/phpmyadmin 访问 phpMyAdmin.但是,当我登录时,URL 被重定向到 http://localhost/sql.php 而不是 http://localhost/phpmyadmin/sql.php.

I'm setting up phpMyAdmin with nginx. I can visit phpMyAdmin at http://localhost/phpmyadmin. However, when I logged in, the URL is redirected to http://localhost/sql.php instead of http://localhost/phpmyadmin/sql.php.

我的/var/www/html/文件夹中有 phpMyAdmin 符号链接.

I have phpMyAdmin symlinked in my /var/www/html/ folder.

sudo ln -s/usr/share/phpmyadmin/var/www/html/phpmyadmin

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        include /etc/nginx/snippets/fastcgi-php.conf;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #   deny all;
    #}
}

推荐答案

今天我实际上在 StackOverflow 上经历了这么多解决方案,遗憾的是没有一个有效,有些甚至给出了一些可怕的建议.可怕的是我遇到了多少被标记为答案的问题.

I have actually been through so many solutions on StackOverflow today and sadly none of which work and some even given some horrid recommendations. What's scary is how many I came across that were marked as answers.

我刚刚做了一个全新的 Ubuntu 16.04 LEMP 服务器,今天早上一切都干净地安装了 Nginx、mySQL、PHP7.0 和 PhpMyAdmin.

I just did a brand new Ubuntu 16.04 LEMP server, everything cleanly installed this morning Nginx, mySQL, PHP7.0 and PhpMyAdmin.

这个重定向到

h**p://my.server.ip/

h**p://my.server.ip/

在登录 phpymadmin 而不是

after logging into phpymadmin instead of

h**p://my.server.ip/phpmyadmin

h**p://my.server.ip/phpmyadmin

实际上与您阅读的所有指南推荐的将 cgi.fix_pathinfo 设置为 0 无关.阅读更多关于为什么它应该在你的 php.ini 文件中设置为 0 并且不要像上面那样直接禁用它.

is nothing actually to do with the cgi.fix_pathinfo being set to 0 as recommended by all those guides you read. Read up a little more on why it should be set to 0 in your php.ini file and don't just go and disable it as above.

换句话说,在 PHP 的配置文件中保留(如向您推荐的)cgi.fix_pathinfo = 0.

So in other words leave (as recommended to you) cgi.fix_pathinfo = 0 in your config file for PHP.

修复 来自这个网站(唯一有正确答案的网站)是将以下内容添加到您的/etc/nginx/sites-available/default 配置文件中.然后重新启动 Nginx ... 立即生效,登录后不再重定向回 root.

THE FIX from this web site (the only one with the correct answer) is to add the following to your /etc/nginx/sites-available/default configuration file. Then restart Nginx ... works immediately, no more re-directing back to root after login.

# Phpmyadmin Configurations
    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:/var/run/php/php7.0-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
       }
       location ~* ^/phpmyadmin/(.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
       }
   }

   location /phpMyAdmin {
       rewrite ^/* /phpmyadmin last;
   }

相关文章