FVN Tech Blog Coding or Programming in C#/PHP | Linux or Windows Server Backend | New Media | SNS | Misc …

20Jan/097

A Wordpress broken link helper

Just an example.
For wordpress ppl who changed their perm link.

  1. <?php
  2. require_once( dirname(__FILE__) . '/wp-load.php' );
  3.  
  4. global $wpdb;
  5.  
  6. $uri = $_SERVER["REQUEST_URI"];
  7. $pattern = "/^\/(\d{4})\/(\d{2})\/(\d{2})\/([^\d].*|\d+[.^-]*).html$/";
  8. if (preg_match($pattern, $uri, $match) == 1)
  9. {
  10.     $year = $match[1];
  11.     $month = $match[2];
  12.     $mday = $match[3];
  13.     $post_name = $match[4];
  14.  
  15.     if ((string)(int)$post_name == $post_name) {
  16.         $id = (int)$post_name;
  17.         $link = get_permalink($id);
  18.         if ($link)
  19.             wp_redirect($link, '301'); // Permanent redirect
  20.     }
  21.     $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID=post_id ".
  22.         "AND YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  23.         "AND post_name = \"$post_name\" LIMIT 1";
  24.  
  25.     $id = (int)$wpdb->get_var($query);
  26.     $link = get_permalink($id);
  27.     if ($link) wp_redirect($link, '301'); // Permanent redirect
  28.  
  29.     $query = "SELECT ID FROM $wpdb->posts WHERE ".
  30.         "YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  31.         "AND post_name = \"$post_name\" LIMIT 1";
  32.     $id = (int)$wpdb->get_var($query);
  33.     $link = get_permalink($id);
  34.         if ($link) wp_redirect($link, '301'); // Permanent redirect
  35.  
  36.     $link = "/$year/$month/$mday/";
  37.  
  38.     if (strstr($_SERVER["HTTP_USER_AGENT"], "Mozilla") === FALSE) // if it's not a user
  39.     {
  40.         wp_redirect($link, '302');
  41.     }
  42. ?>
  43. <html><head>
  44. <meta http-equiv="refresh" content="5;url=<?php echo $link;?>" / > <title>Page has moved</title> </head>
  45. <body><p>The permant link changes, however <i>broken link helper</i> can't solve it automaticlly, it will redirect you to <a href="<?php echo $link;?>">the date archive page</a>. It would be nice if you can report broken links to feuvan#feuvan.net.</p>
  46. </body></html>
  47. <?php } ?>

Corresponding nginx configuration block:

server {
listen 80;
server_name blog.feuvan.net;

root /home/feuvan/wwwdata/blog;
index index.php;
if (-e $request_filename) {
break;
}

rewrite ^/index.php/(.+)$ /index.php?q=$1 last;
rewrite "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\d].*|\d+[.^-]*).html" /brokenlinkhelper.php last;
#rewrite ^/(.+)$ /index.php?q=$1 last;
rewrite ^/ /index.php last;

location ~ \.php$ {
fastcgi_pass unix:/tmp/php.sock;
}
}

Hope it helps.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: , 7 Comments
17Apr/072

Configure nginx as front-end web server

I've been using lighttpd for a long time, it's faster and stable. But 1.4.x series of lighttpd lacks some features I need: powerful proxy mod, dav_svn mod, real IP extract (as mod_extract_forwarded in apache).

Most of the bandwidth of feuvan.net is contributed to a web game proxy using proxy mod of lighttpd 1.4.13 : http://chaoswar.feuvan.net (official: http://alpha.chaoswar.cn:8080 ),  the lighttpd causes many TIME_WAIT connections in high load condition. Therefore, I want to use a powerful proxy like squid to do the proxy work. nginx ( Engine X ) is my selection. Squid is too heavy ;-).

It's easy to install in debian, just type aptitude install nginx as root. Then I changed lighttpd listening port to localhost:81, and configure nginx to run on 0.0.0.0:80.

nginx.conf:

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  4096;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

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

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  35;
    tcp_nodelay        on;

    gzip  on;

    server {
        listen       80;

        location / {
            proxy_pass http://127.0.0.1:81;
            include         /etc/nginx/proxy.conf;
        }

    }

    upstream chaoswar_cn {
        server alpha.chaoswar.cn:8080;
    }
    server {
        listen          80;
        server_name     chaoswar.feuvan.net;

        location / {
            proxy_pass http://chaoswar_cn;
            include         /etc/nginx/proxy.conf;
        }
    }
}

proxy.conf:

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   30;
proxy_send_timeout      30;
proxy_read_timeout      30;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

The most content of configure files are copied from nginx wiki, it's easy to read. And may be I can do more optimizations like static content (picuture) cache ( expire 30d?). But the main purpose of this article is to show you a general whole picture of the proxy function of Nginx.

BTW, nginx is not just a light-weight squid like proxy server! It's also a HTTP/MAIL Server.

Anyway, I'm still waiting for 1.5.0 release of lighttpd.

See also: nginx, nginx wiki, lighttpd 1.4.x mod_proxy, lighttpd 1.5.x mod_proxy_core

Nginx vs Lighttpd for a small VPS

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: , 2 Comments

Pages

Recent Posts

Categories

Tags

apache baidu C# Debian dotNET economy gadget GMA Google hulu IE7 Intel jiwai lighttpd Linux Mobile mod_extforward mojiti nginx oscanner OVAL Parallel PHP phproxy politics proxy Python regsvr32 SNS squid subversion trac TV Vista Visual Studio Windows Live Windows Mobile WLM wordpress wow WTL wzdftpd XDebug XML Zend Framework

Archives

Meta

友情链接与广告

Free web proxy
Old friends:

NSClub: