首页 > 解决方案 > nginx 限制或重定向请求通过根目录中的 index.php

问题描述

我的项目要求是从 Slim 框架 (index.php) 启动一个 angular(v6)/ionic(v4) 应用程序(index.html)。根文件夹结构如下图所示: 在此处输入图像描述

'www' 是 Slim index.php 所在的根目录。在“www”内是添加了角度构建文件(index.php)的应用程序文件夹。要求是所有请求都应该通过根文件夹中的 Slim index.php,并且基于一些会话逻辑,我们必须路由或启动 Angular 应用程序(index.html)。

现在,“ https://domain/ ”通过 index.php。但是,“ https://domain/app/ ”直接启动 Angular 应用程序 (index.html)。

如何配置 nginx 以便 Slim index.php 在根目录中处理所有请求?

server {
    server_name <domain>;

    root /var/www/<some name>/public/www/;
    index index.php index.html index.htm;

    access_log  /var/log/nginx/<some name>.log;
    error_log   /var/log/nginx/<some name>.log;

    sendfile off;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ index.php /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 36000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

谢谢

标签: nginxslimnginx-locationnginx-config

解决方案


您可以像这样限制对app目录的访问

location /app {
    deny all;
    return 404; #show not found instead of 403
}

或者您可以通过发送重定向来覆盖 403 处理https://example.com/

location /app{
     deny all;
     error_page 403 https:/domain.com/;  #redirect to your main directory
 }

推荐阅读