Home
Packages
Forums
Wiki
GitLab
Security
AUR
Download
コンテンツにスキップ
メインメニュー
メインメニュー
サイドバーに移動
非表示
案内
メインページ
目次
コミュニティに貢献
最近の出来事
おまかせ表示
特別ページ
交流
ヘルプ
貢献
最近の更新
最近の議論
新しいページ
統計
リクエスト
ArchWiki
検索
検索
表示
アカウント作成
ログイン
個人用ツール
アカウント作成
ログイン
Nginxのソースを表示
ページ
議論
日本語
閲覧
ソースを閲覧
履歴を表示
ツール
ツール
サイドバーに移動
非表示
操作
閲覧
ソースを閲覧
履歴を表示
全般
リンク元
関連ページの更新状況
ページ情報
表示
サイドバーに移動
非表示
←
Nginx
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
{{Lowercase title}} [[Category:ウェブサーバー]] [[de:Nginx]] [[en:Nginx]] [[ru:Nginx]] [[zh-hans:Nginx]] [[Wikipedia:ja:nginx|nginx]] ("エンジンエックス"と発音します)は2005年から Igor Sysoev(ロシア)によって開発されている、フリーでオープンソースかつハイパフォーマンスな HTTP サーバーかつリバースプロクシで、IMAP/POP3 プロクシサーバーとしても機能します。Nginx は主に、その安定性・多機能・単純な設定・低リソース消費によって知られています。 この記事ではnginxの設定方法と、[[#FastCGI]]を使って[[PHP]]と統合する方法を説明します。 == インストール == [[公式リポジトリ]]から {{Pkg|nginx-mainline}} (メインラインブランチ: 最新の機能・アップデート・修正) か {{Pkg|nginx}} (安定ブランチ: 重要な修正のみを適応) のどちらかを[[Pacman|インストール]]できます。 {{Note| 基本的には[https://www.nginx.com/blog/nginx-1-6-1-7-released/ 最新版の利用が推奨されています]が、 * サードパーティ製のモジュールとの互換性 * 新規に追加された機能の未知のバグ などによる影響が懸念される場合は、安定版を使用することを検討してください。 }} ''Ruby on Rails'' のためにインストールするには、[[Ruby on Rails#Rails パーフェクトセットアップ]]を参照してください。 セキュリティを向上させるために chroot によるインストールをするには、[[#chroot でインストール]] を参照してください。 == サービスの開始 == Nginx サービスを走らせるには: # systemctl start nginx スタートアップ時に Nginx サービスを起動するには: # systemctl enable nginx http://127.0.0.1 にデフォルトで表示されるページは: /usr/share/nginx/html/index.html == 設定 == nginx における最初の手順は [http://nginx.org/en/docs/beginners_guide.html 公式のビギナーズガイド] で説明されています。{{ic|/etc/nginx/}} にあるファイルを編集することで Nginx の設定ができます。メインの設定ファイルは {{ic|/etc/nginx/nginx.conf}} です。 より詳しい解説は、公式の [http://nginx.org/en/docs/ ドキュメント] にある [http://wiki.nginx.org/Configuration Nginx Configuration Examples] にあります。 以下の例は、最も一般的な使用例をカバーしています。ここでは、ドキュメントのデフォルトの場所 ({{ic|/usr/share/nginx/html}}) を使用することを想定しています。そうでない場合は、代わりにあなたのパスを指定してください。 {{Tip|DigitalOcean から [https://nginxconfig.io/ Nginx configuration tool] が提供されています。}} === Configuration example === {{hc|/etc/nginx/nginx.conf|<nowiki> user http; worker_processes auto; worker_cpu_affinity auto; events { multi_accept on; worker_connections 1024; } http { charset utf-8; sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; log_not_found off; types_hash_max_size 4096; client_max_body_size 16M; # MIME include mime.types; default_type application/octet-stream; # logging access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log warn; # load configs include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } </nowiki>}} === 一般設定 === {{ic|worker_processes}} の適当な値を探って下さい。この設定は最終的に nginx が受け入れる接続の数と利用できるプロセスの数を定義します。通常、システムのハードウェアのスレッド数にすると良いでしょう。また、バージョン 1.3.8 と 1.2.5 から {{ic|worker_processes}} には {{ic|auto}} と設定することができ、最適な値を自動的に検出します ([http://nginx.org/en/docs/ngx_core_module.html#worker_processes ソース])。 nginx が受け入れる最大接続数は {{ic|1=max_clients = worker_processes * worker_connections}} で計算できます。 ==== サーバーブロック ==== {{ic|server}} ブロックを使うことで複数のドメインを利用することができます。"バーチャルホスト"とも呼ばれますが、これは [[Apache]] の用語です。{{ic|server}} ブロックの利用方法は [http://wiki.nginx.org/ServerBlockExample Apache] とは異なっています。 以下の例では2つのドメインの接続をサーバーが待機します: {{ic|domainname1.dom}} と {{ic|domainname2.dom}}: {{hc|/etc/nginx/nginx.conf|<nowiki> ... server { listen 80; server_name domainname1.dom; root /usr/share/nginx/domainname1.dom/html; location / { index index.php index.html index.htm; } } server { listen 80; server_name domainname2.dom; root /usr/share/nginx/domainname2.dom/html; ... } ... }</nowiki>}} {{ic|nginx.service}} を[[再起動]]して変更を適用します。 クライアントからの接続時にこれらのドメイン名を解決するには、[[BIND]] や [[dnsmasq]] などのDNSサーバーを設定する必要があります。 ローカルマシンでバーチャルホストをテストするには、{{ic|/etc/hosts}} ファイルにバーチャルネームを追加してください: 127.0.0.1 domainname1.dom 127.0.0.1 domainname2.dom ===== サーバーエントリの管理 ===== 異なる {{ic|server}} ブロックを異なるファイルに配置することができます。これにより、特定のサイトを簡単に有効または無効にできます。 次のディレクトリを作成します。 # mkdir /etc/nginx/sites-available # mkdir /etc/nginx/sites-enabled {{ic|sites-available}} ディレクトリに、一つ以上のサーバーブロックを含むファイルを作成します。 {{hc|/etc/nginx/sites-available/example.conf|<nowiki> server { listen 443 ssl http2; listen [::]:443 ssl http2; ... } </nowiki>}} {{ic|http}} ブロックの最後に、{{ic|include sites-enabled/*;}} を追加します。 {{hc|/etc/nginx/nginx.conf|<nowiki> http { ... include sites-enabled/*; } </nowiki>}} サイトを有効にするには、シンボリックリンクを作成するだけです。 # ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf サイトを無効にするには、アクティブなシンボリックリンクのリンクを解除します。 # unlink /etc/nginx/sites-enabled/example.conf {{ic|nginx.service}} を [[リロード]]/[[リスタート]] して、サイト設定の変更を有効にします。 ==== TLS/SSL ==== SSL を使用するには、{{pkg|openssl}} をインストールする必要があります。 {{Tip| * SSL を設定する前に [http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate ngx_http_ssl_module] のドキュメントを読むことを推奨します。 * [[Let’s Encrypt]] は無料の、自動化されたオープンな証明局です。{{Pkg|letsencrypt-nginx}}{{Broken package link|置換パッケージ: {{Pkg|certbot-nginx}}}} をインストールすることで nginx プラグインを利用してコマンドラインから直接 SSL 証明書を作成して自動で設定することができます。}} 自己署名証明書を作成してください (キーのサイズや効力の日数は変更できます): # mkdir /etc/nginx/ssl # cd /etc/nginx/ssl # openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out cert.key # chmod 600 cert.key # openssl req -new -key cert.key -out cert.csr # openssl x509 -req -days 365 -in cert.csr -signkey cert.key -out cert.crt {{Note|その他の ''openssl'' オプションに関しては、 OpenSSL の [https://www.openssl.org/docs/apps/openssl.html man page] か [https://www.openssl.org/docs/ extensive documentation] を参照してください。}} {{Warning|If you plan on implementing SSL/TLS, know that some variations and implementations are [https://weakdh.org/#affected still] [[wikipedia:Transport_Layer_Security#Attacks_against_TLS.2FSSL|vulnerable to attack]]. For details on these current vulnerabilities within SSL/TLS and how to apply appropriate changes to nginx, visit http://disablessl3.com/ and https://weakdh.org/sysadmin.html}} TLS/SSL を用いる場合の {{ic|nginx.conf}} 設定サンプル: {{hc|/etc/nginx/nginx.conf|<nowiki> http { ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; ssl_session_tickets off; ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; # Google DNS Servers resolver_timeout 5s; } server { #listen 80; # Uncomment to also listen for HTTP requests listen 443 ssl; server_name localhost; ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; root /usr/share/nginx/html; location / { index index.html index.htm; } } </nowiki>}} {{Tip| * Mozilla は有用な [https://wiki.mozilla.org/Security/Server_Side_TLS SSL/TLS の記事] を用意しており、[https://wiki.mozilla.org/Security/Server_Side_TLS#Nginx nginx] の設定ガイドラインや、セキュアな設定を助ける [https://mozilla.github.io/server-side-tls/ssl-config-generator/ 自動化ツール] が載っています。 * [https://cipherli.st Cipherli.st] には大半のモダンなサーバーに対応する強力な SSL の実装例とチュートリアルが載っています。 }} {{ic|nginx.service}} を再起動して変更を適用します。 === FastCGI === FastCGI、または FCGI はウェブサーバーでインタラクティブなプログラムを動作させるためのプロトコルです。FastCGI は Common Gateway Interface (CGI) の変種で、ウェブサーバーと CGI プログラムのオーバーヘッドを減らすよう設計されていて、サーバーはより多くのウェブページのリクエストを一度に捌くことができます。 Nginx には FastCGI が組み込まれており多くの外部ツールが動きます、例えば Perl、[[PHP]]、[[Python]] など。これらを使うためにはまず FastCGI サーバーを動かす必要があります。 ==== PHP を動かす ==== PHP のために FastCGI サーバーを動かす方法は複数あります。ここでは推奨方法として php-fpm を使う方法を記載しています。 ===== 手順1: PHP の設定 ===== {{Pkg|php}} をインストールしてください。{{ic|/etc/php/php.ini}} の中にある {{Ic|open_basedir}} に PHP ファイルが含まれているベースディレクトリを ({{ic|/srv/http/}} や {{ic|/usr/share/webapps/}} のような感じで)指定しなくてはなりません: open_basedir = /usr/share/webapps/:/srv/http/:/home/:/tmp/:/usr/share/pear/ そうしたら必要なモジュールを設定しましょう。例えば sqlite3 を使うなら {{Pkg|php-sqlite}} をインストールして、{{ic|/etc/php/php.ini}} の次の行をアンコメントして有効にします: extension=sqlite3.so {{Note|chroot 環境で nginx を動かしている場合 (chroot は {{ic|/srv/nginx-jail}} で、ウェブページは {{ic|/srv/nginx-jail/www}}) です。{{ic|/etc/php/php-fpm.conf}} ファイルを編集して {{ic|chroot /srv/nginx-jail}} と {{ic|1=listen = /srv/nginx-jail/run/php-fpm/php-fpm.sock}} ディレクティブを pool セクション (デフォルトは {{ic|[www]}}) の中に記述する必要があります。ソケットファイルのディレクトリが存在しない場合は作成してください。}} ====== MariaDB ====== [[MariaDB]] で説明されているようにして MySQL/MariaDB を設定してください。 {{ic|/etc/php/php.ini}} の以下の行の [http://www.php.net/manual/en/mysqlinfo.api.choosing.php 少なくともどれか一つ] をアンコメント: extension=pdo_mysql.so extension=mysqli.so {{Warning|PHP 5.5 から、{{ic|mysql.so}} は [http://www.php.net/manual/de/migration55.deprecated.php 廃止] されログファイルにエラーが表示されます。}} ウェブスクリプトのために最小権限の MySQL ユーザーを追加することができます。また、{{ic|/etc/mysql/my.cnf}} を編集して {{ic|skip-networking}} 行をアンコメントすると MySQL サーバーはローカルホストからしかアクセスできなくなります。変更を適用するには MySQL を再起動する必要があります。 {{Tip|データベースを利用するために [[phpMyAdmin]], [[Adminer]], {{Pkg|mysql-workbench}} などのツールをインストールしても良いでしょう。}} ===== 手順2: php-fpm ===== * http://php-fpm.org {{Pkg|php-fpm}} をインストールします: # pacman -S php-fpm 設定ファイルは {{ic|/etc/php/php-fpm.conf}} です。 サービスを動かします: # systemctl start php-fpm {{Ic|php-fpm}} をスタートアップ時に有効にします: # systemctl enable php-fpm.service ===== 手順3: Nginx の設定 ===== それぞれの {{ic|server}} ブロックの中の {{ic|location}} ブロックに PHP アプリケーションを次のように記述します: location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } {{Ic|/etc/nginx/php.conf}} を作って設定をそこに書く場合、このファイルを {{Ic|server}} ブロックに入れて下さい。 server = { ... include php.conf; ... } .html や .htm ファイルを PHP として処理したい場合は、以下のようにしてください: location ~ \.(php'''|html|htm''')$ { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } 拡張子が .php でないファイルを php-fpm で動かすことを {{Ic|/etc/php/php-fpm.conf}} で有効にする必要があります: security.limit_extensions = .php .html .htm 設定を変えた後は php-fpm デーモンを再起動してください。 # systemctl restart php-fpm {{Note|{{Ic|fastcgi_pass}} 引数に'''注意を払って下さい'''、FastCGI サーバーの設定ファイルで定義された TCP や Unix ソケットでなくてはなりません。'''デフォルトの''' {{Ic|php-fpm}} (Unix) ソケットは fastcgi_pass unix:/run/php-fpm/php-fpm.sock; '''デフォルトの代わりに'''、通常の TCP ソケットを使うこともできます fastcgi_pass 127.0.0.1:9000; ただし Unix ドメインソケットの方が高速です。}} Nginx のための FastCGI 設定がある {{Ic|fastcgi.conf}} か {{Ic|fastcgi_params}} が含まれていますが、後者は使われなくなりました。これらのファイルは Nginx をインストールしたときに作られます。 最後に、Nginx が動作している場合は再起動してください: # systemctl restart nginx FastCGI をテストしたい場合は、{{ic|/usr/share/nginx/html/index.php}} を次の内容で作成して <?php phpinfo(); ?> ブラウザで http://127.0.0.1/index.php を開いて下さい。 以下は実際の設定例です。例の中では {{ic|root}} パスは {{ic|server}} の下に直接指定されており (デフォルト設定のように) {{ic|location}} の中には置いていません。 server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index index.html index.htm index.php; } location ~ \.php$ { #fastcgi_pass 127.0.0.1:9000; (depending on your php-fpm socket configuration) fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } } ==== CGI を動かす ==== この実装は CGI アプリケーションに必要です。 ===== 手順1: fcgiwrap ===== {{Pkg|fcgiwrap}} をインストールしてください。 設定ファイルは {{ic|/usr/lib/systemd/system/fcgiwrap.socket}} です。 [[systemd]] の ''fcgiwrap.socket'' を有効化・起動します。 この systemd ユニットファイルは現在 [https://bugs.archlinux.org/task/31696 ArchLinux のタスクページ]で議論されています。思い通りに動くか確認するためにユニットファイルを自分で検査すると良いでしょう。 ====== マルチワーカースレッド ====== 複数のワーカースレッドを生成したい場合は、{{AUR|multiwatch}} を使用するのが推奨されています。これはクラッシュした子スレッドの再起動の面倒をみてくれます。multiwatch は systemd が作成したソケットを管理できないため、unix ソケットを作成するには {{ic|spawn-fcgi}} を使う必要がありますが、ユニットファイルから直接実行した場合は fcgiwrap には問題が生じません。 {{ic|/usr/lib/systemd/system/fcgiwrap.service}} から {{ic|/etc/systemd/system/fcgiwrap.service}} にユニットファイルをコピーして (存在する場合は {{ic|fcgiwrap.socket}} ユニットも)、{{ic|ExecStart}} 行を必要に応じて修正してください。以下は {{AUR|multiwatch}} を使用するユニットファイルです。{{ic|fcgiwrap.socket}} が実行中だったり有効になっていないことを確認してください、このユニットと衝突してしまうからです: {{hc|/etc/systemd/system/fcgiwrap.service|2= [Unit] Description=Simple CGI Server After=nss-user-lookup.target [Service] ExecStartPre=/bin/rm -f /run/fcgiwrap.socket ExecStart=/usr/bin/spawn-fcgi -u http -g http -s /run/fcgiwrap.sock -n -- /usr/bin/multiwatch -f 10 -- /usr/sbin/fcgiwrap ExecStartPost=/usr/bin/chmod 660 /run/fcgiwrap.sock PrivateTmp=true Restart=on-failure [Install] WantedBy=multi-user.target }} {{ic|-f 10}} は生成する子スレッドの数に変更してください。 {{Warning|The ExecStartPost line is required because of strange behaviour I'm seeing when I use the {{ic|-M 660}} option for {{ic|spawn-fcgi}}. The wrong mode is set. This may be a bug?}} ===== 手順2: Nginx の設定 ===== CGI ウェブアプリケーションを置くそれぞれの {{Ic|server}} ブロックには以下のような {{Ic|location}} ブロックを記述します: location ~ \.cgi$ { root /path/to/server/cgi-bin; fastcgi_pass unix:/run/fcgiwrap.sock; include fastcgi.conf; } {{Ic|fcgiwrap}} の'''デフォルト''' (Unix) ソケットは ''/run/fcgiwrap.sock'' です。 == chroot でインストール == [[chroot]] に Nginx をインストールすることでセキュリティレイヤーを追加することができます。セキュリティを最大限に高めるために、Nginx サーバーを動かすのに必要なファイルだけを入れて、全てのファイルは出来る限り最小限の権限にします。例えば、なるべく root が所有するようにして、{{ic|/usr/bin}} などのディレクトリは読み書きできないようにするなど。 Arch はデフォルトでサーバーを実行するための {{ic|http}} ユーザーとグループが設定されています。chroot は {{ic|/srv/http}} で動作させます。 この牢獄を作るための perl スクリプトが [https://gist.github.com/4365696 jail.pl gist] にあります。このスクリプトを使用するか、またはこの記事の指示に従って作成するか選ぶことができます。スクリプトは root で実行してください。変更を適用する前に行をアンコメントする必要があります。 === 必要なデバイスを作成 === Nginx には {{ic|/dev/null}}, {{ic|/dev/random}}, {{ic|/dev/urandom}} が必要です。これらを chroot にインストールするために {{ic|/dev/}} フォルダを作成してデバイスを mknod で追加します。chroot が危険になったときでも、攻撃者が {{ic|/dev/sda1}} などの重要なデバイスにアクセスするには chroot から脱出しなくてはならないようにするために、{{ic|/dev/}} を全てマウントはしません。 {{Tip|mknod の引数については {{ic|man mknod}} や {{ic|<nowiki>ls -l /dev/{null,random,urandom}</nowiki>}} を見て下さい。}} # export JAIL=/srv/http # mkdir $JAIL/dev # mknod -m 0666 $JAIL/dev/null c 1 3 # mknod -m 0666 $JAIL/dev/random c 1 8 # mknod -m 0444 $JAIL/dev/urandom c 1 9 === 必要なフォルダを作成 === Nginx を適切に実行するためにはたくさんのファイルが必要になります。それらをコピーする前に、ファイルを保存するためのフォルダを作成してください。ここでは Nginx のドキュメントルートが {{ic|/srv/http/www}} であると仮定します。 # mkdir -p $JAIL/etc/nginx/logs # mkdir -p $JAIL/usr/{lib,bin} # mkdir -p $JAIL/usr/share/nginx # mkdir -p $JAIL/var/{log,lib}/nginx # mkdir -p $JAIL/www/cgi-bin # mkdir -p $JAIL/{run,tmp} # cd $JAIL; ln -s usr/lib lib {{Note|64ビットのカーネルを使っている場合 {{ic|usr/lib}} に {{ic|lib64}} と {{ic|usr/lib64}} のシンボリックリンクを作成する必要があります: {{ic|cd $JAIL; ln -s usr/lib lib64}} と {{ic|cd $JAIL/usr; ln -s lib lib64}}。}} そして {{ic|$JAIL/tmp}} と {{ic|$JAIL/run}} を tmpfs でマウントします。攻撃者が RAM を全て喰いつくせないようにサイズを制限すると良いでしょう。 # mount -t tmpfs none $JAIL/run -o 'noexec,size=1M' # mount -t tmpfs none $JAIL/tmp -o 'noexec,size=100M' 再起動してもマウントが維持されるように、以下のエントリを {{ic|/etc/fstab}} に追加します: {{hc|/etc/fstab|<nowiki> tmpfs /srv/http/run tmpfs rw,noexec,relatime,size=1024k 0 0 tmpfs /srv/http/tmp tmpfs rw,noexec,relatime,size=102400k 0 0 </nowiki>}} === chroot に移住 === まず平易なファイルをコピーします。 # cp -r /usr/share/nginx/* $JAIL/usr/share/nginx # cp -r /usr/share/nginx/html/* $JAIL/www # cp /usr/bin/nginx $JAIL/usr/bin/ # cp -r /var/lib/nginx $JAIL/var/lib/nginx そして必要なライブラリをコピーします。ldd を使ってライブラリを確認して適当な場所にコピーして下さい。ハードリンクよりはコピーが推奨されます。攻撃者が書き込み権限を得たときに本当のシステムファイルが破壊されたり改変される可能性があるためです。 {{hc|$ ldd /usr/bin/nginx|<nowiki> linux-vdso.so.1 (0x00007fffc41fe000) libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f57ec3e8000) libcrypt.so.1 => /usr/lib/libcrypt.so.1 (0x00007f57ec1b1000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f57ebead000) libm.so.6 => /usr/lib/libm.so.6 (0x00007f57ebbaf000) libpcre.so.1 => /usr/lib/libpcre.so.1 (0x00007f57eb94c000) libssl.so.1.0.0 => /usr/lib/libssl.so.1.0.0 (0x00007f57eb6e0000) libcrypto.so.1.0.0 => /usr/lib/libcrypto.so.1.0.0 (0x00007f57eb2d6000) libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f57eb0d2000) libz.so.1 => /usr/lib/libz.so.1 (0x00007f57eaebc000) libGeoIP.so.1 => /usr/lib/libGeoIP.so.1 (0x00007f57eac8d000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f57eaa77000) libc.so.6 => /usr/lib/libc.so.6 (0x00007f57ea6ca000) /lib64/ld-linux-x86-64.so.2 (0x00007f57ec604000)</nowiki>}} # cp /lib64/ld-linux-x86-64.so.2 $JAIL/lib {{ic|/usr/lib}} の中にあるファイルについては次のワンライナーが使えます: {{bc|<nowiki># cp $(ldd /usr/bin/nginx | grep /usr/lib | sed -sre 's/(.+)(\/usr\/lib\/\S+).+/\2/g') $JAIL/usr/lib</nowiki>}} {{Note|{{ic|linux-vdso.so}} をコピーしないでください – 実際のライブラリではなく {{ic|/usr/lib}} には存在しません。64ビット環境の場合 {{ic|ld-linux-x86-64.so}} も {{ic|/lib64}} にあるように表示されます。}} 必要なライブラリとシステムファイル以外のファイルをコピーします。 {{bc|# cp /usr/lib/libnss_* $JAIL/usr/lib # cp -rfvL /etc/{services,localtime,nsswitch.conf,nscd.conf,protocols,hosts,ld.so.cache,ld.so.conf,resolv.conf,host.conf,nginx} $JAIL/etc}} chroot のために制限を付けたユーザー・グループファイルを作成します。これにより chroot にアクセスするのに必要なシステムユーザー・グループが攻撃者に漏洩しなくなります。 {{hc|$JAIL/etc/group| http:x:33: nobody:x:99: }} {{hc|$JAIL/etc/passwd| http:x:33:33:http:/:/bin/false nobody:x:99:99:nobody:/:/bin/false }} {{hc|$JAIL/etc/shadow| http:x:14871:::::: nobody:x:14871:::::: }} {{hc|$JAIL/etc/gshadow| http::: nobody::: }} # touch $JAIL/etc/shells # touch $JAIL/run/nginx.pid 最後にパーミッションをできるだけ制限的に設定してください。できるかぎり所有者を root にして書き込み不可にします。 # chown -R root:root $JAIL/ # chown -R http:http $JAIL/www # chown -R http:http $JAIL/etc/nginx # chown -R http:http $JAIL/var/{log,lib}/nginx # chown http:http $JAIL/run/nginx.pid # find $JAIL/ -gid 0 -uid 0 -type d -print | xargs sudo chmod -rw # find $JAIL/ -gid 0 -uid 0 -type d -print | xargs sudo chmod +x # find $JAIL/etc -gid 0 -uid 0 -type f -print | xargs sudo chmod -x # find $JAIL/usr/bin -type f -print | xargs sudo chmod ug+rx # find $JAIL/ -group http -user http -print | xargs sudo chmod o-rwx # chmod +rw $JAIL/tmp # chmod +rw $JAIL/run サーバーがポート 80 (あるいは 0-1024 のどれかのポート) で待機する場合、chroot した実行ファイルに root 権限がなくてもポートを使えるように権限を与えてください: # setcap 'cap_net_bind_service=+ep' $JAIL/usr/bin/nginx === nginx.service を変更して chroot を起動 === {{ic|nginx.service}} ユニットファイルを編集する前に、ユニットファイルを {{ic|/etc/systemd/system/}} にコピーして {{ic|/usr/lib/systemd/system/}} のファイルよりも優先されるようにすると良いでしょう。nginx をアップグレードしたときにカスタムした ''.service'' ファイルが変更されなくなります。 # cp /usr/lib/systemd/system/nginx.service /etc/systemd/system/nginx.service chroot で nginx を起動するために systemd ユニットを変更して、pid ファイルを chroot に保存します。 {{Note|I'm not sure if the pid file needs to be stored in the chroot jail.}} {{hc|/etc/systemd/system/nginx.service|<nowiki> [Unit] Description=A high performance web server and a reverse proxy server After=syslog.target network.target [Service] Type=forking PIDFile=/srv/http/run/nginx.pid ExecStartPre=/usr/bin/chroot --userspec=http:http /srv/http /usr/bin/nginx -t -q -g 'pid /run/nginx.pid; daemon on; master_process on;' ExecStart=/usr/bin/chroot --userspec=http:http /srv/http /usr/bin/nginx -g 'pid /run/nginx.pid; daemon on; master_process on;' ExecReload=/usr/bin/chroot --userspec=http:http /srv/http /usr/bin/nginx -g 'pid /run/nginx.pid; daemon on; master_process on;' -s reload ExecStop=/usr/bin/chroot --userspec=http:http /srv/http /usr/bin/nginx -g 'pid /run/nginx.pid;' -s quit [Install] WantedBy=multi-user.target</nowiki>}} {{Note|Upgrading nginx with pacman will not upgrade the chrooted nginx installation. You have to take care of the updates manually by repeating some of the steps above. Do not forget to also update the libraries it links against.}} chroot されていない nginx 環境は取り除いてもかまいません: # pacman -Rsc nginx chroot されていない nginx 環境を削除しない場合、実行中の nginx プロセスが chroot されているプロセスかどうか確認したくなるときがあるかもしれません。{{ic|/proc/''PID''/root}} シンボリックリンクを確認することでチェックできます。 # ps -C nginx | awk '{print $1}' | sed 1d | while read -r PID; do ls -l /proc/$PID/root; done == ヒントとテクニック == === [[systemd]] を使って非特権で動作させる === [[systemd#ユニットファイルの編集|nginx.service を編集]]して {{ic|[Service]}} の下の {{ic|User}} と任意で {{ic|Group}} オプションを設定してください: {{hc|/etc/systemd/system/nginx.service.d/user.conf|2= [Service] User=''user'' Group=''group'' }} 権限昇格に対してサービスを防御することができます: {{hc|/etc/systemd/system/nginx.service.d/user.conf|2= [Service] ... NoNewPrivileges=yes }} {{Tip|1=制限オプションについて詳しくは [http://www.freedesktop.org/software/systemd/man/systemd.exec.html#User= systemd.exec(5)] を見てください。}} それから {{ic|''user''}} が必要なファイルやポートにアクセスできるようにしてください: <dl> <dt>ポート</dt> <dd> Linux はデフォルトでは {{ic|root}} 以外が 1024 以下のポートを使うことができないようになっています。1024 よりも上のポートは使うことができます: {{hc|/etc/nginx/nginx.conf| server { listen 8080; } }} {{Tip|1=nginx にポート 80 あるいは 443 からアクセスできるようにしたい場合、[[ファイアウォール]]を設定して 80 あるいは 443 から nginx が listen しているポートに転送してください。}} </dd> <dd> また、nginx プロセスに CAP_NET_BIND_SERVICE ケイパビリティを付与することで 1024 以下のポートにバインドできるようになります: {{hc|/etc/systemd/system/nginx.service.d/user.conf|2= [Service] ... CapabilityBoundingSet= CapabilityBoundingSet=CAP_NET_BIND_SERVICE AmbientCapabilities= AmbientCapabilities=CAP_NET_BIND_SERVICE }} </dd> <dt>PID ファイル</dt> <dd> {{Pkg|nginx}} はデフォルトでは {{ic|/run/nginx.pid}} を使います。''user'' が書き込み可能なディレクトリを作成して PID ファイルをそこに配置します。[[systemd#一時ファイル|systemd-tmpfiles]] を使用する例: {{hc|/etc/tmpfiles.d/nginx.conf|2= d /run/nginx 0775 root ''group'' - - }} 設定を実行: # systemd-tmpfiles --create 元の {{ic|nginx.service}} に合わせて PID の値を[[編集]]: {{hc|/etc/systemd/system/nginx.service.d/user.conf|2= [Service] ... PIDFile=/run/nginx/nginx.pid ExecStart= ExecStart=/usr/bin/nginx -g 'pid /run/nginx/nginx.pid; error_log stderr;' ExecReload= ExecReload=/usr/bin/nginx -s reload -g 'pid /run/nginx/nginx.pid;' }} </dd> <dt>{{ic|/var/lib/nginx/*}}</dt> <dd> {{ic|/var/lib/nginx}} の一部のディレクトリは {{ic|root}} として nginx でブートストラップする必要があります。サーバー全体を起動する必要はありません。nginx は[[#設定の確認|設定テスト]]でブートストラップを実行します。一回実行すればそれで十分です。 </dd> <dt>ログファイル & ディレクトリのパーミッション</dt> <dd> 設定テストの実行時に {{ic|root}} によって所有される壊れたログが作成されます。{{ic|/var/log/nginx}} のログを削除して新しく起動してください。 <dd> nginx サービスのユーザーから {{ic|/var/log/nginx}} に書き込む権限が必要です。[[ファイルのパーミッションと属性#パーミッションを変更|パーミッションを変更]]するかディレクトリの所有者を変更してください。 </dl> 以上で設定は完了です。nginx を[[起動]]してください。 {{Tip|[[#FastCGI|FastCGI サーバー]]でも同じ構成が使えます。}} === systemd の別のスクリプト === On pure systemd you can get advantages of chroot + systemd. [http://0pointer.de/blog/projects/changing-roots.html] Based on set [http://wiki.nginx.org/CoreModule#user user group] an pid on: {{hc|/etc/nginx/nginx.conf|2= user http; pid /run/nginx.pid; }} the absolute path of file is {{ic|/srv/http/etc/nginx/nginx.conf}}. {{hc|/etc/systemd/system/nginx.service|2= [Unit] Description=nginx (Chroot) After=syslog.target network.target [Service] Type=forking PIDFile=/srv/http/run/nginx.pid RootDirectory=/srv/http ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/usr/sbin/nginx -c /etc/nginx/nginx.conf -s reload ExecStop=/usr/sbin/nginx -c /etc/nginx/nginx.conf -s stop [Install] WantedBy=multi-user.target }} It is not necesary to set the default location, nginx loads at default {{ic| -c /etc/nginx/nginx.conf}}, but it is a good idea though. Alternatively you can run '''only''' {{ic|ExecStart}} as chroot with parameter {{ic|RootDirectoryStartOnly}} set as {{ic|yes}} [http://www.freedesktop.org/software/systemd/man/systemd.service.html man systemd service] or start it before mount point as effective or a [http://www.freedesktop.org/software/systemd/man/systemd.path.html systemd path] is available. {{hc|/etc/systemd/system/nginx.path|2= [Unit] Description=nginx (Chroot) path [Path] PathExists=/srv/http/site/Public_html [Install] WantedBy=default.target }} 作成した {{ic|nginx.path}} を[[有効化]]して、{{ic|/etc/systemd/system/nginx.service}} の {{ic|1=WantedBy=default.target}} を {{ic|1=WantedBy=nginx.path}} に変更してください。 The {{ic|PIDFile}} in unit file allows systemd to monitor process (absolute path required). If it is undesired, you can change to default one-shot type, and delete the reference from the unit file. === Nginx Beautifier === {{AUR|nginxbeautifier}} は nginx の設定ファイルを整形することができるコマンドラインツールです。 === より良いヘッダー管理 === Nginx のヘッダー管理システムはあまり直感的とは言えません。ヘッダーは特定のコンテキストでしか定義できず、他のヘッダーは無視されます。[https://github.com/openresty/headers-more-nginx-module#more_set_headers headers-more-nginx] モジュールをインストールすることでこの挙動を変更できます。 {{Pkg|nginx-mod-headers-more}} パッケージを[[インストール]]してください。モジュールは {{ic|/usr/lib/nginx/modules}} ディレクトリにインストールされます。 モジュールをロードするには以下をメインの nginx の設定ファイルに追加してください: {{hc|/etc/nginx/nginx.conf|2= load_module "/usr/lib/nginx/modules/ngx_http_headers_more_filter_module.so"; ... }} == トラブルシューティング == === 設定の確認 === # nginx -t 結果: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful === ローカル IP へのアクセスが localhost にリダイレクトされる === Arch Linux [https://bbs.archlinux.org/viewtopic.php?pid=780561#p780561 フォーラム]からの解決法があります。 {{ic|/etc/nginx/nginx.conf}} を開き、"server_name localhost" の前の # を取り除き、以下を付け加えます: server_name_in_redirect off; デフォルトでは、nginx は設定にある server_name へのリクエストをすべてリダイレクトします。 === Error: 404 (Pathinfo error) === いくつかのフレームワーク (thinkphp、cakephp など)や CMS では pathinfo が必要です。 1. {{ic|/etc/php/php.ini}} ファイルを開き、次のように付け加えます cgi.fix_pathinfo=1 2. {{ic|/etc/nginx/nginx.conf}} を開き、次の部分をコメントアウトします location ~ \.php$ { ... } を #location ~ \.php$ { #... #} そして以下を加えます location ~ ^(.+\.php)(.*)$ { root /srv/http/nginx; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; #fastcgi_pass 127.0.0.1:9000; #Un-comment this and comment "fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;" if you are not using php-fpm. fastcgi_index index.php; set $document_root2 $document_root; if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; } fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root2$fastcgi_path_info; include fastcgi_params; fastcgi_param DOCUMENT_ROOT $document_root2; } try_files を使う方法もあります: server { .. root /usr/share/nginx/html; index index.php; location / { try_files $uri $uri/ /index.php?$args; } } === Error: The page you are looking for is temporarily unavailable. Please try again later. === FastCGI サーバーが動作していないか、ソケットが間違ったパーミッションに設定されています。 === Error: No input file specified === おそらくスクリプトのフルパスを含んだ SCRIPT_FILENAME がありません。 nginx の設定 (fastcgi_param SCRIPT_FILENAME) が完全でも、このエラーは php が requestd スクリプトをロードできないことを意味しています。これは単純にパーミッションの問題であることが普通で、root で php-cgi を実行するか # spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi php-cgi を起動するグループとユーザーを作成する必要があります。例えば: # groupadd www # useradd -g www www # chmod +w /srv/www/nginx/html # chown -R www:www /srv/www/nginx/html # spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/bin/php-cgi 他の可能性としては、{{ic|nginx.conf}} 内の "location ~ \.php$" セクションの "root" 引数が間違っていることがありえます。"root" が同じサーバーの "location /" と同じディレクトリを示しているか確認してください。もしくは root をグローバルに設定するには、location セクションで定義しないでください。 Also keep in mind that your php script path was defined as {{ic|/srv/http}} by default using the variable "open_basedir" in {{ic|/etc/php/php.ini}}; you can change them if you need. Also notice that not only php script should have read permission, but also the entire directory structure should have execute permission so that PHP user can traverse the path. === Error: FastCGI で空のページが表示される === access.log にエラーが表示されず、コード200が返されるのに以下のような空のページが表示される場合: <html> <head></head> <body></body> </html> [http://beutelevision.com/blog2/2013/08/26/nginx-with-php-fpm-generating-blank-page/ Thomas Beutel のブログ] に書かれた解決方法: {{ic|/etc/nginx/nginx.conf}} を編集して php の {{ic|location}} セクションに {{ic|fastcgi_param}} という行を追加: location ~ \.php$ { ... include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ... } php_fpm では PHP ファイルのパスを認識するために以上のように設定する必要があります。 == 参照 == * [http://nginx.org/ Nginx 公式サイト] * [http://calomel.org/nginx.html Nginx HowTo] * [http://wiki.gotux.net/config:nginx Easy Config Files] * [http://www.tecmint.com/install-nginx-php-mysql-with-mariadb-engine-and-phpmyadmin-in-arch-linux/ Installing LEMP (Nginx, PHP, MySQL with MariaDB engine and PhpMyAdmin) in Arch Linux]
このページで使用されているテンプレート:
テンプレート:AUR
(
ソースを閲覧
)
テンプレート:Bc
(
ソースを閲覧
)
テンプレート:Hc
(
ソースを閲覧
)
テンプレート:Ic
(
ソースを閲覧
)
テンプレート:Lowercase title
(
ソースを閲覧
)
テンプレート:Man
(
ソースを閲覧
)
テンプレート:Note
(
ソースを閲覧
)
テンプレート:Pkg
(
ソースを閲覧
)
テンプレート:Tip
(
ソースを閲覧
)
テンプレート:Warning
(
ソースを閲覧
)
Nginx
に戻る。
検索
検索
Nginxのソースを表示
話題を追加