Nginx + wordpress rewrite question

Hello,

This is a really basic question. I’m unfamiliar with nginx (didn’t even
know it existed until a couple days ago) and not a master at URL
rewrites. I have been tasked with hooking up Wordpress to nginx. The
basic setup was easy enough. However, I’m running into some problems
with the rewrites. I’ve been asked to rewrite from the static permalink
format…

“/blog/$postname$-$post_id$” (e.g. /blog/my_blog_post_title-49) => to
the standard “/blog/?p=N”

I’ve tried a bunch of variations, but I keep getting it wrong because
the non-post content (css, images, etc.) are returning 404s. Does anyone
have an example of how to do a nginx rewrite that does the above?

thanks for any suggestions,

Jaime Magiera

Sensory Research, Inc.
http://www.sensoryresearch.net

On Mon, 2010-02-08 at 19:09 -0500, Jaime Magiera wrote:

to the standard “/blog/?p=N”

I’ve tried a bunch of variations, but I keep getting it wrong because
the non-post content (css, images, etc.) are returning 404s. Does
anyone have an example of how to do a nginx rewrite that does the
above?

You should probably setup a separate location for the static content
(images, css, etc), that way your rewrite doesn’t affect them.

Cliff

Wordpress uses post_name for queries from the url. Out of the box it
only supports hyphens and not underscores. You need to also change
your permalink settings in wordpress admin Settings > Permalinks.

The rewrite only needs to use try_file conf as outlined in the
wordpress config on wiki.nginx.org. That or:

error_page 404 /index.php?q=$uri;

You can probably do something like this at best:

/blog/my-blog-post-title-49
or
/blog/my-blog-post-title-49.html

On Tue, Feb 9, 2010 at 11:09 AM, Jaime Magiera

On Tue, Feb 9, 2010 at 7:09 AM, Jaime Magiera
[email protected] wrote:

Jaime Magiera

Sensory Research, Inc.
http://www.sensoryresearch.net

wordpress is pretty smart in handling rewrites

here’s example for wordpress installation in a root of a domain:

server {
listen 80;
server_name my.blog.com;
location ~ ^/.*.php$ {
root /srv/http/wordpress;
fastcgi_pass 127.0.0.1:9100;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location / {
index index.php;
root /srv/http/wordpress;
try_files $uri /index.php;
expires max;
}
}


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Not sure whether this would help, but WP usually is able to handle
“rewriting” by itself.

I only need this config to get WP to work:

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

location ~ .php$
{
fastcgi_pass (your php socket/port here);
}

And WP has to be configured correctly in Settings > Permalinks (like
what
Hone W. said).

Ray.

On Tue, Feb 9, 2010 at 2:21 PM, Edho P Arief [email protected]
wrote:

  fastcgi_index index.php;

inside a directory, it’d be either:

server {
listen 80;
server_name my.blog.com;
location ~ ^/blog/.*.php$ {
root /srv/http/wordpress;
fastcgi_pass 127.0.0.1:9100;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location /blog/ {
index index.php;
root /srv/http/wordpress;
try_files $uri /index.php;
expires max;
}
location = /blog {
rewrite ^ /index.php;
}
}

and then create symlink /srv/http/wordpress/blog pointing to
/srv/http/wordpress;

or

server {
listen 80;
server_name my.blog.com;
location ~ ^/blog/(.*.php)$ {
alias /srv/http/wordpress/$1;
fastcgi_pass 127.0.0.1:9100;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location /blog/ {
index index.php;
alias /srv/http/wordpress/;
error_page 404 = /index.php;
expires max;
}
location = /blog {
rewrite ^ /blog/index.php;
}
}


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Mon, Feb 8, 2010 at 11:34 PM, Edho P Arief [email protected]
wrote:

fastcgi_pass 127.0.0.1:9100;
}
root /srv/http/wordpress;
}
server {
index index.php;

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org


nginx mailing list
[email protected]
http://nginx.org/mailman/listinfo/nginx

http://wiki.nginx.org/Wordpress

No need for symlinks, and a million ways to do things. Still need to
add in that supercache config I wrote like a year ago for someone but
I want to test it first :P~.

– Merlin

On Wed, Feb 10, 2010 at 6:37 AM, merlin corey [email protected]
wrote:

http://wiki.nginx.org/Wordpress

No need for symlinks, and a million ways to do things. Â Still need to
add in that supercache config I wrote like a year ago for someone but
I want to test it first :P~.

it doesn’t cover when the wordpress installation is in
/some/where/wordpress and need to be accessible under
www.myweb.com/blog, not www.myweb.com/wordpress .


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Sat, Feb 13, 2010 at 7:58 AM, merlin corey [email protected]
wrote:

it does not work when the wordpress is installed in a subdirectory
named other than blog.


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Tue, Feb 9, 2010 at 4:58 PM, Edho P Arief [email protected]
wrote:

it doesn’t cover when the wordpress installation is in
/some/where/wordpress and need to be accessible under
www.myweb.com/blog, not www.myweb.com/wordpress .

Actually, this one does:
http://wiki.nginx.org/Wordpress#Non-root_try_files_to_internal_location

You would just change the location to /blog instead of /wordpress.

Do I assume too much about the reader’s understanding of location in
nginx and should I make this section more clear?

– Merlin

OK, I appreciate all your help. It seems I’ve messed things up even more
now. While attempting to translate what you folks provided, I’ve now got
a configuration that doesn’t work right even with standard wordpress
links. If I try to go to the default URL for the blog, I end up getting
a download of the php instead of the page displaying (however, some
other wordpress URLS work?). I’m really not getting nginx. Can someone
perhaps point out what it is that I’m not getting. Taking this project
over cold with no nginx experience is becoming a very frustrating
experience.

/////////////////////////

worker_processes 1;

events {
worker_connections 1024;
}

http {
passenger_root
/opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.2;
passenger_ruby /opt/ruby-enterprise-1.8.6-20090610/bin/ruby;
passenger_default_user playsay;
include mime.types;
default_type application/octet-stream;

sendfile        on;
keepalive_timeout  65;

gzip  on;

client_max_body_size 512m;
server {
    listen 80;
    server_name www.playsay.com;
    root /home/playsay/apps/playsay/current/public;
    access_log /home/playsay/logs/access.log;
    error_log /home/playsay/logs/error.log;
    passenger_enabled on;
    location /files {
        root /home/playsay;
        internal;
    }

    index           index.php index.html;
    location ~ \.php$ {
            fastcgi_index  index.php;
            fastcgi_pass   localhost:9000;
            fastcgi_param  SCRIPT_FILENAME
                   $document_root$fastcgi_script_name;
            include        /opt/nginx/conf/fastcgi_params;
    }

    location /blog {
            passenger_enabled off;
            index index.html index.htm index.php;
            try_files $uri $uri/ @blog;
    }

    location @blog {
            include        /opt/nginx/conf/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME 

$document_root/blog/index.php;
fastcgi_param QUERY_STRING q=$uri&$args;
}


}
}

Jaime Magiera

Sensory Research, Inc.
http://www.sensoryresearch.net

On Tue, Feb 16, 2010 at 7:28 PM, Jaime Magiera
[email protected] wrote:

http {

      internal;

    }
you forgot fastcgi_pass.
and QUERY_STRING is not needed.

http://nginx.org/mailman/listinfo/nginx


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Feb 16, 2010, at 8:30 AM, Edho P Arief wrote:

you forgot fastcgi_pass.
and QUERY_STRING is not needed.

OK, that would give me what’s copied below. However, I’m still getting a
download of the php when I navigate to /blog instead of the php content.
Every other .php file in the wordpress directory is working. Any other
thoughts? This is stumping me.

index index.php index.html;
location ~ .php$ {
fastcgi_index index.php;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include /opt/nginx/conf/fastcgi_params;
}

   location /blog {
           passenger_enabled off;
           index index.html index.htm index.php;
           try_files $uri $uri/ @blog;
   }

   location @blog {
           include        /opt/nginx/conf/fastcgi_params;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME 

$document_root/blog/index.php;
fastcgi_pass localhost:9000;
}

Jaime Magiera

Sensory Research, Inc.
http://www.sensoryresearch.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs