# Problems with fastcgi php migration

**URL:** <https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385>\
**Category:** NGINX\
**Created:** [March 9, 2008, 4:25am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385 "2008-03-09T04:25:13Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 10, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/21 "2008-03-10T19:15:49Z")

</div>

Igor S. wrote:

> ```
> }
> 
> ```

Unfortunately, I tried both formats and it tosses a 404 when I try and  
go to one of the extensionless files.

I’m assuming this is the order:

location / {  
…  
}

location ~ .(shtml|php)$ {  
…fastcgi\_pass stuff…  
}

location ^/(galleries|poll|news/ {  
…fastcgi\_pass stuff…  
}

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 10, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/22 "2008-03-10T23:12:43Z")

</div>

Igor S. wrote:

> location ~ ^/(galleries|poll|news)/ {

It’s one of those days…that didn’t work either.

> Second, what’s about galleries/etc root ?

I have the root set to the same root as the other locations. Most of the  
extensionless php files are in the site’s root directory, while others  
can be found in multiple subdirectories, e.g.

galleries is off the main like [example.com/galleries](http://example.com/galleries) while the file  
photos is run from, as an example:  
[example.com/fest/tiff/2007/1/photos](http://example.com/fest/tiff/2007/1/photos)  
[example.com/academy/80/photos](http://example.com/academy/80/photos)  
[example.com/globes/56/photos](http://example.com/globes/56/photos)

Back in the time when I tried lighttpd they had a fastcgi.map-extensions  
config so you said something like “/galleries” =\> “.php”, “/photos” =\>  
“.php”, etc.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 3:27am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/23 "2008-03-11T03:27:01Z")

</div>

Ian M. Evans wrote:

> In IE7:  
> calling /galleries/ causes a 404  
> calling /galleries runs the script as expected

Oddly, IE7 is now exposing the php script, so I guess I’m shutting that  
test server down till I hear back… 🙂

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 3:20am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/24 "2008-03-11T03:20:32Z")

</div>

Igor S. wrote:

> location ~ ^/(galleries|poll|news)/ {

Just did some further testing…

In IE7:  
calling /galleries/ causes a 404  
calling /galleries runs the script as expected

In Firefox:  
calling /galleries/ causes a 404  
calling /galleries gives a “Open with…/Save to disk” octet stream  
dialogue.

Interesting…

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/25 "2008-03-11T21:50:06Z")

</div>

Igor,

Just had a thought…could some of the problems with the extensionless  
files I mentioned last night have anything to do with nginx not knowing  
what content-type to send them as? Could that explain the one issue I  
noticed where the browsers tried to download the files?

I’m hoping we’re just one or two quick changes from getting the site to  
work fully under nginx.

Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/26 "2008-03-11T21:57:52Z")

</div>

On Tue, Mar 11, 2008 at 04:35:52PM -0400, Ian M. Evans wrote:

> Just had a thought…could some of the problems with the extensionless  
> files I mentioned last night have anything to do with nginx not knowing  
> what content-type to send them as? Could that explain the one issue I  
> noticed where the browsers tried to download the files?

nginx does set any content-type for proxied and fastcgi responses.  
It should be set by backend.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/27 "2008-03-11T22:07:53Z")

</div>

Igor S. wrote:

> nginx does set any content-type for proxied and fastcgi responses.  
> It should be set by backend.

Then maybe it’s not that.

Can you think of any other reason why browsers would be trying to  
download the extensionless files as opposed to seeing them as web pages?  
As I said last night, the setting you suggested for the extensionless  
files produced these results:

> location ~ ^/(galleries|poll|news)/ {

In IE7:  
calling /galleries/ causes a 404  
calling /galleries runs the script as expected

In Firefox:  
calling /galleries/ causes a 404  
calling /galleries gives a “Open with…/Save to disk” octet stream  
dialogue.

There has to be one final tweak that we’re missing. We’ve been running  
the extensionless PHP file method for almost a decade under Apache, so  
we must be missing one last thing that’s preventing us from ditching the  
Apache backend and going 100% nginx.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/28 "2008-03-11T22:35:06Z")

</div>

Igor S. wrote:

> telnet \<your\_site\> 8088 ← the nginx/fastcgi test is running at 8088  
> GET /galleries HTTP/1.0
> 
> show ?

GET /galleries HTTP/1.0

Using the MS Telnet client I’m getting a 400 Bad Request.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/29 "2008-03-11T22:50:59Z")

</div>

Ian M. Evans wrote:

> Igor S. wrote:
> 
> > telnet \<your\_site\> 8088 ← the nginx/fastcgi test is running at 8088  
> > GET /galleries HTTP/1.0
> > 
> > show ?
> 
> GET /galleries HTTP/1.0
> 
> Using the MS Telnet client I’m getting a 400 Bad Request.

Actually it appears I’m getting the bad request on 80 and 8088 as soon  
as I try to connect…before the GET, even though the site’s up and  
running.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/30 "2008-03-11T22:17:53Z")

</div>

On Tue, Mar 11, 2008 at 04:56:24PM -0400, Ian M. Evans wrote:

> There has to be one final tweak that we’re missing. We’ve been running  
> the extensionless PHP file method for almost a decade under Apache, so  
> we must be missing one last thing that’s preventing us from ditching the  
> Apache backend and going 100% nginx.

What does

telnet \<your\_site\> 80  
GET /galleries HTTP/1.0

show ?

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/31 "2008-03-11T22:59:12Z")

</div>

On Tue, Mar 11, 2008 at 04:56:24PM -0400, Ian M. Evans wrote:

> > location ~ ^/(galleries|poll|news)/ {
> 
> In IE7:  
> calling /galleries/ causes a 404  
> calling /galleries runs the script as expected
> 
> In Firefox:  
> calling /galleries/ causes a 404  
> calling /galleries gives a “Open with…/Save to disk” octet stream  
> dialogue.

You need to change:

- location ~ ^/(galleries|poll|news)/ {

- location ~ ^/(galleries|poll|news)(/|$) {

because otherwise nginx handles /galleries, etc. as local files.

Then probably you need to set PATH\_INFO:

location ~ ^/(galleries|poll|news)(/|$) {  
set $path\_info “”;  
if ($uri ~ ^/[^/]+(/.+)) {  
set $path\_info $1;  
}

```
   fastcgi_pass ...
   fastcgi_param PATH_INFO $path_info;
   ...

```

}

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/32 "2008-03-11T22:59:12Z")

</div>

On Tue, Mar 11, 2008 at 05:41:24PM -0400, Ian M. Evans wrote:

> > Using the MS Telnet client I’m getting a 400 Bad Request.
> 
> Actually it appears I’m getting the bad request on 80 and 8088 as soon  
> as I try to connect…before the GET, even though the site’s up and running.

It’s probably because of MS Telnet: it may send some control commands  
before your text.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 11, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/33 "2008-03-11T23:26:49Z")

</div>

Igor S. wrote:

> ```
> set $path_info $1;
> }
> 
> fastcgi_pass ...
> fastcgi_param PATH_INFO $path_info;
> ...
> 
> ```
> 
> }

The results:

Firefox:  
Calling /galleries pulls up page. Adding further info (e.g. /30/1/1)  
pulls up photo page as expected.

Calling /galleries/ (with trailing slash) still pulls up a 404 error.

IE7:  
/galleries creates same passing result as Firefox.  
/galleries/, like Firefox calls up the 404 page.

So we’re much, much closer…only stumbling block is getting it to run  
properly if a visitor adds a trailing slash. Is that just a matter of  
forcing no trailing slash if there’s nothing else following it?

Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 2:00am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/34 "2008-03-12T02:00:07Z")

</div>

Fernando,

Thanks for your email. The fastcgi settings I have are basically the  
same as yours and all extensioned files (.php,.shtml) are working fine.

I had a problem with extensionless files that Igor solved with a special  
regex for the path\_info.

My only outstanding issue is that if the extensionless file is called  
WITH a trailing slash and WITHOUT further path content I get a 404.

So remembering that /galleries is a FILE and not a directory:

/galleries calls the content it should (a main listing of photo events  
/galleries/123/456/1 calls up a photo  
/galleries/ tosses a 404

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 12:01am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/35 "2008-03-12T00:01:18Z")

</div>

Hi Ian,

I’ve just lost 4 hours trying to make phpmyadmin work. The problem was  
not passing the correct fastcgi\_param.

Here is my fastcgi.conf file, try including it to your conf file:

* * *

# /etc/nginx/fastcgi.conf

fastcgi\_param PATH\_INFO $fastcgi\_script\_name;

# GATEWAY\_INTERFACE is required otherwise python bugs

fastcgi\_param GATEWAY\_INTERFACE CGI/1.1;

fastcgi\_param SERVER\_SOFTWARE nginx;

# Required otherwise the .cgi content is served as plain text

fastcgi\_param QUERY\_STRING $query\_string;

# Required for dealing with POST requests

fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param CONTENT\_TYPE $content\_type;  
fastcgi\_param CONTENT\_LENGTH $content\_length;

fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param DOCUMENT\_URI $document\_uri;  
fastcgi\_param SERVER\_PROTOCOL $server\_protocol;  
fastcgi\_param REMOTE\_ADDR $remote\_addr;  
fastcgi\_param REMOTE\_PORT $remote\_port;  
fastcgi\_param SERVER\_ADDR $server\_addr;

# SERVER\_PORT and SERVER\_NAME are required

fastcgi\_param SERVER\_PORT $server\_port;  
fastcgi\_param SERVER\_NAME $server\_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

## #fastcgi\_param REDIRECT\_STATUS 200;

Also make sure you have SCRIPT\_FILENAME correctly set, but it should be  
as you can see some stuff being rendered.

Best regards,

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 10:31am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/36 "2008-03-12T10:31:37Z")

</div>

Igor S. wrote:

> Then you try to remove cgi.fix-pathinfo and to use the following:

Okay, changed cgi.fix-pathinfo to cgi.fix-pathinfo=0 Restarted the  
fastcgi server, made the nginx changes and restarted.

> ```
> fastcgi_pass ...
> fastcgi_param SCRIPT_FILENAME $document_root$script_name;
> fastcgi_param PATH_INFO $path_info;
> ...
> 
> ```
> 
> }

That didn’t work.

Firefox:  
/galleries gave the application/octet stream download  
/galleries/ gave a 404  
/galleries/30/1/1 gave a 404

IE7:  
/galleries displayed the PHP file  
/galleries/ gave a 404  
/galleries/30/1/1 gave a 404

Also, this appeared in the error log:  
for /galleries/30/1/1:

[error] 13547#0: \*59343 open()  
“/usr/local/apache/htdocs/galleries/30/1/1” failed (20: Not a directory)

for /galleries/  
[error] 13578#0: \*59589 “/usr/local/apache/htdocs/galleries/index.shtml”  
is not found (20: Not a directory)

So it appears that we were a bit closer with the previous email where  
cgi.fix-pathinfo was on and the only 404 was when there was a trailing  
slash after the filename.

Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 8:50am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/37 "2008-03-12T08:50:09Z")

</div>

On Tue, Mar 11, 2008 at 06:17:50PM -0400, Ian M. Evans wrote:

> > ```
> > if ($uri ~ ^/[^/]+(/.+)) {
> > 
> > ```
> 
> So we’re much, much closer…only stumbling block is getting it to run  
> properly if a visitor adds a trailing slash. Is that just a matter of  
> forcing no trailing slash if there’s nothing else following it?

Then you try to remove cgi.fix-pathinfo and to use the following:

location ~ ^/(galleries|poll|news)(/|$) {

```
   set $script_name $uri;
   set $path_info "";

   if ($uri ~ ^(/[^/]+)(/.*)) {
       set $script_name $uri;
       set $path_info $1;
   }

   fastcgi_pass ...
   fastcgi_param SCRIPT_FILENAME $document_root$script_name;
   fastcgi_param PATH_INFO $path_info;
   ...

```

}

Then you should get the following:

URL SCRIPT\_FILENAME PATH\_INFO

/galleries $document\_root/galleries -  
/galleries/ $document\_root/galleries /  
/galleries/30/1/1 $document\_root/galleries /30/1/1

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 10:37am UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/38 "2008-03-12T10:37:34Z")

</div>

On Wed, Mar 12, 2008 at 05:17:51AM -0400, Ian M. Evans wrote:

> > ```
> > set $path_info "";
> > 
> > ```
> > 
> > }  
> > /galleries displayed the PHP file  
> > [error] 13578#0: \*59589 “/usr/local/apache/htdocs/galleries/index.shtml”  
> > is not found (20: Not a directory)
> 
> So it appears that we were a bit closer with the previous email where  
> cgi.fix-pathinfo was on and the only 404 was when there was a trailing  
> slash after the filename.

Add a single fastcgi\_pass inside if:

```
        set $path_info $1;

```

- 

```
      fastcgi_pass ...
  }

```

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/39 "2008-03-12T22:32:45Z")

</div>

Still trying to work out why, in my last mail (after I fixed my  
cut’n’paste error), the path\_info wasn’t getting populated with the  
stuff after the / but instead the script.

Began trying to finally understand how regex works. So a question:

In your location:

if ($uri ~ ^(/[^/]+)(/.\*)) {  
set $script\_name $uri;  
set $path\_info $1;  
}

If I’m understanding the tutorial I’m reading, don’t the $NUM variables  
get populated by the parantheses? So wouldn’t the set $path\_info come  
from $2 since you have two sets of ()?

I thought that might explain the findings from my last email:

URL: /testgalleries  
the URL gave a 404 error

URL: /testgalleries/  
PATH\_INFO:/testgalleries with no trailing slash. Should be /

URL: /testgalleries/30/1/1  
PATH\_INFO: /testgalleries Should be /30/1/1

However, changing it to $2 created 404’s in each test case.

So I’m not sure why $path\_info isn’t getting the right info.

Many thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [March 12, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385/40 "2008-03-12T22:36:55Z")

</div>

On Wed, Mar 12, 2008 at 05:23:52PM -0400, Ian M. Evans wrote:

> set $path\_info $1;  
> }
> 
> If I’m understanding the tutorial I’m reading, don’t the $NUM variables  
> get populated by the parantheses? So wouldn’t the set $path\_info come  
> from $2 since you have two sets of ()?

This is my mistype, it should be:

set $script\_name $1;  
set $path\_info $2;

[Previous page](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385.md?page=1)

[Next page](https://www.ruby-forum.com/t/problems-with-fastcgi-php-migration/131385.md?page=3)
