使用者工具

網站工具


tech:nginx_rewrite

nginx 的 rewrite 設定

  • 主要是因為 dokuwiki 原本使用的 apache 改成 nginx 之後遇到 rewrite 無法如預期的運作所進行的設定紀錄.

啟動 nginx 的 rewrite 運作紀錄

  • vi /etc/nginx/conf.d/default.conf
    server {
      listen 80;
      server_name wiki.ichiayi.com www.ichiayi.com;
    :
      index index.html index.htm index.php doku.php;
      access_log  /var/log/nginx/wiki.ichiayi.com/access.log;
      error_log  /var/log/nginx/wiki.ichiayi.com/error.log notice;
      #error_log  /var/log/nginx/wiki.ichiayi.com/error.log;
      rewrite_log on;
      root /var/www/html;
    :

    主要是在 error_log 後面增加 notice 以及增加 rewrite_log on

  • 重新啟動 nginx 讓設定生效
    systemctl restart nginx
  • 開始觀察 nginx rewrite 運作紀錄
    tail -f /var/log/nginx/wiki.ichiayi.com/error.log

    Exp.

    :
    2018/07/24 20:27:32 [notice] 894#894: *56 "^/wiki/_detail/(.*)" does not match "/wiki/tag/vmware", client: 192.168.11.101, server: wiki.ichiayi.com, request: "GET /wiki/tag/vmware
    2018/07/24 20:27:32 [notice] 894#894: *56 "^/wiki/_export/([^/]+)/(.*)" does not match "/wiki/tag/vmware", client: 192.168.11.101, server: wiki.ichiayi.com, request: "GET /wiki/tag/vmware
    2018/07/24 20:27:32 [notice] 894#894: *56 "^/wiki/(.*)" matches "/wiki/tag/vmware", client: 192.168.11.234, server: wiki.ichiayi.com, request: "GET /wiki/tag/vmware
    2018/07/24 20:27:32 [notice] 894#894: *56 rewritten data: "/wiki/doku.php", args: "id=tag/vmware", client: 192.168.11.101, server: wiki.ichiayi.com, request: "GET /wiki/tag/vmware
    :
  • 可以透過 rewritten data: “/wiki/doku.php”, args: “id=tag/vmware” 這樣的資訊來判別是否與預期的設定相符

設定 /wiki 為 dokuwiki 網址目錄範例

  • 依照 nginx 網站針對 dokuwiki 的設定範例修改
    vi /etc/nginx/conf.d/default.conf
    server {
      listen 80;
      server_name wiki.ichiayi.com www.ichiayi.com;
      autoindex off;
      client_max_body_size 15M;
      client_body_buffer_size 128k;
      index index.html index.htm index.php doku.php;
      access_log  /var/log/nginx/wiki.ichiayi.com/access.log;
      #error_log  /var/log/nginx/wiki.ichiayi.com/error.log notice;
      error_log  /var/log/nginx/wiki.ichiayi.com/error.log;
      #rewrite_log on;
      root /var/www/html;
     
      location / {
        try_files $uri $uri/ @wiki;
      }
     
      location ~ ^/wiki/lib.*\.(gif|png|ico|jpg)$ {
        expires 30d;
      }
     
      location = /robots.txt  { access_log off; log_not_found off; }
      location = /favicon.ico { access_log off; log_not_found off; }
      location ~ /\.          { access_log off; log_not_found off; deny all; }
      location ~ ~$           { access_log off; log_not_found off; deny all; }
     
      location @wiki {
        rewrite ^/wiki/_media/(.*) /wiki/lib/exe/fetch.php?media=$1 last;
        rewrite ^/wiki/_detail/(.*) /wiki/lib/exe/detail.php?media=$1 last;
        rewrite ^/wiki/_export/([^/]+)/(.*) /wiki/doku.php?do=export_$1&id=$2 last;
        rewrite ^/wiki/(.*) /wiki/doku.php?id=$1 last;
      }
     
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
      }
     
      location ~ /(data|conf|bin|inc)/ {
        deny all;
      }
     
      location ~ /\.ht {
        deny  all;
      }
     
    }

參考網址

tech/nginx_rewrite.txt · 上一次變更: 2018/07/24 20:38 由 Jonathan Tsai