dreamer's profiledreamerPhotosBlogListsMore Tools Help
    10/25/2009

    Apache2 -> Nginx

    之前在網路上看到有網友推薦改用 nginx,「據說」效能比較好,之前有試裝一下,但是 drupal 的 multi-site 的設定有一點點小麻煩,懶得研究,就把這件事一直拖著。昨晚,表妹來家裡泡茶,我跟著喝了兩大杯,趁晚上睡不著的時候,決定來好好實驗一下。

    在 BSD 上要裝 nginx 很簡單,到 /usr/ports/www/nginx 裡打個 make install 就裝起來了,它的 php 支援則是要透過 FastCGI 的介面,這一部份其實可以透過 spawn-fcgi 來完成。之前在網路上查到的資料,說要先裝 lighttpd 然後再把它的 spawn-fcgi 複製出來再反安裝 lighttpd。我試了一下,其實 spawn-fcgi 可以單獨安裝,到 /usr/ports/www/spawn-fcgi 裡 make install 一樣輕鬆搞定。裝完 nginx 與 spawn-fcgi 之後,在 /etc/rc.conf 加入以下兩行:

    spawn_fcgi_enable="YES"
    nginx_enable="YES"

    然後再

    /usr/local/etc/rc.d/spawn-fcgi start
    /usr/local/etc/rc.d/nginx start

    將服務啟動。

    ==========

    先來說明一下伺服器提供的服務,這部機器上有 mysql-server 以及 web,因為之前覺得一般在學校的網路應用(網站)的使用率不會太高,負載其實不怎麼大,所以把一堆應用服務全放在同一台機器上,透過 Virtual Host 的設定提供服務。基本上大部份的服務都是利用 drupal 架設的。

    • app.dcsh.tp.edu.tw(drupal,應用服務)
      在同一個 Document Root 透過 drupal 的 multi-site 的設定,其實它還有幾個分身:
      cc.dcsh.tp.edu.tw(資訊組)
      engweb.dcsh.tp.edu.tw(英文網站)
      repair.dcsh.tp.edu.tw(報修系統)
      repair.dcsh.tp.edu.tw/construction/(施工意見)
      site.dcsh.tp.edu.tw/pa/(家長會)
    • sub.dcsh.tp.edu.tw(drupal,學科網站)
      同樣,各科各設一個分身:
      sub.dcsh.tp.edu.tw/hs-chi/
      sub.dcsh.tp.edu.tw/jh-eng/
      ...
    • query.dcsh.tp.edu.tw(自行撰寫的簡單成績查詢)
    • gallery.dcsh.tp.edu.tw(gallery,網路相簿)

    ==========

    我將 /usr/local/etc/nginx/nginx.conf 裡的所有 server 設定全拿掉,改透過 include 的方式,將 virtual host 的設定分成一個個獨立的檔案。

    http {
        include mime.types;
        ...
        ...
        include /usr/local/etc/nginx/vhosts/*.conf; # <- 新增這行,記得要自己建 vhosts 這個資料夾
    }

    ===== _default.conf BEGIN =====

    server { listen 80 default; # 注意 default server_name _; charset utf-8; access_log /home/www/logs/default.access.log main; error_log /home/www/logs/default.error.log error; root /home/www/sites/app.dcsh.tp.edu.tw; location / { index index.php index.html index.htm; error_page 404 = @drupal; } location @drupal { rewrite ^/(construction|pa)/(.*)$ /$1/index.php?q=$2 last; rewrite ^/(.*)$ /index.php?q=$1 last; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html   error_page 500 502 503 504 /50x.html;

    location = /50x.html { root /usr/local/www/nginx-dist; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

    ===== _default.conf END / sub.conf BEGIN =====

    server {
        listen      80;
        server_name sub sub.dcsh.tp.edu.tw;
        charset     utf-8;
        access_log  /home/www/logs/sub.access.log main;
        error_log   /home/www/logs/sub.error.log error;
        root        /home/www/sites/sub.dcsh.tp.edu.tw;
    
        location / {
            index  index.php index.html index.htm;
            error_page  404 = @subsite;
        }
    
        location @subsite {
            rewrite ^/([^/]*-[^/]*|df-olp|glcp|h1n1|nano|tmp)/(.*)$ /$1/index.php?q=$2  last;
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    ===== sub.conf END / query.conf BEGIN =====

    server {
        listen      80;
        server_name query query.dcsh.tp.edu.tw;
        charset     utf-8;
        access_log  /home/www/logs/query.access.log main;
        error_log   /home/www/logs/query.error.log error;
        root        /home/www/sites/query.dcsh.tp.edu.tw;
    
        location / {
            index  index.php index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    ===== query.conf END / gallery.conf BEGIN =====

    server {
        listen      80;
        server_name gallery gallery.dcsh.tp.edu.tw;
        charset     utf-8;
        access_log  /home/www/logs/gallery.access.log main;
        error_log   /home/www/logs/gallery.error.log error;
        root        /home/www/sites/gallery.dcsh.tp.edu.tw;
    
        location / {
            index  index.php index.html index.htm;
            error_page  404 = @gallery;
        }
    
        location @gallery {
            rewrite ^/d/([0-9]+)-([0-9]+)/([^?]+)$ /main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&g2_fileName=$3 last;
            rewrite ^/v/([^?]+)$ /main.php?g2_path=$1 last;
        }
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    ===== gallery.conf END =====

    改完 nginx 的設定之後,記得要重跑 nginx 才會生效:

    /usr/local/etc/rc.d/nginx restart

    先跑個一陣子觀察看看。 XD

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://idreamer.spaces.live.com/blog/cns!86AB72D5979C4135!861.trak
    Weblogs that reference this entry
    • None