Nginx PHP MVC 403禁止

时间:2019-07-15 11:35:51

标签: php nginx

我的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 all正在工作。

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
}

1 个答案:

答案 0 :(得分:1)

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

sudo apt-get install phpx-fpm

其中x是php7.3的php版本,例如

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路线对您不起作用。

相关问题