【问题标题】:Working with alias inside location在位置内使用别名
【发布时间】:2016-06-01 20:22:14
【问题描述】:

这是我的服务器块

server {
    listen      80;
    server_name domain.tld;
    root        /var/www/domain.tld/html;
    index       index.php index.html index.htm;

    location / {
    }

    location /phpmyadmin {
        alias /var/www/phpmyadmin;
    }

    location /nginx_status {
        stub_status on;
        access_log  off;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

浏览http://domain.tld/index.php 工作正常,我遇到的唯一问题是浏览http://domain.tld/phpmyadmin/。它返回 404 但文件夹 /var/www/phpmyadmin 存在于服务器上。查看 /var/log/nginx/error.log,那里没有记录错误,但对它的访问记录在 /var/log/nginx/access.log 中。这可能是什么问题?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    问题在于 phpmyadmin 是一个 PHP 应用程序,而您的 location ~ \.php$ 块没有指向正确的文档根目录。

    您需要构建两个具有不同文档根目录的 PHP 位置。

    如果 phpmyadmin 位于/var/www/phpmyadmin,则不需要alias 指令,因为root 指令会更有效。见this document

    server {
        listen      80;
        server_name domain.tld;
        root        /var/www/domain.tld/html;
        index       index.php index.html index.htm;
    
        location / { 
        }
    
        location /nginx_status {
            stub_status on;
            access_log  off;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
        location ^~ /phpmyadmin {
            root /var/www;
    
            location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }
    }
    

    location ^~ /phpmyadmin 是一个前缀位置,它优先于通常用于处理.php 文件的正则表达式位置。它包含一个location ~ \.php$ 块,该块继承了文档根的/var/www 值。

    建议在定义其他fastcgi_param 参数之前先include fastcgi_params,否则您的自定义值可能会被静默覆盖。

    请参阅this document 了解更多信息。

    【讨论】:

    • 嗨,如果,我希望位于/var/www/phpmyadmin 的phpmyadmin 可以通过http://domain.tld/sql 访问
    • 您可以添加内部重写 location /sql { rewrite ^/sql(.*)$ /phpmyadmin$1 last; } 并保持上述内容不变。
    猜你喜欢
    • 2014-05-19
    • 2014-06-28
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多