NGINX目录中的PHP别名不起作用

前端之家收集整理的这篇文章主要介绍了NGINX目录中的PHP别名不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个全新安装的Ubuntu 14.04服务器.我已经安装了Nginx,PHP等….

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

  server_name testone.local;

  root /var/www/htmlone;
  index index.html;

  # pass the PHP scripts to FastCGI server listening on the PHP-fpm socket
  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;
  }

  location /alias {
    alias  /var/www/htmlalias;
    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;
    }
  }
}

如果我在/ var / www / htmlone中使用一个简单的PHP脚本PHP按预期执行.如果我在/ var / www / htmlalias中使用相同的脚本,它不会按预期执行.如果我在/ var / www / htmlalias中放置一个HTML脚本,它会按预期显示,所以别名充当别名,但不执行PHP文件,但PHP在主根目录中工作.

我在许多服务器故障问题上发现这个常规设置应该可以工作但事实并非如此.有没有人看到我可能做错的事情?我在错误日志中看不到任何消息.

我应该添加这是针对Nginx版本:Nginx / 1.8.0

最佳答案
您遇到的问题实际上是long-standing bug that was filed 3 years ago,导致别名和try_files无法真正协同工作.

错误页面上有一个workaround by Luke Howell,如下所示:

location /api { ## URL string to use for api ##
    alias /home/api/site_files/; ## Site root for api code ##

    ## Check for file existing and if there,stop ##
    if (-f $request_filename) {
        break;
    }

    ## Check for file existing and if there,stop ##
    if (-d $request_filename) {
        break;
    }

    ## If we get here then there is no file or directory matching request_filename ##
    rewrite (.*) /api/index.PHP?$query_string;

    ## Normal PHP block for processing ##
    location ~ \.PHP${
        fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
        fastcgi_pass unix:/var/run/PHP5-fpm.sock;
        fastcgi_index index.PHP;
        include fastcgi_params;
    }
}

请注意,就Nginx而言,IF is evil and should be avoided if possible.

猜你在找的Nginx相关文章