nginx

nginxは高速な、製品レベルのHTTPサーバです。本番環境への展開(Deployment to Production)のリストにあるWSGIサーバのひとつを使って自分のアプリケーションを提供するとき、その前面に専用のHTTPサーバを置くことは、しばしば良いこともしくは必要なことになります。この「リバースプロキシ」は受信リクエスト、TLS、その他のセキュリティおよび性能に関することをWSGIサーバよりもうまく処理できます。 `nginx`_ is a fast, production level HTTP server. When serving your application with one of the WSGI servers listed in :doc:`index`, it is often good or necessary to put a dedicated HTTP server in front of it. This "reverse proxy" can handle incoming requests, TLS, and other security and performance concerns better than the WSGI server.

Nignxはシステムのパッケージ管理ツール(system package manager)もしくはWindowsではビルド済みの実行ファイルを使ってインストールできます。Nginxのインストールおよび実行自体はこのドキュメントの範囲外です。このページは自分のアプリケーションのプロキシになるようhttpdを設定する基本について説明します。Nginxのドキュメントを呼んでどの目玉機能(feature)が利用可能か理解しておいてください。 Nginx can be installed using your system package manager, or a pre-built executable for Windows. Installing and running Nginx itself is outside the scope of this doc. This page outlines the basics of configuring Nginx to proxy your application. Be sure to read its documentation to understand what features are available.

ドメイン名 Domain Name

ドメイン名の取得と設定は、このドキュメントの範囲外です。大概は、registrar(訳注: ドメイン名の登録・管理をしている団体のようなもの)からドメイン名を購入し、ホスティング事業者(hosting provider)のサーバ領域に支払い、そしてそれからホスティング事業者のDNSサーバが自分のregistrarを指すようにします。 Acquiring and configuring a domain name is outside the scope of this doc. In general, you will buy a domain name from a registrar, pay for server space with a hosting provider, and then point your registrar at the hosting provider's name servers.

これを模擬するために、Linuxでは/etc/hostsにある、自分のhostsファイル編集することもできます。ドメイン名をローカルのIPに関連付ける行を追加してください。 To simulate this, you can also edit your ``hosts`` file, located at ``/etc/hosts`` on Linux. Add a line that associates a name with the local IP.

最近のLinuxシステムでは、以下の例のように.localhostで終わるドメイン名はすべて、hostsファイルにその行を追加することなく扱われるように設定されているかもしれません。 Modern Linux systems may be configured to treat any domain name that ends with ``.localhost`` like this without adding it to the ``hosts`` file.

/etc/hosts
127.0.0.1 hello.localhost

設定(Configuration) Configuration

nginxの設定はLinuxでは/etc/nginx/nginx.confにあります。オペレーティングシステムによっては違う場所かもしれません。ドキュメントを調べてnginx.confを探してください。 The nginx configuration is located at ``/etc/nginx/nginx.conf`` on Linux. It may be different depending on your operating system. Check the docs and look for ``nginx.conf``.

存在するすべてのserverセクションを削除するかコメントアウトします。serverセクションを追加し、proxy_passディレクティブを使ってWSGIサーバがリクエストを受け付けているアドレスを指すようにします。ここでは、WSGIサーバはローカルのhttp://127.0.0.1:8000でリクエストを受け付けていると想定しています。 Remove or comment out any existing ``server`` section. Add a ``server`` section and use the ``proxy_pass`` directive to point to the address the WSGI server is listening on. We'll assume the WSGI server is listening locally at ``http://127.0.0.1:8000``.

/etc/nginx.conf
server {
    listen 80;
    server_name _;

    location / {
        proxy_pass http://127.0.0.1:8000/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Prefix /;
    }
}

それから、自分のアプリケーションがこれらのヘッダーを使うように、Proxyの背後にいることのFlaskへの指示のとおりにします。 Then :doc:`proxy_fix` so that your application uses these headers.