douping5226 2015-06-29 21:23
浏览 689
已采纳

Nginx阻止根目录

I have nginx. There is a site in it, the root of the site is /root/SITE In this SITE directory i have some .php files, and few directories. How can i deny the access ONLY to the .php files in the /root/SITE directory (i mean that in those few directories that are in /root/SITE .php files shouldn't be blocked)

  • 写回答

1条回答 默认 最新

  • doxn43207 2015-06-29 22:51
    关注

    So you're asking that for this server block in your Nginx configuration, only PHP files should be allowed, everything else would get a 404? There's lots of ways to do this (and it would be better to ask in a more Nginx-specific place) - here's just one way:

    server {
        listen 1.2.3.4:80;
        server_name example.com;
        root /root/SITE;
        location ~ \.php$ {
            include fastcgi.conf;
        }
        location ~ .* {
            return 404;
        }
    }
    

    Note that using this method, the order of the location blocks is important, since first regex location wins. Also, you can use the case-insensitive location regex operator ~* if you need to support .PHP files for some reason.

    Keep in mind this means that image files and CSS files and anything else will be denied. You'll only be able to serve images and stylesheets if you push them through (or create them in) PHP. This also will not allow you to access index.php by browsing to the root of the site with no trailing filename.

    I suggest that you may have needs that can be satisfied in a different, better way. You should more fully explain what you are trying to do. What kind of security are you trying to create for your application?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏