Convert Apache rewrite to NGinx

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.
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 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 [email protected]

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 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. [email protected]

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 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%/ and with the
hello world topic it looks like this :
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,
request: “GET /2013/hello-world/ HTTP/1.1”, host: “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:
http://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 [email protected]

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 http://nginx.org/r/location

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. [email protected]

Le Sun, 03 Mar 2013 19:59:17 +0100, Steve H.
[email protected] 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
[email protected]
http://mailman.nginx.org/mailman/listinfo/nginx


nginx mailing list
[email protected]
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 [email protected]

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 http://nginx.org/r/location

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

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 :slight_smile: !

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

Steve