Nginx
このページは更新が必要です。 更新を行い、完了したらこのテンプレートを削除してください。
Nginx [engine x] は、Igor Sysoevが開発したHTTPサーバおよびリバースプロキシサーバであり、メールプロキシサーバでもあります。nginxプロジェクトは、高い同時実行性、高いパフォーマンスおよび低いメモリ使用量を重視して始まりました。2条項BSDライクライセンスでライセンスされており、Linux、BSD系OS、Mac OS X、Solaris、AIX、HP-UX、その他の*nix系OSで動作します。また、Microsoft Windows向けの概念実証版の移植もあります。
以下は、Nginxの設定に関するコミュニティ提供のドキュメントです。修正や追記を歓迎します。
Nginxの設定
PHP-FPM
Nginxは通常、php-fpmを介してPHPと連携するように設定されます。この方法は高速かつ堅牢です。
PHP-FPMのプールのデフォルトの動作では通常、スクリプトの実行が特定の拡張子(つまり.php)に制限されます。使用しているパッケージ/ディストリビューションでこの動作が設定されていることを確認してください。例えばDebianの場合は次のとおりです:
/etc/php/8.2/fpm/pool.d/www.conf
security.limit_extensions = .php
Nginx
バージョン4.5以降、Moodleは「スラッシュ引数」とルーティングエンジンの両方を使用しています。
どちらも、ファイル名で正しく分割するようにサーバを正しく設定する必要があります。また、try_filesディレクティブを使用すると、適切に設定しない限りFastCGIの設定に干渉することが知られています。
正しく設定されていない場合、画像やCSSが表示されないなどの問題が発生します。
root /srv/moodle/public;)。これにより、ウェブからアクセス可能なファイルのみが配信されます。詳細については、再構成ガイドをご覧ください。FastCGIの設定
次の設定は、Moodle 4.5以降で動作することが確認されています。
location ~ \.php(/|$) {
# Split the path info based on URI.
fastcgi_split_path_info ^(.+\.php)(/.*)$;
# Note: Store the original path_info. It will be wiped out in a moment by try_files.
set $path_info $fastcgi_path_info;
# Look for the php file, trying a trailing slash for directories if required.
# Finally, send the request to the router - r.php - as a fallback.
try_files $fastcgi_script_name $fastcgi_script_name/ /r.php$is_args$args;
# File was found - pass to fastcgi.
fastcgi_pass 127.0.0.1:9000;
# Alternately, pass to unix socket (depends on pool listen configuration)
# fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
include fastcgi_params;
# Re-apply the path_info after including fastcgi_params.
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
ルーティングエンジン
Moodleサイトをルートの場所でホストしている場合は、次の設定を使用して、リクエストを正しいファイルにルーティングできます。
location / {
try_files $uri $uri/ /r.php$is_args$args;
}
Moodleサイトをサブディレクトリでホストしている場合は、locationブロックでサブディレクトリを指定する必要があります。
location /moodle/ {
try_files $uri $uri/ /moodle/r.php$is_args$args;
}
内部ファイルの非表示
# Hide all dot files but allow "Well-Known URIs" as per RFC 5785
location ~ /\.(?!well-known).* {
return 404;
}
# This should be after the php fpm rule and very close to the last nginx ruleset.
# Don't allow direct access to various internal files. See MDL-69333
location ~ (/vendor/|/node_modules/|composer\.json|/readme|/README|readme\.txt|/upgrade\.txt|/UPGRADING\.md|db/install\.xml|/fixtures/|/behat/|phpunit\.xml|\.lock|environment\.xml) {
deny all;
return 404;
}
XSendfile(別名X-Accel-Redirect)
XSendfile機能を使用するようにMoodleとNginxを設定すると、PHPがファイルの配信から解放され、Nginxが最も得意とすること、つまりファイルの配信を行えるようになるため、大きなメリットがあります。
Moodleのconfig.phpで、Nginx用のxsendfileを有効にします。これはconfig-dist.phpに記載されています。最小限の設定は次のようになります:
$CFG->xsendfile = 'X-Accel-Redirect';
$CFG->xsendfilealiases = array(
'/dataroot/' => $CFG->dataroot
);
これに合わせて、nginxのserver設定に対応する「location」ブロックを追加します。
location /dataroot/ {
internal;
alias <full_moodledata_path>; # ensure the path ends with /
}
ここでの「internal」の定義は、クライアントがdatarootにアクセスできないようにするため、極めて重要です。
ロードバランサのヒント(AWS)
インフラストラクチャの前段でAWSロードバランサを使用している場合は、上記の設定の一部を適用して、トラフィックがその先に進まないようにできます。以下は、ファイルを非表示にするために適用した設定です。既知の制限事項により、いくつかの考慮事項があります:
- Application Load Balancerあたり合計100ルール
- ルールあたり5つの条件値
- ルールあたり5つのワイルドカード
- ルールあたり5つの重み付きターゲットグループ:
[
{
"Conditions": [
{
"Field": "path-pattern",
"Values": [
"*/.*",
"*/upgrade.txt",
"*/db/install.xml",
"*/README.md"
],
"PathPatternConfig": {
"Values": [
"*/.*",
"*/upgrade.txt",
"*/db/install.xml",
"*/README.md"
]
}
}
],
"Actions": [
{
"Type": "fixed-response",
"Order": 1,
"FixedResponseConfig": {
"ContentType": "text/html",
"MessageBody": "<html>\n<head><title>404 Not Found</title></head>\n<body>\n<center><h1>404 Not Found</h1></center>\n<hr>\n</body>\n</html>",
"StatusCode": "404"
}
}
]
},
{
"Conditions": [
{
"Field": "path-pattern",
"Values": [
"*/composer.json",
"*/Gruntfile.js",
"*.lock",
"*/environtment.xml",
"*/readme.txt"
],
"PathPatternConfig": {
"Values": [
"*/composer.json",
"*/Gruntfile.js",
"*.lock",
"*/environtment.xml",
"*/readme.txt"
]
}
}
],
"Actions": [ #### Same as above
...
]
},
{
"Conditions": [
{
"Field": "path-pattern",
"Values": [
"*/fixtures/*",
"*/behat/*",
"*/phpunit.xml"
],
"PathPatternConfig": {
"Values": [
"*/fixtures/*",
"*/behat/*",
"*/phpunit.xml"
]
}
}
],
"Actions": [ #### Same as above
...
]
}
]
関連項目
- 実際のPATH_INFOのサポート:
- [非推奨] HTTP GETのfileパラメータへの内部リライト: