# Auth\_basic restricted files with php

**URL:** <https://www.ruby-forum.com/t/auth-basic-restricted-files-with-php/186802>\
**Category:** NGINX\
**Created:** [April 12, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/auth-basic-restricted-files-with-php/186802 "2010-04-12T16:26:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clinisbud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecb155/32.png) [@clinisbud](https://www.ruby-forum.com/u/clinisbud)\
**Post date:** [April 12, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/auth-basic-restricted-files-with-php/186802/1 "2010-04-12T16:26:53Z")

</div>

Hi,

I’ve read a lot of threads (like this ) but I can’t solve my problem.  
It’s my first day working with nginx, so I’m a really newbie.

Here is a part of my nginx.conf

server {

listen 80;  
server\_name myserver.es;

location ~\* /apc {  
root /usr/local/nginx/htdocs/;  
auth\_basic “Restricted”;  
auth\_basic\_user\_file /usr/local/nginx/htdocs/.htpasswd;

# rewrite ^/apc($|/.\*) $1 break;

```
    }

```

location ~..php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/usr/local/nginx/htdocs$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
}

When I load in my browser myserver.es/apc.php the password for  
Restricted is requestes, so I introduce it. But then the browser does  
not load php, it shows the php in the browser. If I open  
myserver.es/index.php it’s loaded properly, as other php files in my  
directory.

Any help? It woul be very apreciated 🙂

Thank you in advance

Posted at Nginx Forum:

> **[auth\_basic restricted files with php](https://forum.nginx.org/read.php?2,74117,74117#msg-74117)**
>
> Hi, I've read a lot of threads (\[url=http://forum.nginx.org/read.php?2,68890,68944#msg-68944\]like this \[/url\]) but I can't solve my problem. It's my first day working with nginx, so I'm a really newbie. Here is a part of my nginx.conf...

---

<div class="post-metadata">

**Author:** ![clinisbud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecb155/32.png) [@clinisbud](https://www.ruby-forum.com/u/clinisbud)\
**Post date:** [April 13, 2010, 8:41am UTC](https://www.ruby-forum.com/t/auth-basic-restricted-files-with-php/186802/2 "2010-04-13T08:41:36Z")

</div>

Hi,

finally, I’ve solved it.

server {

listen 80;  
server\_name myserver.es;

location ~\*/apc {  
#root /usr/local/nginx/htdocs;  
auth\_basic “Restricted”;  
auth\_basic\_user\_file /usr/local/nginx/htdocs/.htpasswd;  
location ~.php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/usr/local/nginx/htdocs$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
}  
}  
location ~.php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/usr/local/nginx/htdocs$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
}  
}

Solution posted.

Thank you

Posted at Nginx Forum:

> **[Re: auth\_basic restricted files with php](https://forum.nginx.org/read.php?2,74117,74401#msg-74401)**
>
> Hi, I've read a lot of threads (\[url=http://forum.nginx.org/read.php?2,68890,68944#msg-68944\]like this \[/url\]) but I can't solve my problem. It's my first day working with nginx, so I'm a really newbie. Here is a part of my nginx.conf...

---

<div class="post-metadata">

**Author:** ![clinisbud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecb155/32.png) [@clinisbud](https://www.ruby-forum.com/u/clinisbud)\
**Post date:** [February 4, 2011, 4:53pm UTC](https://www.ruby-forum.com/t/auth-basic-restricted-files-with-php/186802/3 "2011-02-04T16:53:00Z")

</div>

I know this is old, but for those still finding this in search engines,  
thats not the ‘correct’ fix, though it could work, The problem us using  
a root path inside of the location block, if you move the root outside  
of location and into the server { } block, then there is no need to have  
a duplicated nested php block, nor is there a need to manually define  
the document root since $document\_root would be correctly filled by that  
point.

ie:

server {

listen 80;  
server\_name myserver.es;  
root /usr/local/nginx/htdocs;

location ~\*/apc {  
auth\_basic “Restricted”;  
auth\_basic\_user\_file /usr/local/nginx/htdocs/.htpasswd;  
}

location ~.php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;

```
#if the line isn't already in fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;

```

}  
}

Posted at Nginx Forum:

> **[Re: auth\_basic restricted files with php](https://forum.nginx.org/read.php?2,74117,172193#msg-172193)**
>
> Hi, I've read a lot of threads (\[url=http://forum.nginx.org/read.php?2,68890,68944#msg-68944\]like this \[/url\]) but I can't solve my problem. It's my first day working with nginx, so I'm a really newbie. Here is a part of my nginx.conf...
