【问题标题】:nginx - php7.0-fpm php scripts no works with aliasnginx - php7.0-fpm php 脚本不适用于别名
【发布时间】:2017-01-10 19:54:28
【问题描述】:

我想创建如下路由:

  • hxxp://127.0.0.1/
  • hxxp://127.0.0.1/allegro/

怎么做? 如果我去 hxxp://127.0.0.1/allegro/scripts/test.php 我看到一个空白页。如果我去 hxxp://127.0.0.1/ php 脚本正常执行,我看到 phpinfo()

我的 nginx 配置:

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

root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;

location / {
    try_files $uri $uri/ =404;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php7.0-fpm.sock;
}

location /allegro/ {
    alias /var/www/allegro/;
    autoindex on;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
        fastcgi_pass unix:/run/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

}

【问题讨论】:

  • 尝试root /var/www; 而不是alias /var/www/allegro/;(在location /allegro/ 块内)
  • 在 127.0.0.1/allegro/script/test.php 上仍然是空白页 :(

标签: nginx php-7


【解决方案1】:

您当前的配置存在多个问题。最重要的是 URI /allegro/scripts/test.php由嵌套的位置块处理,因为正则表达式位置块优先(除非使用了 ^~ 修饰符)。

location ^~ /allegro/ {
    root /var/www;
#   autoindex on;
    try_files $uri $uri/ =404;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php7.0-fpm.sock;
        include fastcgi_params;
    }
}

^~ 修饰符确保此前缀位置在同一级别优先于正则表达式位置。详情请见this document

请注意,在适用的情况下,root 语句优先于alias 语句(有关详细信息,请参阅this document)。

fastcgi_split_path_infofastcgi_index 指令对于与路径信息不匹配 URI 的位置块是不必要的。

您需要确定include fastcgi_paramsinclude snippets/fastcgi-php.conf 是更适合FastCGI 参数的来源。

【讨论】:

  • 还是空白页:/