The MU forums have moved to WordPress.org

wordpress rewrites on nginx (35 posts)

  1. br41n
    Member
    Posted 17 years ago #

    hello

    i installed WPMU today on nginx ( http://sysoev.ru/en/ ) and everything seems ok except one "small" issue, i don't know how to convert the rewrite rules.
    So if anybody has any experience or knows how to do it i'd be gratefull.

    Thank you.

  2. br41n
    Member
    Posted 17 years ago #

    22 hours and no reply...
    cmon people, im sure im not the only one using/trying nginx or the only one not understanding how to translate the rewrite rules :)
    Please help.

  3. drmike
    Member
    Posted 17 years ago #

    - Don't use nginx.

    - Actually never heard of it until your post.

    - Hate rewrite rules actually but I can do them. :)

    - Have no idea where you issue is within the rewrite rules or where you're getting stuck or what they're suppose to look like when you're done.

    Willing to help though. Just don't know where things need to be changed.

  4. kingler
    Member
    Posted 17 years ago #

    I use nginx as the front-end load balancer and also serves static contents, but for WPMU, the requests are passed on to back-end Apache servers. I chose nginx, because the rumor about memory leak problems in Lighttpd.

    I am not confident enough to change the rewrite rules in WPMU. Considering that these rules might change in every release, it is A LOT OF trouble for the future.

    Anyway, just my 2 cents. Hopefully some guru could stand up for this.

  5. br41n
    Member
    Posted 17 years ago #

    i want to squeeze every bit of performance i can and using nginx has done wonders on one of the websites i administrate
    so recently i was asked to take care of few "big" blogs and i was thinking i could try nginx on them too.
    so basicly i don't want to do anything special, just make a default WPMU install work on nginx, aka 'translate' these default rules:

    RewriteRule ^(.*)?/?files/(.*) wp-content/blogs.php?file=$2 [L]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule . - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]

    ... for nginx

    I got as far as not getting 404 when trying to visit a test blog on WPMU with the following rules ..but the design was screwed (had only text and some images on it like it was made on Notepad 10 years ago or so).

    rewrite ^(.*)?/?files/(.*) /wp-content/blogs.php?file=$2;

    location / {
    root /var/www/localhost/htdocs/sub.domain.tld;

    if (!-e $request_filename) {
    rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
    rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
    rewrite ^ /index.php last;
    }

  6. quenting
    Member
    Posted 17 years ago #

    I've been thinking about using ngix myselft, but only as a front reverse proxy, not as the actual http server. Nginx doesn't fork child processes right ? I wouldn't handle MU to a server which doesn't.

  7. andrea_r
    Moderator
    Posted 17 years ago #

    "but the design was screwed (had only text and some images on it like it was made on Notepad 10 years ago or so)."

    Which just means it wasn't snagging the stylesheet, so a look at the source of the page would have told you what url it was trying to grab, and thus help to narrow down the problem. :)

  8. br41n
    Member
    Posted 17 years ago #

    To make Wordpress Mu work in nginx (which btw quenting doesnt work like you suspected, it's async so it doesn't spawn processes for every request) you have to add the following in your server { .. directive

    rewrite ^.*/files/(.*) /wp-content/blogs.php?file=$1;

    if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    rewrite ^ /index.php last;
    }

    Thanks Igor and Chris for their great support.

    p.s. maybe this example will be added to next WP/WPMU release or something :) it would help some ppl that just don't wanna use Apache anymore :)

  9. drmike
    Member
    Posted 17 years ago #

    You could write it up and I can add it into the trac. Would love to see someone doa write up for lightspeed or whatever it's called and IIS as well.

  10. br41n
    Member
    Posted 17 years ago #

    Sorry i didnt get what to write up... the options that make WPMU work on nginx are posted in my previous post, if more info is needed just tell me and i'll gladly help

  11. gaper
    Member
    Posted 16 years ago #

    First I apologize for my limited English xD, I have WordpressMU working with nginx web server, I want to be grateful to br41n for that his rewrite rules work, the detail is that when I create a blog and a user tries to place a commentary or when the owner of the blog does click in the link of "login", recive one " Not input file specified. ", I will be tnks your support, regards.

  12. honewatson
    Member
    Posted 16 years ago #

    Any ideas on how to get this working in NginX?


    RewriteCond %{HTTP_HOST} ^www\.(.*)
    RewriteRule ^(.*) http://%1/$1 [R,L]

  13. honewatson
    Member
    Posted 16 years ago #

    Ok found it:

    
            # Rewrite http://www.domain.com to domain.com
            if ($http_host ~ "^www.(.*)") {
                set      $name    $1;
                rewrite  ^(.*)    http://$name/$1;
            }
    

    Further info here:

    http://thread.gmane.org/gmane.comp.web.nginx.english/637

    Thanks to Igor.

  14. honewatson
    Member
    Posted 16 years ago #

    I got to say that Nginx is totally awesome.

    I'm serving pages about 4 times faster with a 1/4 of the memory usage.

  15. ales
    Member
    Posted 16 years ago #

    This is the only way that worked me so I didn't get the "No input file specified" mentioned above.

    server {...
    # rewrite rule - fancy urls for wordpress mu  1.2.5                                                                        
    
    rewrite  ^.*/files/(.*) /wp-content/blogs.php?file=$1;
    	if (!-e $request_filename) {
    	rewrite  ^.+?(/wp-.*)     $1          last;
    	rewrite  ^.+?(/.*\.php)$  $1          last;
    	rewrite ^(.+)$ /index.php?q=$1        last;
    	break;
    	}
    
    ...}
  16. ales
    Member
    Posted 16 years ago #

    Don't forget that on 1.2.5 you will most likely need to change the blogs.php file as the following path crapped out on nginx

    require_once( dirname( dirname( __FILE__) ) . '/wp-config.php' );

    I changed it to
    require_once( '/var/www/html/mysubdoimain/wp-config.php' );

    I spent days on this wondering why my files/images aren't showing up in posts.

  17. honewatson
    Member
    Posted 16 years ago #

    I have not had an issue with this ales, but I have got this before the above rewrites...

    location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    {
    root /home/YOURDIRECTORY/public_html;
    expires 30d;
    break;
    }
  18. viulian
    Member
    Posted 16 years ago #

    For those running wpmu inside a folder (such as /blogs/) and not as the main site, these are the rules that work for me:

    location /blogs/ {
                    index index.php;
                    rewrite ^.*/files/(.*) /blogs/wp-content/blogs.php?file=$1;
    
                    if (!-e $request_filename) {
                            rewrite ^.+?(/wp-.*) /blogs/$1 last;
                            rewrite ^.+?(/.*\.php)$ /blogs/$1 last;
                            rewrite ^ /blogs/index.php last;
                    }
            }

    Images were an issue for me too and I had to comment out the static serving part honewatson was talking about.
    Basically, when nginx sees this line:

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|p
    df|ppt|txt|tar|mid|midi|wav|bmp|rtf)$
            {
                    expires 7d;
                    # break;
            }

    it will refuse to match further.. (even if you don't have a break there).

    After commenting them out, it works for me.

  19. honewatson
    Member
    Posted 16 years ago #

    This my conf for Wordpress SubFolder install on Nginx 0.6.4:

    server {
    listen       80;
    server_name  THEBLOG.com;
    
    access_log  logs/THEBLOG.access.log;
    
    location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    {
    root /home/THEBLOG/public_html;
    rewrite rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
    rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-content/blogs.php?file=$1 last;
    expires 10d;
    break;
    }
    
    location / {
    root   /home/THEBLOG/public_html;
    index  index.php;
    
    if (!-e $request_filename) {
    rewrite ^.+/?(/wp-.*) $1 last;
    rewrite ^.+/?(/.*\.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    }
    
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /var/www/nginx-default;
    }
    
    location ~ \.php$ {
    rewrite ^/.*(/wp-.*/.*.php)$ $1;
    fastcgi_pass   127.0.0.1:xxxx;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /home/THEBLOG/public_html$fastcgi_script_name;
    include        /usr/local/nginx/conf/fastcgi_params;
    }
    }
    
    server {
    server_name http://www.THEBLOG.com;
    rewrite ^/(.*) http://THEBLOG.com/$1 permanent;
    }
  20. viulian
    Member
    Posted 16 years ago #

    Well, I have again rewritten the rewrite rules :)

    What happens is that I have my main site on lovetime.ro, but the WPMU blogs are in lovetime.ro/blogs/
    Using the rules I put above - everything seemed to work but NOT the posting. Whenever I tried to post a new blog, I got "No input file was specified"

    Basically, wpmu expects that a URL

    http://lovetime.ro/blogs/Lumealbastra/wp-login.php

    to be rewritten to

    http://lovetime.ro/blogs/wp-login.php

    However, the location ~ \.php$ { ... } location was chosen before the location /blogs/ { } and no rewrite tools were actually applied (especially the rule:

    rewrite ^.+/?(/.*\.php)$ $1 last;

    (that matches everything ending in .php and trims the blog name from the URL).

    Anyway, I was only able to see this after enabling --with-debug in nginx (in fact that nginx pickup the .php location before the /blogs/ one).

    Hence, I rewritten the rules like this:

    location ~ \.php$ {

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

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME;
    .....
    }

    And seems to work :) i can login now and make posts.

  21. cafespain
    Member
    Posted 16 years ago #

    Hi
    Just want to say a big thank you to br41n and everyone else.
    I now have WordPress MU running on nginx (with no problems so far) and it flies.
    Rgds
    Cafespain

  22. honewatson
    Member
    Posted 15 years ago #

    Here is a configuration for 0.6.29 to test for Super Cache.

    Explanation here can be found here http://bookmarks.honewatson.com/2008/04/24/nginx-supercache-wp-super-cache-plugin-rewrites-wordpress-mu/

    server {
    listen       80;
    server_name  yourmublogs.com *.yourmublogs.com;
    
    location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    {
    root /home/yourmublogs/public_html;
    rewrite ^/.*(/wp-admin/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
    rewrite ^/.*(/wp-includes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
    rewrite ^/.*(/wp-content/themes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
    rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-content/blogs.php?file=$1 last;
    expires 10d;
    break;
    }
    
    location / {
    root   /home/yourmublogs/public_html;
    index  index.html index.htm index.php;
    
    if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    }
    
    if ($http_cookie !~* "comment_author_|wordpress|wp-postpass_" ) {
    rewrite ^(.*)$ /wp-content/cache/supercache/$http_host/$1index.html;
    break;
    }
    
    error_page    404  =  @tricky;
    }
    
    location @tricky {
    rewrite ^ /index.php last;
    rewrite ^/.*(/wp-login.php)$ $1;
    rewrite ^/.*(/wp-admin/.*\.php)$ $1;
    rewrite ^/.*(/wp-includes/.*\.php)$ $1;
    fastcgi_pass   127.0.0.1:8084;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /home/yourmublogs/public_html$fastcgi_script_name;
    include /usr/local/nginx/conf/fastcgi_params;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
    }
    
    server {
    server_name http://www.yourmublogs.com;
    rewrite ^/(.*) http://yourmublogs.com/$1 permanent;
    }
  23. honewatson
    Member
    Posted 15 years ago #

    I had to change my statics rewrites as so:

    rewrite ^/.*(/(wp-admin|wp-includes)/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
        rewrite ^/.*(/wp-content/(themes|plugins|mu-plugins)/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
        rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-content/blogs.php?file=$1 last;
  24. greendev
    Member
    Posted 15 years ago #

    had a tough time to figure out what exactly to put in my config file and couldn't make it work (basically 404s for css, problems with logins, etc)

    i found out a neat post with the whole config file, seems to work fine so far:

    http://siava.su/2008/04/28/wordpress-mu-rewrite-rules-for-nginx/

  25. VanDaMe
    Member
    Posted 15 years ago #

    this rewrite rules for nginx is working well. But i have some issues for
    rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-content/blogs.php?file=$1 last;

    I have jpg files located on blogs.dir/1/bla.jpg

    If I call directly, the image can't be load successfull. But if I remove that rewrite, the image load successfully.
    Anybody have this issue before? How can I solve it?

  26. VanDaMe
    Member
    Posted 15 years ago #

    actually the image can't load successfull if my image is bigger than 100Kb

  27. honewatson
    Member
    Posted 15 years ago #

    Supercache

    location ~* ^.+\.(xml|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    {
    root /home/mudirectory/public_html;
    rewrite ^/files(/.*)$ /wp-content/blogs.php?file=$1 last;
    expires 10d;
    break;
    
    }
    
    location / {
                	root   /home/mudirectory/public_html;
                	index  index.html index.htm index.php;
    		if ($http_cookie !~* ".*(comment_author_|wordpress|wp-postpass_).*" ) {
    		rewrite ^(.*)$ /wp-content/cache/supercache/$http_host/$1index.html;
    		break;
    		}
    	    	error_page       404 = /index.php?q=$uri;
    	}
    
    location ~ \.php$ {
    	fastcgi_pass   127.0.0.1:9000;
    	fastcgi_index  index.php;
    	fastcgi_param  SCRIPT_FILENAME /home/mudirectory/public_html$fastcgi_script_name;
    	include        /usr/local/nginx/conf/fastcgi_params;
    }
  28. honewatson
    Member
    Posted 15 years ago #

    Batcache

    location ~* ^.+\.(xml|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    {
    root /home/mudirectory/public_html;
    rewrite ^/files(/.*)$ /wp-content/blogs.php?file=$1 last;
    expires 10d;
    break;
    
    }
    
    location / {
    	rewrite ^/(wp-.*)/$ /$1/index.php last;
            set  $memcached_key  $host$uri;
            memcached_pass   127.0.0.1:9001;
    	default_type     text/html;
            error_page       404 = /index.php?q=$uri;
    }
    
    location ~ \.php$ {
    	fastcgi_pass   127.0.0.1:9000;
    	fastcgi_index  index.php;
    	fastcgi_param  SCRIPT_FILENAME /home/mudirectory/public_html$fastcgi_script_name;
    	include        /usr/local/nginx/conf/fastcgi_params;
    }
  29. jhh2006
    Member
    Posted 15 years ago #

    I can't get the *.wildcard.com to work. What am I missing?

    Thanks!

    server {

    listen 80;
    charset utf-8;

    server_name MYSITE.com;
    index index.php;
    root /home/public_html/MYSITE.com/public/;

    access_log /home/public_html/MYSITE.com/log/access.log;
    error_log /home/public_html/MYSITE.com/log/error.log;

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

    # rewrite rule - fancy urls for wordpress mu

    rewrite ^.*/files/(.*) /wp-content/blogs.php?file=$1;

    if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    rewrite ^ /index.php last;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include /usr/local/nginx/conf/fastcgi_params;
    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 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;
    fastcgi_param SCRIPT_FILENAME /home/public_html/MYSITE.com/public/$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;
    }
    }

  30. honewatson
    Member
    Posted 15 years ago #

    Should be:

    server_name MYSITE.com *.MYSITE.com;

    You should have an A Record in your DNS which points *.MYSITE.com to your servers IP address.

About this Topic