Wordpress (single blog installation) and nginx

Hi guys,

I am running php with nginx, with fastcgi. But instead of heaving my
urls
like www.myblog.com/index.php/bla/bla I would like
www.myblog.com/bla/blahttp://www.myblog.com/bla/bla.

With the setup at the end of the mail, it redirect if a file can’t be
found
to index.php, but I get the content of index.php back. (So if I go to
www.myblog.com/bla/bla).

But I also have other requests that should not be rewritten (directories
and
files in the root directory). Is this possible? to redirect only if
www.myblog.com/bla/bla doesn’t exist to a normal request to the root
directory and send that to the fastcgi process?

I really love nginx, and thanks to this mailinglist I didn’t need much
help.
But most rewrite rules are written wordpress MU (multiuser) and not
wordpress redux. I just need to to rewrite all request that do not
exists to
index.php and pass this to fastcgi.

I did get this working one time… but I can’t figure out how I did it :S

server {
listen 80; # Replace this IP and port with the right
ones for your requirements
server_name www.myblog.com

    #charset koi8-r;

    #access_log  logs/host.access.log

    location ~*

^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$
{
root /var/www/blog;
}

    error_page  404 /index.php;

    location ~ \.php$ {
      include        /etc/nginx/fastcgi.conf;
      fastcgi_pass

127.0.0.1:8888;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/blog/$fastcgi_script_name;
}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

 }


Abdul-Rahman A.

IM: [email protected]

On Thu, Dec 13, 2007 at 09:06:05AM +0100, Abdul-Rahman A. wrote:

But I also have other requests that should not be rewritten (directories and
files in the root directory). Is this possible? to redirect only if
www.myblog.com/bla/bla doesn’t exist to a normal request to the root
directory and send that to the fastcgi process?

How does wordpress find what URI you request ?
I know that wordpress may use REQUEST_URI (it should already set
in /etc/nginx/fastcgi.conf):

fastcgi_param REQUEST_URI $request_uri;

The second way (I do not know whether Wordpress use it) is PATH_INFO:

  location / {
       root /var/www/blog;
       error_page  404 /index.php;
  }

  location ~ \.php$ {
       include        /etc/nginx/fastcgi.conf;
       fastcgi_pass 127.0.0.1:8888;
       fastcgi_param  PATH_INFO        $request_uri;
       fastcgi_param  SCRIPT_FILENAME 

/var/www/blog/$fastcgi_script_name;
}

  location ~* 

^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$
{
root /var/www/blog;
}

Hey Igor,

Works great, only know it returns a 404 error with every request :frowning: is
there
a way I can remove the header send by nginx?

On 12/13/07, Igor S. [email protected] wrote:

With the setup at the end of the mail, it redirect if a file can’t be
How does wordpress find what URI you request ?
}
^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$
index.php and pass this to fastcgi.
#access_log logs/host.access.log
location ~ .php$ {
#

IM: [email protected]


Igor S.
Igor Sysoev


Abdul-Rahman A.

IM: [email protected]

On Thu, Dec 13, 2007 at 03:04:54PM +0100, Abdul-Rahman A. wrote:

Works great, only know it returns a 404 error with every request :frowning: is there
a way I can remove the header send by nginx?

By the way, what method does work - REQUEST_URI or PATH_INFO ?

On Thu, Dec 13, 2007 at 03:04:54PM +0100, Abdul-Rahman A. wrote:

Works great, only know it returns a 404 error with every request :frowning: is there
a way I can remove the header send by nginx?

  •   error_page  404 /index.php;
    
  •   error_page  404 = /index.php;

There seems to be an other problem, it doesn’t pass the params so…
blog.domain.com/?page_id=2 doesn’t work, where
blog.domain.com/index.php?page_id=2 does work, I added both
requirest_uri
and path_info, but no success. Can’t find much on it on the mailinglist.

Greetz,

Abdul

On 12/13/07, Abdul-Rahman A. [email protected] wrote:

On Thu, Dec 13, 2007 at 09:06:05AM +0100, Abdul-Rahman A. wrote:
be

www.myblog.com/bla/bla doesn’t exist to a normal request to the
PATH_INFO:

it :S

    location ~*
      include        /etc/nginx/fastcgi.conf;
    #

Abdul-Rahman A.

Abdul-Rahman A.

IM: [email protected]


Abdul-Rahman A.

IM: [email protected]

On Sat, Dec 15, 2007 at 07:37:00PM +0100, Abdul-Rahman A. wrote:

There seems to be an other problem, it doesn’t pass the params so…
blog.domain.com/?page_id=2 doesn’t work, where
blog.domain.com/index.php?page_id=2 does work, I added both requirest_uri
and path_info, but no success. Can’t find much on it on the mailinglist.

       error_page  404 /index.php?$arg;

On Sat, Dec 15, 2007 at 09:45:25PM +0300, Igor S. wrote:

On Sat, Dec 15, 2007 at 07:37:00PM +0100, Abdul-Rahman A. wrote:

There seems to be an other problem, it doesn’t pass the params so…
blog.domain.com/?page_id=2 doesn’t work, where
blog.domain.com/index.php?page_id=2 does work, I added both requirest_uri
and path_info, but no success. Can’t find much on it on the mailinglist.

       error_page  404 /index.php?$arg;
  •        error_page  404 /index.php?$arg;
    
  •        error_page  404 = /index.php?$arg;
    

I was on nginx 5.26… gave a error, upgraded to 5.33 and getting the
same error…

2007/12/15 19:56:07 [emerg] 28620#0: unknown “arg” variable

even tried

  • error_page 404 = /index.php?$arg;
  • error_page 404 = /index.php?$args;

On Sat, Dec 15, 2007 at 08:58:02PM +0100, Abdul-Rahman A. wrote:

I was on nginx 5.26… gave a error, upgraded to 5.33 and getting the
same error…

2007/12/15 19:56:07 [emerg] 28620#0: unknown “arg” variable

even tried

  • error_page 404 = /index.php?$arg;
  • error_page 404 = /index.php?$args;

Yes, I missed: it should be “$args”.
However, you need 0.6.x branch to use it.

There’s another way:

You should inline /etc/nginx/fastcgi.conf to change
fastcgi_param QUERY_STRING $query_string;
to
fastcgi_param QUERY_STRING $orig_args;

and use following configuration (important places are marked by “!”):

location / {
     root /var/www/blog;

! set $orig_args $args;
error_page 404 = /index.php;
}

location ~ \.php$ {
     fastcgi_pass 127.0.0.1:8888;
     fastcgi_param  SCRIPT_FILENAME 

/var/www/blog/$fastcgi_script_name;

! fastcgi_param QUERY_STRING $orig_args;
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;
}

haha, yeah, good hack, but know its not passing the params when I have
like www.blog.com/admin/index.php?edit=5

Is there some coditional coding I can do?

If $orig_args
fastcgi_param QUERY_STRING $orig_args;
else
fastcgi_param QUERY_STRING $args;

On Sat, Dec 15, 2007 at 11:25:39PM +0100, Abdul-Rahman A. wrote:

haha, yeah, good hack, but know its not passing the params when I have
like Sooma

Is there some coditional coding I can do?

If $orig_args
fastcgi_param QUERY_STRING $orig_args;
else
fastcgi_param QUERY_STRING $args;

You need to move

    set   $orig_args   $args;

to a server level.

REQUEST_URI makes it work.

Everythings works great, I will write this up for future reference for
myself and others, and refer to you and this mailinglist. I will mail
the
link.

Thanks for your help and nginx!,

On 12/13/07, Igor S. [email protected] wrote:

be

directory and send that to the fastcgi process?
root /var/www/blog;

help.

ones for your requirements
{

 }

Igor Sysoev
Igor S.
Igor Sysoev


Abdul-Rahman A.

IM: [email protected]

That doesn’t work, it just handles the admin/index.php?bla=bla but not
/?bla=bla and give the args to index.php

Also tried to remove from server level en put in both location blocks,
doesn’t work…

I can life with this but would be good if I could figure it our for
the 5.xxbranch…

On 12/16/07, Igor S. [email protected] wrote:

else

to
location ~ .php$ {
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SERVER_PORT $server_port;

and path_info, but no success. Can’t find much on it on the
Igor Sysoev


Igor S.
Igor Sysoev


Abdul-Rahman A.

IM: [email protected]

On Sun, Dec 16, 2007 at 03:34:44PM +0100, Abdul-Rahman A. wrote:

That doesn’t work, it just handles the admin/index.php?bla=bla but not
/?bla=bla and give the args to index.php

Also tried to remove from server level en put in both location blocks,
doesn’t work…

You should use “set $orig_args $args;” on server level only.

I can life with this but would be good if I could figure it our for
the 5.xxbranch…

It will be merged in 0.5.35, probably before New Year.

Jups, thats what I did, Ill wait for 0.5.35.

On 12/16/07, Igor S. [email protected] wrote:

have

wrote:

  • error_page 404 = /index.php?$args;
   fastcgi_pass 127.0.0.1:8888;
   fastcgi_param  DOCUMENT_URI       $document_uri;
   fastcgi_param  SERVER_NAME        $server_name;

On Sat, Dec 15, 2007 at 09:45:25PM +0300, Igor S. wrote:

mailinglist.


Igor S.
Igor Sysoev


Abdul-Rahman A.

IM: [email protected]

Doesn’t work for me, even if I try Sooma

For single blog installations I’ve just used this after the location of
static files:

if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
break;
}

or

if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
break;
}

ok I’m running 0.6.4. maybe thats why mine didn’t work for you.

Abdul-Rahman A. wrote:

Doesn’t work for me, even if I try Sooma

HonDev’s regex rewrite doesn’t work for me. I found a few other sites
that list the same or slightly modified rewrite rule. After 10 hours
trying to figure out wtf the problem was (I just installed and started
using nginx today) I realized it had to be the rewrite rule so I
examined it more closely. Below is the rewrite rule that works for me
with

Wordpress 2.3.1
nginx version: nginx/0.5.34

Make sure you clear your cache after changing the rewrite rule.

      if (!-e $request_filename) {
        rewrite ^(.*)$ $1/index.php last;

If anyone wants to see my nginx.conf I can post it here. Also, if the
rewrite rule works for you please reply and let me know. Thanks!