# From Apache to Nginx+php-fpm : How to port configuration?

**URL:** <https://www.ruby-forum.com/t/from-apache-to-nginx-php-fpm-how-to-port-configuration/178252>\
**Category:** NGINX\
**Created:** [October 31, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/from-apache-to-nginx-php-fpm-how-to-port-configuration/178252 "2009-10-31T23:53:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ftrudeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba9def/32.png) [@ftrudeau](https://www.ruby-forum.com/u/ftrudeau)\
**Post date:** [October 31, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/from-apache-to-nginx-php-fpm-how-to-port-configuration/178252/1 "2009-10-31T23:53:11Z")

</div>

Greetings all.

I successfully installed nginx + php-fpm. I already read numerous  
tutorials and exemples out there on the web, and I would need some  
additional insights on how to port the various configuration options I  
had via .htaccess under Apache.

For instance, CSS files needs to be processed by PHP.  
Under Apache, I simply put this in the .htaccess :

SetHandler application/x-httpd-php

1. How can I achieve this under Nginx with php-fpm ?
2. Any way to specify per server PHP configs Ã -la Apache (ex : php\_value  
memory\_limit 64M) ?
3. Weird behavior : if I try to access a PHP file (ex :  
[http://www.mydomain.com/somefile.php](http://www.mydomain.com/somefile.php)), I get a download prompt.

Here is the server configuration for mydomain.  
Any tweaks and advices would be appreciated.

```
    server
    {
            listen :80 default;
            server_name mydomain.com www.mydomain.com;
            expires 0;

            access_log access.log;
            error_log error.log;

            # Redirect from non-www to www
            if ($host = 'mydomain.com')
            {
                    rewrite ^/(.*)$ http://www.mydomain.com/$1 

```

permanent;  
}

```
            # Simple static content delivery system (versionning 

```

only)  
location ~\*  
“^(+)-[0-9]{10}.(js|gif|jpg|jp?g|png|css|swf)$”  
{  
rewrite  
“(.\*)/(+)-[0-9]{10}.(js|gif|jp?g|png|css|swf)$” $1/$2.$3 last;  
return 403;  
}

```
            location /
            {

                    # set document root and index file
                    root ;
                    index index.php index.html index.htm;

                    # if file exists, set expire and return it
                    if (-f $request_filename)
                    {
                            expires 1y;
                            break;
                    }

                    # if directory exists return it right away
                    if (-d $request_filename)
                    {
                            break;
                    }

                    # rewrite everyting else to index.php
                    if (!-e $request_filename)
                    {
                            rewrite ^ /index.php last;
                            break;
                    }

            }
            # if the request starts with our frontcontroller, pass 

```

it on to fastcgi  
location ~ ^/index.php  
{

```
                    # set document root
                    root ;

                    # fastcgi setup
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.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;

```
            }

            # Prevent files beginning with . from being viewed
            location ~ /\.
            {
                    deny all;
            }

    }

```

Posted at Nginx Forum:

> **[From Apache to Nginx+php-fpm : How to port configuration ?](https://forum.nginx.org/read.php?2,18743,18743#msg-18743)**
>
> Greetings all. I successfully installed nginx + php-fpm. I already read numerous tutorials and exemples out there on the web, and I would need some additional insights on how to port the various configuration options I had via .htaccess under Apache....

---

<div class="post-metadata">

**Author:** ![ftrudeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba9def/32.png) [@ftrudeau](https://www.ruby-forum.com/u/ftrudeau)\
**Post date:** [November 1, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/from-apache-to-nginx-php-fpm-how-to-port-configuration/178252/2 "2009-11-01T19:34:31Z")

</div>

Found the solution.  
Hope this helps someone else.

```
            [...]

            # Handle PHP scripts via fastcgi
            location ~ ^.+\.php$
            {
                    # set document root
                    root ;

                    # test for file existence
                    # fallback to bootstrap to display 404
                    try_files $uri /index.php?$args;

                    # fastcgi setup
                    fastcgi_pass 127.0.0.1:9000;
                    # fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME 

```

$document\_root$fastcgi\_script\_name;

```
                    # include common fastcgi params
                    include /etc/nginx/fastcgi_params;
            }

            [...]

```

Posted at Nginx Forum:

> **[Re: From Apache to Nginx+php-fpm : How to port configuration ?](https://forum.nginx.org/read.php?2,18743,18959#msg-18959)**
>
> Greetings all. I successfully installed nginx + php-fpm. I already read numerous tutorials and exemples out there on the web, and I would need some additional insights on how to port the various configuration options I had via .htaccess under Apache....
