从别名目录访问 php 时,nginx 给出 404

从别名目录访问 php 时,nginx 给出 404

我正在尝试从 apache 迁移到 nginx。我托管的 php 站点需要访问一个共享库,而这个共享库实际上是一个别名目录。下面是我提出的配置。html 文件工作正常,但 php 文件出现 404。我已阅读并尝试了大多数(如果不是全部)类似问题的答案,但均未成功。有没有提示一下,在我的情况下,是什么原因导致了这个问题?

location /wtlib/ {
    alias /var/www/shared/wtlib_4/;
    index index.php;
}

location ~ /wtlib/.*\.php$ {
    alias /var/www/shared/wtlib_4/;
    try_files $uri =404;
    if ($fastcgi_script_name ~ /wtlib(/.*\.php)$) {
        set $valid_fastcgi_script_name $1;
    }
    fastcgi_pass 127.0.0.1:9013;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/shared/wtlib_4$valid_fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
}

谢谢大家!

更新:以下内容似乎运行良好:

location /wtlib/ {
    alias /usr/share/php/wtlib_4/;
    location ~* .*\.php$ {
      try_files $uri @php_wtlib;
    }

    location ~* \.(html|htm|js|css|png|jpg|jpeg|gif|ico|pdf|zip|rar|air)$ {
      expires 7d;
      access_log off;
    }
}

location @php_wtlib {
    if ($fastcgi_script_name ~ /wtlib(/.*\.php)$) {
        set $valid_fastcgi_script_name $1;
    }
    fastcgi_pass $byr_pass;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/php/wtlib_4$valid_fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
}

答案1

您的线路try_files $uri =404;是原因,但您可以改进您的配置:

server {
  # other code ...
  index index.php;
  location /wtlib/ {
    alias /var/www/shared/wtlib_4/;
    location ~* .*\php$ {
      try_files $uri @php;
    }
  }
  location @php {
    fastcgi_pass 127.0.0.1:9013;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
  }
}

相关内容