加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:41:14 所属栏目:Nginx 来源:网络整理
导读:我有一个全新安装的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

我有一个全新安装的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读