A Wordpress broken link helper
Just an example.
For wordpress ppl who changed their perm link.
- <?php
- require_once( dirname(__FILE__) . '/wp-load.php' );
- global $wpdb;
- $uri = $_SERVER["REQUEST_URI"];
- $pattern = "/^\/(\d{4})\/(\d{2})\/(\d{2})\/([^\d].*|\d+[.^-]*).html$/";
- if (preg_match($pattern, $uri, $match) == 1)
- {
- $year = $match[1];
- $month = $match[2];
- $mday = $match[3];
- $post_name = $match[4];
- if ((string)(int)$post_name == $post_name) {
- $id = (int)$post_name;
- $link = get_permalink($id);
- if ($link)
- wp_redirect($link, '301'); // Permanent redirect
- }
- $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID=post_id ".
- "AND YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
- "AND post_name = \"$post_name\" LIMIT 1";
- $id = (int)$wpdb->get_var($query);
- $link = get_permalink($id);
- if ($link) wp_redirect($link, '301'); // Permanent redirect
- $query = "SELECT ID FROM $wpdb->posts WHERE ".
- "YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
- "AND post_name = \"$post_name\" LIMIT 1";
- $id = (int)$wpdb->get_var($query);
- $link = get_permalink($id);
- if ($link) wp_redirect($link, '301'); // Permanent redirect
- $link = "/$year/$month/$mday/";
- if (strstr($_SERVER["HTTP_USER_AGENT"], "Mozilla") === FALSE) // if it's not a user
- {
- wp_redirect($link, '302');
- }
- ?>
- <html><head>
- <meta http-equiv="refresh" content="5;url=<?php echo $link;?>" / > <title>Page has moved</title> </head>
- <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>
- </body></html>
- <?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.
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












