Nginx Multiple Root

在做網站改版的時候,可能因為專案太大而無法一次全部改版,所以希望能夠針對部分的 頁面或API 進行改版,然後慢慢的全部迭代改版完成,為了做到這樣的效果,可以透過 Nginx 指定不同的網址路徑,去執行不同的程式,這邊以 PHP Laravel Framework 為例。

由於舊專案是 Laravel 4.2 但新專案是 Laravel 5.5,程式改版過程中將改版完成的網址路徑導向至 Laravel 5.5,但尚未完成改版的項目則還是繼續導向至 Laravel 4.2,由 nginx 判斷請求路徑,去控制判斷要執行哪一部分的程式。

server {
    listen 80;
    server_name _;
    # 預設使用 Laravel 5.5
    root "/home/deploy/laravel55/public";
    charset utf-8;
    index index.htm index.html index.php;
    error_page 404 /404.html;


    if ($request_uri ~* "^(.*/)index\.php/*(.*)") {
        return 301 $1$2;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location /elb-status {
        access_log off;
        return 200;
    }

    access_log /var/log/nginx/web-access.log cloudflare;
    error_log  /var/log/nginx/web-error.log error;
    location = /robots.txt { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }

    location ~ ^/assets/ {
        try_files $uri $uri/ /index.php?$args;
        expires max;
        add_header Cache-Control public;
        access_log off;
    }


    location ~ ^/(api|old-uri-path1|old-uri-path2)(.*)$ {
        # 指定舊網址,變更網站路徑為 Laravel 4
        root "/home/deploy/laravel4/current/public";

        # Rewrite $uri=/api/v1/xyz back to just $uri=/xyz
        rewrite ^(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ [^/]\.php(/|$) {
        set $newurl $request_uri;
        # 預設使用 php 7.1 fpm 執行

        fastcgi_pass unix:/run/php/php7.1-fpm.sock;

        if ($newurl ~ ^/(api|old-uri-path1|old-uri-path2)(.*)$) {
            # 變更網站路徑為 Laravel 4.2
            root "/home/deploy/laravel4/current/public";
            # 舊網址使用 php 7.0 fpm 執行
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $newurl;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }


    proxy_intercept_errors on;
    fastcgi_intercept_errors on;

    # gzip
    gzip on;
    gzip_min_length 1000;
    gzip_types  *;
    gzip_comp_level 3;
    gzip_proxied any;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
}

預設是執行 Laravel 5.5,而設定的例外路徑則是存取 Laravel 4.2

參考資料

results matching ""

    No results matching ""