# Nginx, spawn-fcgi and php on freebsd - adding '/' incorrectly

**URL:** <https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462>\
**Category:** NGINX\
**Created:** [January 30, 2011, 1:47am UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462 "2011-01-30T01:47:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 30, 2011, 1:47am UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462/1 "2011-01-30T01:47:58Z")

</div>

hello, i have nginx setup, but i have a weird problem and i am unsure  
whether it is to do with the application or something i messed up on the  
nginx.conf file:

```auto
    server {
        listen 87.98.133.231:80;
        server_name dev.editionsdelga.fr;

        location / {
                root /www/dev.editionsdelga.fr/1.3.6;
                index index.html index.htm index.php;
                #see
http://www.prestashop.com/forums/viewthread/61175/installing_prestashop/nginxphpfmp_configuration
                rewrite
^/([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$
/img/p/$1-$2$3.jpg last;
                rewrite ^/([0-9]+)\-([0-9]+)/([_a-zA-Z0-9-]*)\.jpg$
/img/p/$1-$2.jpg last;
                rewrite
^/([0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$ /img/c/$1$2.jpg
last;
                rewrite
"^/lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$3&isolang;=$1$5" last;
                rewrite
"^/lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$2&isolang;=$1$4" last;
                rewrite
"^/lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$
/category.php?id_category=$2&isolang;=$1" last;
                rewrite
^/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$2$4 last;
                rewrite ^/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$1$3 last;
                rewrite ^/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$
/category.php?id_category=$1 last;
                rewrite ^/content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$
/cms.php?id_cms=$1 last;
                rewrite ^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$
/supplier.php?id_supplier=$1$3 last;
                rewrite ^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$
/manufacturer.php?id_manufacturer=$1$3 last;
                rewrite "^/lang-([a-z]{2})/(.*)$ /$2?isolang=$1" last;

        }
        # pass the PHP scripts to FastCGI server listening on
127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_connect_timeout 60;
                fastcgi_send_timeout 180;
                fastcgi_read_timeout 180;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
                fastcgi_intercept_errors on;

                fastcgi_param SCRIPT_FILENAME
/www/dev.editionsdelga.fr/1.3.6$fastcgi_script_name;
                fastcgi_param SERVER_NAME $http_host;
                fastcgi_ignore_client_abort on;
                fastcgi_param QUERY_STRING $query_string;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param CONTENT_TYPE $content_type;
                fastcgi_param CONTENT_LENGTH $content_length;
                }
        } # end server for dev.editionsdelga.fr

```

when you view the site, all links are missing the last ‘/’ so instead of  
getting [http://dev.editionsdelga.fr/manufacturer.php](http://dev.editionsdelga.fr/manufacturer.php)  
you get [http://dev.editionsdelga.frmanufacturer.php/](http://dev.editionsdelga.frmanufacturer.php/)

# nginx -v

nginx version: nginx/0.8.54

# php -v

PHP 5.3.5 with Suhosin-Patch (cli) (built: Jan 26 2011 19:38:46)  
Copyright (c) 1997-2009 The PHP Group  
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies  
with XCache v1.3.1, Copyright (c) 2005-2010, by mOo

Any advice much appreciated.

Norman

Posted at Nginx Forum:

> **[nginx, spawn-fcgi and php on freebsd - adding '/' incorrectly](https://forum.nginx.org/read.php?2,170721,170721#msg-170721)**
>
> hello, i have nginx setup, but i have a weird problem and i am unsure whether it is to do with the application or something i messed up on the nginx.conf file: \[code\] server { listen 87.98.133.231:80; server\_name dev.editionsdelga.fr;

---

<div class="post-metadata">

**Author:** ![khine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c67d28/32.png) [@khine](https://www.ruby-forum.com/u/khine)\
**Post date:** [January 30, 2011, 2:31am UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462/2 "2011-01-30T02:31:46Z")

</div>

Hello!

On Sat, Jan 29, 2011 at 07:46:25PM -0500, khine wrote:

> hello, i have nginx setup, but i have a weird problem and i am unsure  
> whether it is to do with the application or something i messed up on the  
> nginx.conf file:

[…]

> when you view the site, all links are missing the last ‘/’ so instead of  
> getting [http://dev.editionsdelga.fr/manufacturer.php](http://dev.editionsdelga.fr/manufacturer.php)  
> you get [http://dev.editionsdelga.frmanufacturer.php/](http://dev.editionsdelga.frmanufacturer.php/)

You mean - links generated by php scripts on your site, right?  
Most likely they need params you don’t set in your config  
(i.e. REQUEST\_URI) to construct correct links.

It’s really bad idea to set fastcgi\_param’s by hand unless you  
understand what you are doing and have a good reason to. Use  
“include fastcgi.conf” instead.

Maxim D.

---

<div class="post-metadata">

**Author:** ![khine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c67d28/32.png) [@khine](https://www.ruby-forum.com/u/khine)\
**Post date:** [January 30, 2011, 10:06pm UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462/3 "2011-01-30T22:06:26Z")

</div>

hi,

## Maxim D. Wrote:

> $query\_string;  
> the last ‘/’ so instead of
> 
> > getting  
> > [http://dev.editionsdelga.fr/manufacturer.php](http://dev.editionsdelga.fr/manufacturer.php)  
> > you get  
> > [http://dev.editionsdelga.frmanufacturer.php/](http://dev.editionsdelga.frmanufacturer.php/)
> 
> You mean - links generated by php scripts on your  
> site, right?

no, because these are actual files on the server and are not generated  
by my application.

```auto
# tree -L 1
.
|-- 404.php
|-- CHANGELOG
...
|-- js
|-- mails
|-- manage
|-- manufacturer.php
|-- modules
|-- my-account.php
|-- new-products.php
|-- order-confirmation.php
...
|-- tools
|-- translations
`-- upload

14 directories, 43 files
```

> Most likely they need params you don’t set in your  
> config  
> (i.e. REQUEST\_URI) to construct correct links.
> 
> It’s really bad idea to set fastcgi\_param’s by  
> hand unless you  
> understand what you are doing and have a good  
> reason to. Use  
> “include fastcgi.conf” instead.

ok, i will move this out.

> Maxim D.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,170721,170902#msg-170902](http://forum.nginx.org/read.php?2,170721,170902#msg-170902)

---

<div class="post-metadata">

**Author:** ![khine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c67d28/32.png) [@khine](https://www.ruby-forum.com/u/khine)\
**Post date:** [February 11, 2011, 2:10pm UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462/4 "2011-02-11T14:10:40Z")

</div>

hello,  
i managed to get this almost working, but have another issue, in that  
when i enable the friendly URL’s from within the Back-Office of  
Prestashop, all the links on this site are being rewritten back to the  
home site.

```
[code]

```

server {  
server\_name [dev.editionsdelga.fr](http://dev.editionsdelga.fr);  
root /www/editionsdelga.fr/prestashop;  
access\_log logs/editionsdelga.access.log main;  
error\_log logs/editionsdelga.error.log;  
rewrite\_log on;

```
    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    include prestashop.conf;
    include php.conf;
}

```

[/code]

in the above snippet, the rewrite\_log, does not seem to write anything  
in the logs/editionsdelga.error.log file! is there a way to test the  
rewrite rules for /usr/local/etc/nginx/prestashop.conf file?

```auto
# cat /usr/local/etc/nginx/prestashop.conf
rewrite
^/([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$
/img/p/$1-$2$3.jpg last;
rewrite ^/([0-9]+)\-([0-9]+)/([_a-zA-Z0-9-]*)\.jpg$ /img/p/$1-$2.jpg
last;
rewrite ^/([0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$
/img/c/$1$2.jpg last;
rewrite
"^/lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$3&isolang;=$1$5" last;
rewrite "^/lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$2&isolang;=$1$4" last;
rewrite "^/lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$
/category.php?id_category=$2&isolang;=$1" last;
rewrite ^/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$2$4 last;
rewrite ^/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$
/product.php?id_product=$1$3 last;
rewrite ^/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$ /category.php?id_category=$1
last;
rewrite ^/content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$ /cms.php?id_cms=$1
last;
rewrite ^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$ /supplier.php?id_supplier=$1$3
last;
rewrite ^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$
/manufacturer.php?id_manufacturer=$1$3 last;
rewrite "^/lang-([a-z]{2})/(.*)$ /$2?isolang=$1" last;
```

```auto
# cat /usr/local/etc/nginx/php.conf
location ~ \.php$ {
        fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;

        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SERVER_PROTOCOL $server_protocol;

        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        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;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
}
```

thank you for any advice.

norman

Posted at Nginx Forum:

> **[Re: nginx, spawn-fcgi and php on freebsd - adding '/' incorrectly](https://forum.nginx.org/read.php?2,170721,174129#msg-174129)**
>
> hello, i have nginx setup, but i have a weird problem and i am unsure whether it is to do with the application or something i messed up on the nginx.conf file: \[code\] server { listen 87.98.133.231:80; server\_name dev.editionsdelga.fr;

---

<div class="post-metadata">

**Author:** ![khine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c67d28/32.png) [@khine](https://www.ruby-forum.com/u/khine)\
**Post date:** [January 31, 2011, 12:09am UTC](https://www.ruby-forum.com/t/nginx-spawn-fcgi-and-php-on-freebsd-adding-incorrectly/202462/5 "2011-01-31T00:09:20Z")

</div>

Hello!

On Sun, Jan 30, 2011 at 04:05:18PM -0500, khine wrote:

[…]

> no, because these are actual files on the server and are not generated  
> by my application.

I.e. you have problems with links you type yourself in your  
browser? Requests are redirected to wrong address?

Anyway, this is likely to be done by your scripts as well, and the  
reason is most likely the same: insufficient fastcgi\_param’s.

Maxim D.
