# Add header after fcgi handling

**URL:** <https://www.ruby-forum.com/t/add-header-after-fcgi-handling/221988>\
**Category:** NGINX\
**Created:** [August 8, 2012, 11:06am UTC](https://www.ruby-forum.com/t/add-header-after-fcgi-handling/221988 "2012-08-08T11:06:27Z")\
**Posts on this page:** 3\
**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:** [August 8, 2012, 11:06am UTC](https://www.ruby-forum.com/t/add-header-after-fcgi-handling/221988/1 "2012-08-08T11:06:27Z")

</div>

Hi,

please can anybody help me, I’m stucked and sure that I think in wrong  
way.

the .htaccess

/web/root/interfaces/.htaccess

RewriteEngine on  
RewriteBase /interfaces  
RewriteRule ^json/(.\*) index.php?x=$1&%{QUERY\_STRING} [NC]

Translated to (the easy part I think)

```
     location ~ /interfaces/json/(\S+) {
       try_files /$uri /interfaces/index.php?x=$1&$args;
     }

```

now

Header set Access-Control-Allow-Origin \*  
Header set Access-Control-Allow-Headers “X-Requested-With, \*”

to  
location /interfaces {  
include  
/home/nginx/server/conf/add\_header-Access-Control-Allow-Origin.conf;  
add\_header Access-Control-Allow-Headers “X-Requested-With,  
\*”;  
}

but when the php request is handled the ‘location /interfaces {…}’  
does not match any more,  
of course.

How can I add the header to the reply after the php is executed?!

Many thanks for help.  
Best regards  
Aleks

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [August 8, 2012, 12:56pm UTC](https://www.ruby-forum.com/t/add-header-after-fcgi-handling/221988/2 "2012-08-08T12:56:32Z")

</div>

Hello!

On Wed, Aug 08, 2012 at 11:05:46AM +0200, Aleksandar L. wrote:

> RewriteBase /interfaces  
> Header set Access-Control-Allow-Origin \*  
> but when the php request is handled the ‘location /interfaces {..}’  
> does not match any more,  
> of course.
> 
> How can I add the header to the reply after the php is executed?!

You have to add headers in the location where you pass requests to  
php. And you may need to add another location if you don’t want to  
add headers to all responses from php. Something like this should  
be fine:

```
location /interfaces/ {
    add_header ...

    location ~ \.php$ {
        fastcgi_pass ...
        ...
    }
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [August 8, 2012, 1:32pm UTC](https://www.ruby-forum.com/t/add-header-after-fcgi-handling/221988/3 "2012-08-08T13:32:11Z")

</div>

Hi Maxim,

On 08-08-2012 12:55, Maxim D. wrote:

> Hello!
> 
> On Wed, Aug 08, 2012 at 11:05:46AM +0200, Aleksandar L. wrote:
> 
> > Hi,
> > 
> > please can anybody help me, I’m stucked and sure that I think in  
> > wrong way.

[snipp]

> ```
> location ~ \.php$ {
> fastcgi_pass ...
> ...
> }
> }
> 
> ```

Super thank you. I must rethink my setup with nested locations ;-).

Best regards  
Aleks
