首页 > 解决方案 > Nginx php mvc 403 禁止

问题描述

我的 nginx 服务器不允许子文件夹上的 php 文件给出 403

我有一个运行良好的 .htaccess 的 apache

{
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?path=$1 [QSA,L]
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
}

文件夹结构是这样的:

-root
-- Classes
-- Controllers
-- Includes
-- Models
-- Views
--- Home
---- home-view.php
-Index.php

索引在浏览器上运行并打开,但是在一个带有指向 \views\home\ 的链接的按钮中,应该通过控制器加载 home-view.php,它给了我一个 403 禁止,在 apache 上一切正常。

nginx conf是:

root /var/www/html;
index index.php index.html index.htm;
server_name cityguide.com wwwcityguide.com

location / {
  if (!-e $request_filename){
    rewrite ^(.+)$ /index.php?path=$1 break;
  }
  rewrite "^/(.*)\.[\d]{10}\.(css|js)$" /$1.$2 break;
  try_files $uri $uri/ =404
}

标签: phpnginx

解决方案


如果您在 Nginx 中有 php-fpm 简单提供路径,如下例所示。如果没有,您可以使用以下命令安装 php-fpm

sudo apt-get install phpx-fpm

其中 x 是 php 的版本,例如对于 php7.3 它应该是

sudo apt-get install php7.3-fpm

然后在您的配置文件中添加以下行

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

然后你的文件看起来像(考虑到你使用的是php7.3)

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

    . . .

    root /var/www/your folder path;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

另外,不要忘记try_files $uri $uri/ /index.php?$query_string;将此行添加到您的位置,否则 MVC 的其他路线不适合您。


推荐阅读