AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 403098
Accepted
Sergey Eremin
Sergey Eremin
Asked: 2012-06-29 07:06:29 +0800 CST2012-06-29 07:06:29 +0800 CST 2012-06-29 07:06:29 +0800 CST

使用 Nginx 限制对 MVC 应用程序的一个控制器的访问

  • 772

我有一个 MVC 应用程序,其中一个控制器只需要从多个 ips 访问(这个控制器是一个 oauth 令牌回调陷阱 - 对于 google/fb api 令牌)。我的会议看起来像这样:

geo $oauth {
    default 0;
    87.240.156.0/24 1;
    87.240.131.0/24 1;
}

server {
    listen 80;
    server_name some.server.name.tld default_server;
    root /home/user/path;
    index index.php;

    location /oauth {
        deny all;
        if ($oauth) {
            rewrite ^(.*)$ /index.php last;
        }
    }

    location / { 
        if ($request_filename !~ "\.(phtml|html|htm|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|xlsx)$") {
            rewrite ^(.*)$ /index.php last;
            break;
        }
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

它有效,但看起来不正确。

以下对我来说似乎合乎逻辑:

    location /oauth {
        allow 87.240.156.0/24;
        deny all;
        rewrite ^(.*)$ /index.php last;
    }

但是这种重写方式一直在发生,允许和拒绝指令被忽略。我不明白为什么...

rewrite nginx mvc
  • 1 1 个回答
  • 589 Views

1 个回答

  • Voted
  1. Best Answer
    kolbyjack
    2012-06-29T08:43:24+08:002012-06-29T08:43:24+08:00

    它总是重写的原因是因为重写指令是在重写阶段评估的,该阶段在访问阶段之前评估允许和拒绝。它们在文件中的出现顺序无关紧要。您可以通过两种方式解决这个问题:要么不使用 rewrite in location /oauth 将请求发送到您的前端控制器,要么在重写阶段处理源 ip。你已经在你的工作配置中做了后者,但它可以更清楚一点:

    geo $oauth_denied {
        default 1;
        87.240.156.0/24 0;
        87.240.131.0/24 0;
    }
    
    server {
        ...
    
        location /oauth {
            if ($oauth_denied) { return 403; }
            rewrite ^ /index.php last;
        }
    
        ...
    }
    

    或者:

    server {
        ...
    
        # include at server level so they're inherited by locations
        include fastcgi_params;
    
        location /oauth {
            allow 87.240.156.0/24;
            deny all;
    
            # try_files will change $uri so all the params work
            try_files /index.php =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    
        ...
    }
    
    • 2

相关问题

  • 阿帕奇虚拟主机

  • IIS 7 中的 URL 重写

  • IIS7 请求跟踪日志上的 URL_CHANGED 事件是什么?

  • 如何强制我的网址始终以 www 开头?

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve