nginx不按位置运行php

时间:2019-02-20 07:39:39

标签: nginx phppgadmin

我应该通过phppgadmin得到example.com/phppgadmin,但是出了点问题。我可以通过example.com/来获得它(请参阅下面的conf中的注释)。但是,如果我尝试通过在nginx配置中创建phppgadmin来获得location,那么我得到的是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;
        }
}

1 个答案:

答案 0 :(得分:0)

如果将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产生任何歧义。有关详细信息,请参见this document