Nginx-Konfiguration: Schöne URL`s - Rewrite

Nginx Sample Configuration of DokuWiki

Configuration Setting: userewrite

Enable this to use rewriting for nicer, search engine and people friendly, URLs.

  • Type: Number
  • Default: 0

You can set the following values:

Value Info Example URL
0 Default. No URL rewriting is used. http://example.com/dokuwiki/doku.php?id=wiki:syntax
1 URL rewriting is done in conjunction with the webserver. e.g. With Apache you need to setup an .htaccess file http://example.com/dokuwiki/wiki:syntax
2 The rewriting is done by DokuWiki. http://example.com/dokuwiki/doku.php/wiki:syntax

Danger: Changing this option will make your wiki and the configuration menu inaccessible. You must additionally configure your web server accordingly, follow Rewrite.

sample.conf with rewrite

http://192.168.0.10/

server {
listen   80;
    server_name  sub1.example.com;
 
    access_log  /var/log/nginx/sub1.example.com.access.log;
    error_log   /var/log/nginx/sub1.example.com.error.log;
 
    #maximum file upload size is 4MB - change accordingly if needed
    client_max_body_size 4M;
    client_body_buffer_size 128k;
 
    rewrite ^(/)_media/(.*) $1lib/exe/fetch.php?media=$2 last;
    rewrite ^(/)_detail/(.*) $1lib/exe/detail.php?media=$2 last;
    rewrite ^(/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 last;
 
    location / {
        root /usr/share/nginx/sub1.example.com;
        index  index.html index.htm index.php;
        if (!-f $request_filename) {
            rewrite ^(/)(.*)?(.*)  $1doku.php?id=$2&$3 last;
            rewrite ^(/)$ $1doku.php last;
        }
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/sub1.example.com;
    }
 
    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php index.html;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
 
    location ~ /\.ht {
        deny  all;
    }
 
    location ~ ^/(data|conf|bin|inc)/ {
        deny all;
    }
 
    location ~ ^/lib/^((?!php).)*$ {
        root html/dokuwiki/lib;
        expires 30d;
    }
 
}

sample.conf with rewrite

http://192.168.0.10/dokuwiki/

server {
    listen  80 default_server;
    server_name localhost 192.168.0.10;
 
    root    /usr/share/nginx/html;
    index   index.php index.html doku.php;
 
    #/dokuwiki/
    rewrite ^(/dokuwiki/)_media/(.*) $1lib/exe/fetch.php?media=$2 last;
    rewrite ^(/dokuwiki/)_detail/(.*) $1lib/exe/detail.php?media=$2 last;
    rewrite ^(/dokuwiki/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 last;
 
 
    location /dokuwiki/ {
        if (!-f $request_filename) {
            rewrite ^(/dokuwiki/)(.*)?(.*)  $1doku.php?id=$2&$3 last;
            rewrite ^(/dokuwiki/)$ $1doku.php last;
        }
    }
 
    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php index.html;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
 
    location ~ /\.ht {
        deny  all;
    }
 
 
    location ~ ^/dokuwiki/(data|conf|bin|inc)/ {
        deny all;
    }
 
    location ~ ^/dikuwiki/lib/^((?!php).)*$ {
        root html/dokuwiki/lib;
        expires 30d;
    }
 
}

sample.conf with rewrite

http://192.168.0.10/dokuwiki/site/

server {
    listen  80 default_server;
    server_name localhost 192.168.0.10;
 
    root    /usr/share/nginx/html;
    index   index.php index.html doku.php;
 
    #/dokuwiki/site/
    rewrite ^(/dokuwiki/site/)_media/(.*) $1lib/exe/fetch.php?media=$2 last;
    rewrite ^(/dokuwiki/site/)_detail/(.*) $1lib/exe/detail.php?media=$2 last;
    rewrite ^(/dokuwiki/site/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 last;
 
    location /dokuwiki/site/ {
        if (!-f $request_filename) {
            rewrite ^(/dokuwiki/site/)(.*)?(.*)  $1doku.php?id=$2&$3 last;
            rewrite ^(/dokuwiki/site/)$ $1doku.php last;
        }
    }
 
 
    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php index.html;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
 
    location ~ /\.ht {
        deny  all;
    }
 
    location ~ ^/dokuwiki/(data|conf|bin|inc)/ {
        deny all;
    }
 
    location ~ ^/dikuwiki/lib/^((?!php).)*$ {
        root html/dokuwiki/lib;
        expires 30d;
    }
 
}