php - 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
}
解决方案
如果您在 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 的其他路线不适合您。
推荐阅读
- android - 安装 react-native-image-crop-picker 后构建失败
- java - 使用改造将文件上传为多部分/表单数据会给出 400 错误代码
- c - 没有使用 hal_Driver 的 USART 通信
- spring-boot - 使用 okta 实现 RBAC
- ios - 如何将内容与 SwiftUI 中的导航栏标题对齐?
- javascript - 如何解决“无法对未安装的组件执行反应状态更新”
- r - R:为所有变量格式化双向列表,只改变一个
- javascript - v-model 取决于条件
- css - 将居中的自定义复选框与 Flexbox 对齐
- javascript - 在特定尺寸的屏幕中禁用 javascript 功能