SUCKUP.de

Nginx + WordPress

This blog post has been published on 2010-08-30 and may be out of date.

Ich habe bereits in einem vorherigem Blog-Post erklärt, wie du “Nginx” (Webserver) unter Debian und / oder Ubuntu installierst. Heute zeige ich mit einigen Beispielen, wie du Nginx konfigurierst, diese Einstellungen werden in dem entsprechendem “Virtual Host” (/etc/nginx/sites-enabled/*.conf) durchgeführt.

## Slash am Ende anfuegen
if ($request_uri ~* "^[\w\-\/]+[^\/?]$") {
    rewrite ^(.*)$ $scheme://$host$1/ permanent;
}
## entferne doppel-slashes //
if ($request_uri ~* "\/\/") {
    rewrite ^/(.*) $scheme://$host/$1 permanent;
}
## www eifuegen
if ($host !~* ^(www|subdomain)) {
    rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
}
## www vorne entfernen
if ($host ~* ^www\.(.*)) {
    set $host_without_www $1;
    rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
## immer SSL verwenden
    rewrite ^(.*) https://$server_name$1 permanent;
## immer HTTP verwenden
    rewrite ^(.*) http://$server_name$1 permanent;
## WordPress3
if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
}
## WordPress3 MU
if (!-e $request_filename) {
    rewrite ^.+/?(/wp-.*) $1 last;
    rewrite ^.+/?(/.*\.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
}
## WordPress3 Multi-Domain
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename) {
    rewrite ^.+/?(/ms-.*) $1 last;
    rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
    rewrite ^.+/?(/wp-.*) $1 last;
    rewrite ^.+/?(/.*.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
}
location ~* ^.+.(htm|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)$ {
    access_log off;
    expires max;
    root /var/www/www.domain.de/web/;
    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-includes/ms-files.php?file=$1 last;
    if (!-e $request_filename) {
        rewrite ^.+/?(/wp-.*) $1 last;
        rewrite ^.+/?(/.*.php)$ $1 last;
        rewrite ^(.+)$ /index.php?q=$1 last;
        access_log off;
        expires max;
        break;
    }
}
## WordPress-Forum
if (!-e $request_filename) {
    rewrite ^/forums/topic/(.*)$ /forums/topic.php?q=$1 last;
    rewrite ^/forums/forum/(.*)$ /forums/forum.php?q=$1 last;
    rewrite ^/forums/profile/(.*)$ /forums/profile.php?q=$1 last;
    rewrite ^/forums/view/(.*)$ /forums/view.php?q=$1 last;
    rewrite ^/forums/tags/(.*)$ /forums/tags.php?q=$1 last;
    rewrite ^/forums/rss/(.*)$ /forums/rss.php?q=$1 last;
    rewrite ^/forums/bb-admin/ /forums/bb-admin/index.php last;
    rewrite ^/forums/ /forums/index.php last;
    break;
}
## Feedburner
if ($http_user_agent !~ FeedBurner) {
    rewrite ^/comment/feed/?$ http://feeds.feedburner.com/domain last;
    rewrite ^/feed/?$ http://feeds.feedburner.com/domain last;
}
## WordPress W3 Total Cache
set $totalcache_file '';
set $totalcache_uri $request_uri;
if ($request_method = POST) {
    set $totalcache_uri '';
}
if ($query_string) {
    set $totalcache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
    set $totalcache_uri '';
}
if ($totalcache_uri ~ ^(.+)$) {
    set $totalcache_file /wp-content/w3tc-$http_host/pgcache/$1/_default_.html;
}
if (-f $document_root$totalcache_file) {
    rewrite ^(.*)$ $totalcache_file break;
}

ggf. kann man auch gzip-Dateien ausgeben lassen: set $totalcache_file /wp-content/w3tc$http_host/pgcache/$1/_default_.html.gzip;

## WordPress SuperCache
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
    set $supercache_uri '';
}
if ($query_string) {
    set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
    set $supercache_uri '';
}
if ($supercache_uri ~ ^(.+)$) {
    set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}
if (-f $document_root$supercache_file) {
    rewrite ^(.*)$ $supercache_file break;
}
if (!-e $request_filename) {
    rewrite . /index.php last;
}
location ~* ^.+.(htm|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)$ {
    access_log off;
    expires max;
    root /var/www/www.domain.de/web/;
}
## .htaccess & .htpassword sperren
location ~ /\.ht {
    deny  all;
}


Weitere Infos findest du im Wiki von nginx: wiki.nginx.org/Wordpress