nginx不按位置运行PHP



我应该通过example.com/phppgadmin获得phppgadmin,但是出了问题。我可以通过example.com/获得它(请参阅下面的注释中的评论)。但是,如果我试图通过在nginx config中创建location来获得phppgadmin,那么我将获得404 not found。我究竟做错了什么?Error.log很好。

这是nginx Conf:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com;
        #This path works. We are getting phppgadmin by example.com/ , 
        #but I need to get it by location (example.com/phppgadmin):
        #root /usr/share/phppgadmin;
        #This should work but it doesn't:
        location /phppgadmin/ {  
        root /usr/share;
        }
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        location ~ .php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include fastcgi_params;
        }
}

如果将root /usr/share;放置在server块中(原始root语句),则URI example.com/phppgadmin/将按预期工作。

但是,它也将暴露/usr/share目录的全部内容,您可能不需要。

您可以将root语句放入location中,但您需要包括处理请求所需的所有指令。

例如:

location ^~ /phppgadmin/ {  
    root /usr/share;
    try_files $uri $uri/ =404;
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        include fastcgi_params;
    }
}

^~修饰符以.php结尾的URI避免了任何歧义。有关详细信息,请参见此文档。

相关内容

  • 没有找到相关文章

最新更新