# Block access to a file!

**URL:** <https://www.ruby-forum.com/t/block-access-to-a-file/220349>\
**Category:** NGINX\
**Created:** [June 6, 2012, 3:23pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349 "2012-06-06T15:23:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 6, 2012, 3:23pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/1 "2012-06-06T15:23:23Z")

</div>

Hi,  
it must be a piece of cake but i can not find a soloution…

the problem is " location admin.php". you can check it below…

i just want to allow access to admin.php to some ip address and deny  
others.

if i remove allow line from it it deny all; but when I add allow line it  
dont work …  
please help me…  
I tried too many syntax but no success.  
i even add root and index to this location but browser prompt to  
download my php file…

please help

here 's my server section of config…very simple  
server {  
listen 80;  
server\_tokens off;  
server\_name [www.mysite.com](http://www.mysite.com);  
access\_log /var/log/nginx/mysite.log;  
error\_page 404 /404.html;  
error\_page 500 502 503 504 /50x.html;

```
    location / {
       root /usr/share/nginx/www;
       index index.php;
       client_max_body_size 8m;
       client_body_buffer_size 256k;
    }
   location = /50x.html {
       root /usr/share/nginx/html;
    }
    location = /404.html {
       root /usr/share/nginx/html;
    }
    location ~ /\.ht {
        deny all;
    }
    location ~ \.php$ {
        root /usr/share/nginx/www;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
set $ssl off;  
if ($ssl\_protocol != “” ) {  
set $ssl on;  
}  
fastcgi\_param HTTPS $ssl;  
}

```
    location ~ admin.php {
         allow 217.66.196.193;
         deny all;
     }

```

Posted at Nginx Forum:

> **[block access to a file !!](https://forum.nginx.org/read.php?2,227281,227281#msg-227281)**
>
> Hi, it must be a piece of cake but i can not find a soloution... the problem is " location admin.php". you can check it below... i just want to allow access to admin.php to some ip address and deny others. if i rem

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 6, 2012, 3:41pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/2 "2012-06-06T15:41:30Z")

</div>

On Wed, Jun 06, 2012 at 09:22:50AM -0400, torajx wrote:

Hi there,

> it must be a piece of cake but i can not find a soloution…
> 
> the problem is " location admin.php". you can check it below…

No, the problem is “location ~ admin.php” coming after “location ~  
.php$”. The exact characters matter.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

> i just want to allow access to admin.php to some ip address and deny  
> others.

Either use something like “location = /admin.php”, or maybe reorder to  
regex locations (with ~) so they match in the order you want.

You will (probably) want to repeat your fastcgi\_pass and other fastcgi\_\*  
config lines inside the new location block. One request is handled by  
one location.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 6, 2012, 3:51pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/3 "2012-06-06T15:51:27Z")

</div>

thank you for reply…  
i change it to "location = /admin.php "  
and move it above the "location ~ .php$ "  
it blocks other IPs but give file not found error for allowed Ips…

the last time I added root to admin.php location the browser promot me  
to download admin.php…

what can I do now ?

Posted at Nginx Forum:

> **[Re: block access to a file !!](https://forum.nginx.org/read.php?2,227281,227284#msg-227284)**
>
> Hi, it must be a piece of cake but i can not find a soloution... the problem is " location admin.php". you can check it below... i just want to allow access to admin.php to some ip address and deny others. if i rem

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 9, 2012, 8:44am UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/4 "2012-06-09T08:44:52Z")

</div>

thank you again; i will test and inform you here…

Posted at Nginx Forum:

> **[Re: block access to a file !!](https://forum.nginx.org/read.php?2,227281,227376#msg-227376)**
>
> Hi, it must be a piece of cake but i can not find a soloution... the problem is " location admin.php". you can check it below... i just want to allow access to admin.php to some ip address and deny others. if i rem

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 10, 2012, 4:54pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/5 "2012-06-10T16:54:05Z")

</div>

Thank you ! it worked great…

Posted at Nginx Forum:

> **[Re: block access to a file !!](https://forum.nginx.org/read.php?2,227281,227400#msg-227400)**
>
> Hi, it must be a piece of cake but i can not find a soloution... the problem is " location admin.php". you can check it below... i just want to allow access to admin.php to some ip address and deny others. if i rem

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 6, 2012, 4:32pm UTC](https://www.ruby-forum.com/t/block-access-to-a-file/220349/6 "2012-06-06T16:32:23Z")

</div>

On Wed, Jun 06, 2012 at 09:50:57AM -0400, torajx wrote:

> thank you for reply..  
> i change it to "location = /admin.php "  
> and move it above the "location ~ .php$ "

“”"  
Either use something like “location = /admin.php”, or maybe reorder to  
regex locations (with ~) so they match in the order you want.  
“”"

You’ve done both. That’s ok, but unnecessary.

> it blocks other IPs but give file not found error for allowed Ips..
> 
> the last time I added root to admin.php location the browser promot me  
> to download admin.php..
> 
> what can I do now ?

“”"  
You will (probably) want to repeat your fastcgi\_pass and other fastcgi\_\*  
config lines inside the new location block. One request is handled by  
one location.  
“”"

Do that.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
