# Convert Apache rewrite to NGinx

**URL:** <https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074>\
**Category:** NGINX\
**Created:** [March 3, 2013, 1:53pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074 "2013-03-03T13:53:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 1:53pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/1 "2013-03-03T13:53:10Z")

</div>

Hi,

Using nginx 1.2.1 on Debian Wheezy 64 bits.

My wordpress need rewrite, it gave me this:

RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST\_FILENAME} !-f RewriteCond %{REQUEST\_FILENAME} !-d RewriteRule . /index.php [L]

I’ve tried to convert it with this website: [http://winginx.com/htaccess](http://winginx.com/htaccess).  
The result is:

# nginx configuration

location / {  
if (!-e $request\_filename){  
rewrite ^(.\*)$ /index.php break;  
}  
}

I’ve put it into /etc/nginx/conf.d/doinalefort.fr.conf like this:

server {  
listen 80;  
listen 443 ssl;  
# server\_name 176.31.122.26;  
server\_name [doinalefort.fr](http://doinalefort.fr) [www.doinalefort.fr](http://www.doinalefort.fr);  
root /var/www/doinalefort.fr;

```
       msie_padding on;

       ssl_session_timeout 5m;

       ssl_protocols SSLv2 SSLv3 TLSv1;
       ssl_ciphers HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers on;

       error_log /var/log/nginx/error.log;
       access_log /var/log/nginx/access.log;

       index index.php;
       fastcgi_index index.php;

       client_max_body_size 8M;
       client_body_buffer_size 256K;

       location ~ \.php$ {
               include fastcgi_params;

               # Assuming php-fastcgi running on localhost port 9000
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;

```
               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;

       location / {
               if (!-e $request_filename){
               rewrite ^(.*)$ /index.php break;
               }

```

}

```
       }

```

}

And restarted nginx, but it give me this error:

Restarting nginx: nginx: [emerg] location “/” is outside location  
“.php$”  
in /etc/nginx/conf.d/doinalefort.fr.conf:44  
nginx: configuration file /etc/nginx/nginx.conf test failed

And idea?

Cordially, Koshie

–  
Sorry for my english, I’m trying the best in each e-mail writing. Tell  
me  
if I’m not clear enough.  
This mail account is only for list reading, to contact me send an e-mail  
at removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 4:01pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/2 "2013-03-03T16:01:45Z")

</div>

On Sun, Mar 03, 2013 at 01:52:48PM +0100, GASPARD kvin wrote:

Hi there,

> Using nginx 1.2.1 on Debian Wheezy 64 bits.
> 
> My wordpress need rewrite, it gave me this:

> # nginx configuration
> 
> location / {  
> if (!-e $request\_filename){  
> rewrite ^(.\*)$ /index.php break;  
> }  
> }

See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Pitfalls)

Particularly the “Front Controller Pattern based packages” section.

Probably a single extra try\_files line will work for you.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 7:39pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/3 "2013-03-03T19:39:03Z")

</div>

Thanks for your reply.

> > location / {
> 
> f

This is my new config file :

server {  
listen 80;  
listen 443 ssl;  
# server\_name 176.31.122.26;  
server\_name [doinalefort.fr](http://doinalefort.fr) [www.doinalefort.fr](http://www.doinalefort.fr);  
root /var/www/doinalefort.fr;

```
     msie_padding on;

```

# ssl\_certificate /etc/nginx/certs/auction-web.crt;

# ssl\_certificate\_key /etc/nginx/certs/auction-web.key;

```
     ssl_session_timeout 5m;

     ssl_protocols SSLv2 SSLv3 TLSv1;
     ssl_ciphers HIGH:!aNULL:!MD5;
     ssl_prefer_server_ciphers on;

     error_log /var/log/nginx/error.log;
     access_log /var/log/nginx/access.log;

     index index.php;
     fastcgi_index index.php;

     client_max_body_size 8M;
     client_body_buffer_size 256K;

     location ~ \.php$ {
             include fastcgi_params;

             # Assuming php-fastcgi running on localhost port 9000
             fastcgi_pass unix:/var/run/php5-fpm.sock;
             fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;

```
             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;

             try_files $uri $uri/ /index.php?q=$uri&$args;

     }

```

}

By checking the URL you gave me it seems to be the good choice for  
Wordpress (3.5.1). I’ve restarted nginx, then I’ve this kind of  
permalink  
in Wordpress now: [http://doinalefort.fr/%year%/%postname%/](http://doinalefort.fr/%25year%25/%25postname%25/) and with the  
hello world topic it looks like this :  
[http://doinalefort.fr/2013/hello-world/](http://doinalefort.fr/2013/hello-world/)

As you can see, I’ve a 404 error, in /var/log/nginx/error.log I’ve:  
2013/03/03 19:34:31 [error] 27463#0: \*252  
“/var/www/doinalefort.fr/2013/hello-world/index.php” is not found (2: No  
such file or directory), client: 80.239.242.158, server: [doinalefort.fr](http://doinalefort.fr),  
request: “GET /2013/hello-world/ HTTP/1.1”, host: “[doinalefort.fr](http://doinalefort.fr)”

And in access.log:

80.239.242.158 - - [03/Mar/2013:19:35:28 +0100] “GET /2013/hello-world/  
HTTP/1.1” 404 142 “-” “Opera/9.80 (X11; Linux x86\_64; Edition Next)  
Presto/2.12.388 Version/12.14”

An idea?

Also, I’ve googled a little more and I finally found this blog:

> **[High Performance WordPress Part 3 - themesforge](https://themesforge.com/featured/high-performance-wordpress-part-3/)**

But honestly I don’t have enough Nginx’s knowledge to know if this guy  
have write something serious. If you can tell me if it’s okay it’ll  
maybe  
solve a lot of problem before they appear.

Cordially, Koshie

–  
Sorry for my english, I’m trying the best in each e-mail writing. Tell  
me  
if I’m not clear enough.  
This mail account is only for list reading, to contact me send an e-mail  
at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 8:10pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/4 "2013-03-03T20:10:36Z")

</div>

On Sun, Mar 03, 2013 at 07:38:39PM +0100, GASPARD kvin wrote:

Hi there,

> > Probably a single extra try\_files line will work for you.

> This is my new config file :

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

> ```
> try_files $uri $uri/ /index.php?q=$uri&$args;
> }
> 
> ```

You will probably find things much easier when you fully understand what  
is written at [Module ngx\_http\_core\_module](http://nginx.org/r/location)

> [http://doinalefort.fr/2013/hello-world/](http://doinalefort.fr/2013/hello-world/)

One request is handled in one location{}. That request does not match  
this location, and so will not be handled in this location.

The try\_files directive should be in a location that does match –  
perhaps “location / {}”.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 8:19pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/5 "2013-03-03T20:19:57Z")

</div>

Le Sun, 03 Mar 2013 19:59:17 +0100, Steve H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a écrit:

> Why not just use the wp config examples in the docs? Both Wordpress and  
> nginx offer them.

Well, I forget to check that… Sorry.

Anyway, I’ve found this:

> > > Hi there,
> > > 
> > > > }  
> > > > This is my new config file :
> > 
> > # ssl\_certificate /etc/nginx/certs/auction-web.crt;
> > 
> > ```
> > fastcgi_pass unix:/var/run/php5-fpm.sock;
> > fastcgi_intercept_errors on;
> > 
> > ```
> > 
> > and with the hello world topic it looks like this :
> > 
> > have write something serious. If you can tell me if it’s okay it’ll
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

## –

Sorry for my english, I’m trying the best in each e-mail writing. Tell  
me  
if I’m not clear enough.  
This mail account is only for list reading, to contact me send an e-mail  
at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 6, 2013, 2:41pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/6 "2013-03-06T14:41:33Z")

</div>

Hi,

> > ```
> > try_files $uri $uri/ /index.php?q=$uri&$args;
> > }
> > 
> > ```
> 
> You will probably find things much easier when you fully understand what  
> is written at [Module ngx\_http\_core\_module](http://nginx.org/r/location)

I understand a little more how it works now :).

> > [http://doinalefort.fr/2013/hello-world/](http://doinalefort.fr/2013/hello-world/)
> 
> One request is handled in one location{}. That request does not match  
> this location, and so will not be handled in this location.
> 
> The try\_files directive should be in a location that does match –  
> perhaps “location / {}”.
> 
> f

I’ve created a new locations as you say and I’ve put the try\_files  
directive into, it works.

Cordially, Thanks 🙂 !

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 3, 2013, 8:00pm UTC](https://www.ruby-forum.com/t/convert-apache-rewrite-to-nginx/227074/7 "2013-03-03T20:00:05Z")

</div>

Why not just use the wp config examples in the docs? Both Wordpress and  
nginx offer them.

Steve
