インストール/環境構築/Nginxの設定

インストール

Nginxの設定でMagic3で必須となる項目は特にありません。以下、奨励する設定項目です。

エラー画面の表示(①)

fastcgi_intercept_errorsの値はonにしてMagic3のエラー画面が表示できるようにします。
エラーコード503の場合はファイルを割り当てないでMagic3側で画面を表示させます。その他のエラーの場合は独自に作成した固定のHTMLファイルを表示させます。

非公開ディレクトリのアクセス制限(②)

includeディレクトリはシステムのみアクセス可能な内部ファイルが格納されています。外部からの直接のアクセスを禁止します。
バックアップファイルやテキストファイルを拡張子で直接アクセス防止します。

クローラーのアクセス制限(③)

不要なクローラーはアクセスを拒否します。

設定例)

server {
    listen      80;
    server_name example.com;

    #
    # Directory configuration
    #
    root  /var/www/html;
    index index.php index.html;

    #
    # Access log configuration
    #
    access_log /var/log/nginx/access.log  main;
    error_log  /var/log/nginx/error.log warn;

    #
    # redirect server error pages
    #  ⇒①
    error_page 401 /unauthorized.html;
    location = /unauthorized.html {
        root   /usr/share/nginx/html/error;
    }
    error_page 403 /forbidden.html;
    location = /forbidden.html {
        root   /usr/share/nginx/html/error;
    }
    error_page 404 /notfound.html;
    location = /notfound.html {
        root   /usr/share/nginx/html/error;
    }
 #    error_page   503 /serviceunavailable.html;  # show Magic3 page
    location = /serviceunavailable.html {
        root   /usr/share/nginx/html/error;
    }
    error_page   500 502 504  /internalservererror.html;
    location = /internalservererror.html {
        root   /usr/share/nginx/html/error;
    }

    #
    # reject crawler
    #  ⇒③ 
    if ($http_user_agent ~* (Hatena|SemrushBot|AhrefsBot|Linguee|proximic|BLEXBot|GrapeshotCrawler|Mappy|MJ12bot|MegaIndex|bidswitchbot|SMTBot|ltx71|integralads|jet-bot|trendictionbot)){
        return 403;
    }

    #
    # file access limit for Magic3
    #  ⇒②
    location ~ /include/(.*)\.php$ {
        deny all;
    }
    location ~* \.(txt|log|dat|bak|md|zip)$ {
        deny all;
    }

    #
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;    # ⇒①
        include fastcgi_params;
    }

    location = /favicon.icon {
        access_log    off;
        log_not_found off;
    }
    location = /robots.txt {
        allow all;
        access_log    off;
        log_not_found off;
    }
}

2021-06-18 (金) 17:19:02 (1040d)