【问题标题】:nginx Restrict or Redirect requests to go through index.php in the rootnginx 限制或重定向请求通过根目录中的 index.php
【发布时间】:2018-12-11 06:28:10
【问题描述】:

我的项目要求是从 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;
    }
}

谢谢

【问题讨论】:

  • Mybe,您可以限制对 app 文件夹的访问,或将所有 https://domain/app/ 请求重定向到 https://domain,如果这能解决您的问题,我可以给您代码
  • 嗨@ABDELLATIFLAKEHAL,如果你能分享代码会很有帮助。我的要求是限制对 app 文件夹的访问并重定向到 root。我尝试过重定向和重写但没有成功,但这可能是因为我不专业。

标签: nginx slim nginx-location nginx-config


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 2021-01-30
    • 1970-01-01
    • 2017-01-12
    • 2013-04-10
    • 2013-03-06
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多