Nginx: 初心者ガイド
このガイドでは、nginx の基本的な概要を説明し、nginx を使用して実行できるいくつかの簡単なタスクについて説明します。 nginx は読者のマシンにすでにインストールされているものとします。そうでない場合は、「インストール」を参照してください。 nginx ページ。このガイドでは、nginx の起動と停止、構成のリロード方法、構成ファイルの構造の説明、静的コンテンツを提供するための nginx のセットアップ方法、プロキシ サーバーとして nginx を構成する方法、および FastCGI アプリケーションに接続します。
nginx には 1 つのマスター プロセスと複数のワーカー プロセスがあります。マスター プロセスの主な目的は、構成を読み取って評価し、ワーカー プロセスを維持することです。ワーカー プロセスはリクエストの実際の処理を行います。 nginx はイベントベースのモデルと OS に依存するモデルを採用しています ワーカー プロセス間でリクエストを効率的に分散するメカニズム。ワーカー プロセスの数は構成ファイルで定義され、特定の構成に対して固定されるか、使用可能な CPU コアの数に自動的に調整されます (worker_processes を参照)。
nginx とそのモジュールの動作方法は構成ファイルで決定されます。デフォルトでは、構成ファイルの名前は nginx.conf
で、ディレクトリ /usr/local/nginx/conf
、/etc/nginx
に配置されます。 、または /usr/local/etc/nginx
。nginx.conf
and placed in the directory /usr/local/nginx/conf
, /etc/nginx
, or /usr/local/etc/nginx
.
Starting, Stopping, and Reloading Configuration
To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s
parameter. Use the following syntax:
nginx -s <em>signal</em>ログイン後にコピー
Where signal may be one of the following:
-
stop
— fast shutdown -
quit
— graceful shutdown -
reload
— reloading the configuration file reopen
nginx を起動するには、実行可能ファイルを実行します。 nginx が起動したら、-s
パラメータを指定して実行可能ファイルを呼び出すことで制御できます。次の構文を使用します:
nginx -s quitログイン後にコピー
ここで、シグナルは次のいずれかになります:
-
stop
- 高速シャットダウンquit
- 正常なシャットダウン -
reload
- 設定ファイルの再読み込み -
reopen
- ログ ファイルを再度開きますkill
たとえば、ワーカープロセスが現在のリクエストの処理を完了するのを待って nginx プロセスを停止するには、次のコマンドを実行できます:nginx.pid
in the directory/usr/local/nginx/logs
or/var/run
. For example, if the master process ID is 1628, to send the QUIT signal resulting in nginx’s graceful shutdown, execute:nginx -s reload
ログイン後にコピーログイン後にコピーFor getting the list of all running nginx processes, the
ps
このコマンドは、nginx を開始したのと同じユーザーの下で実行する必要があります。設定ファイルに加えられた変更は無効になります。構成を再ロードするコマンドが nginx に送信されるか、nginx が再起動されるまで適用されます。設定をリロードするには、次のコマンドを実行します:kill -s QUIT 1628
ログイン後にコピー マスタープロセスが設定をリロードする信号を受信すると、新しい設定ファイルの構文の有効性をチェックし、そこに提供されている設定を適用しようとします。これが成功すると、マスター プロセスは新しいワーカー プロセスを開始し、 古いワーカー プロセスにメッセージを送り、シャットダウンを要求します。それ以外の場合、マスター プロセスは変更をロールバックし、古い構成で動作し続けます。古いワーカー プロセスはシャットダウン コマンドを受け取り、新しい接続の受け入れを停止し、 すべてのリクエストが処理されるまで、現在のリクエストの処理を続けます。その後、古いワーカー プロセスは終了します。ps -ax | grep nginx
ログイン後にコピー
ユーティリティなどの Unix ツールを使用して、シグナルが nginx プロセスに送信されることもあります。この場合、シグナルは指定されたプロセス ID を持つプロセスに直接送信されます。 nginx マスター プロセスのプロセス ID は、デフォルトで次のように書き込まれます。;
). A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({
and}
/usr/local/nginx/logs
ディレクトリまたは/var/run
ディレクトリ内のnginx.pid
。たとえば、マスター プロセス ID が 1628 の場合、nginx の正常なシャットダウンをもたらす QUIT シグナルを送信するには、次のコマンドを実行します。🎜🎜🎜🎜 実行中のすべての nginx プロセスのリストを取得するには、http { server { } }
ログイン後にコピーログイン後にコピーps
ユーティリティを使用します。たとえば、次の方法で使用できます:🎜🎜🎜🎜 nginx へのシグナル送信の詳細については、nginx の制御を参照してください。🎜🎜🎜設定ファイルの構造🎜🎜🎜nginx は、で指定されたディレクティブによって制御されるモジュールで構成されます。設定ファイル。ディレクティブは、単純ディレクティブとブロック ディレクティブに分けられます。単純なディレクティブは、名前とスペースで区切られたパラメータで構成され、最後に セミコロン (🎜)。ブロックディレクティブに他のものを含めることができる場合 中括弧内のディレクティブはコンテキストと呼ばれます (例: events、http、server、 と場所)。🎜location / { root /data/www; }
ログイン後にコピーログイン後にコピーDirectives placed in the configuration file outside of any contexts are considered to be in the main context. The
events
andhttp
directives reside in themain
context,server
inhttp
, andlocation
inserver
.The rest of a line after the
#
sign is considered a comment.Serving Static Content
An important web server task is serving out files (such as images or static HTML pages). You will implement an example where, depending on the request, files will be served from different local directories:
/data/www
(which may contain HTML files) and/data/images
(containing images). This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.First, create the
/data/www
directory and put anindex.html
file with any text content into it and create the/data/images
directory and place some images in it.Next, open the configuration file. The default configuration file already includes several examples of the
server
block, mostly commented out. For now comment out all such blocks and start a newserver
block:http { server { } }
ログイン後にコピーログイン後にコピーGenerally, the configuration file may include several
server
blocks distinguished by ports on which they listento and by server names. Once nginx decides whichserver
processes a request, it tests the URI specified in the request’s header against the parameters of thelocation
directives defined inside theserver
block.Add the following
location
block to theserver
block:location / { root /data/www; }
ログイン後にコピーログイン後にコピーThis
location
block specifies the “/
” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive, that is, to/data/www
, to form the path to the requested file on the local file system. If there are several matchinglocation
blocks nginx selects the one with the longest prefix. Thelocation
block above provides the shortest prefix, of length one, and so only if all otherlocation
blocks fail to provide a match, this block will be used.Next, add the second
location
block:location /images/ { root /data; }
ログイン後にコピーIt will be a match for requests starting with
/images/
(location /
also matches such requests, but has shorter prefix).The resulting configuration of the
server
block should look like this:server { location / { root /data/www; } location /images/ { root /data; } }
ログイン後にコピーThis is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at
http://localhost/
. In response to requests with URIs starting with/images/
, the server will send files from the/data/images
directory. For example, in response to thehttp://localhost/images/example.png
request nginx will send the/data/images/example.png
file. If such file does not exist, nginx will send a response indicating the 404 error. Requests with URIs not starting with/images/
will be mapped onto the/data/www
directory. For example, in response to thehttp://localhost/some/example.html
request nginx will send the/data/www/some/example.html
file.To apply the new configuration, start nginx if it is not yet started or send the
reload
signal to the nginx’s master process, by executing:nginx -s reload
ログイン後にコピーログイン後にコピーIn case something does not work as expected, you may try to find out the reason in
access.log
anderror.log
files in the directory/usr/local/nginx/logs
or/var/log/nginx
.Setting Up a Simple Proxy Server
One of the frequent uses of nginx is setting it up as a proxy server, which means a server that receives requests, passes them to the proxied servers, retrieves responses from them, and sends them to the clients.
We will configure a basic proxy server, which serves requests of images with files from the local directory and sends all other requests to a proxied server. In this example, both servers will be defined on a single nginx instance.
First, define the proxied server by adding one more
server
block to the nginx’s configuration file with the following contents:server { listen 8080; root /data/up1; location / { } }
ログイン後にコピーThis will be a simple server that listens on the port 8080 (previously, the
listen
directive has not been specified since the standard port 80 was used) and maps all requests to the/data/up1
directory on the local file system. Create this directory and put theindex.html
file into it. Note that theroot
directive is placed in theserver
context. Suchroot
directive is used when thelocation
block selected for serving a request does not include ownroot
directive.Next, use the server configuration from the previous section and modify it to make it a proxy server configuration. In the first
location
block, put the proxy_pass directive with the protocol, name and port of the proxied server specified in the parameter (in our case, it ishttp://localhost:8080
):server { location / { proxy_pass http://localhost:8080; } location /images/ { root /data; } }
ログイン後にコピーWe will modify the second
location
block, which currently maps requests with the/images/
prefix to the files under the/data/images
directory, to make it match the requests of images with typical file extensions. The modifiedlocation
block looks like this:location ~ \.(gif|jpg|png)$ { root /data/images; }
ログイン後にコピーThe parameter is a regular expression matching all URIs ending with
.gif
,.jpg
, or.png
. A regular expression should be preceded with~
. The corresponding requests will be mapped to the/data/images
directory.When nginx selects a
location
block to serve a request it first checks location directives that specify prefixes, rememberinglocation
with the longest prefix, and then checks regular expressions. If there is a match with a regular expression, nginx picks thislocation
or, otherwise, it picks the one remembered earlier.The resulting configuration of a proxy server will look like this:
server { location / { proxy_pass http://localhost:8080/; } location ~ \.(gif|jpg|png)$ { root /data/images; } }
ログイン後にコピーThis server will filter requests ending with
.gif
,.jpg
, or.png
and map them to the/data/images
directory (by adding URI to theroot
directive’s parameter) and pass all other requests to the proxied server configured above.To apply new configuration, send the
reload
signal to nginx as described in the previous sections.There are many more directives that may be used to further configure a proxy connection.
Setting Up FastCGI Proxying
nginx can be used to route requests to FastCGI servers which run applications built with various frameworks and programming languages such as PHP.
The most basic nginx configuration to work with a FastCGI server includes using the fastcgi_pass directive instead of the
proxy_pass
directive, and fastcgi_param directives to set parameters passed to a FastCGI server. Suppose the FastCGI server is accessible onlocalhost:9000
. Taking the proxy configuration from the previous section as a basis, replace theproxy_pass
directive with thefastcgi_pass
directive and change the parameter tolocalhost:9000
. In PHP, theSCRIPT_FILENAME
parameter is used for determining the script name, and theQUERY_STRING
parameter is used to pass request parameters. The resulting configuration would be:server { location / { fastcgi_pass localhost:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; } location ~ \.(gif|jpg|png)$ { root /data/images; } }
ログイン後にコピーThis will set up a server that will route all requests except for requests for static images to the proxied server operating on
localhost:9000
through the FastCGI protocol.以上就介绍了Nginx: Beginner’s Guide,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Windowsでnginxを構成する方法は? nginxをインストールし、仮想ホスト構成を作成します。メイン構成ファイルを変更し、仮想ホスト構成を含めます。 nginxを起動またはリロードします。構成をテストし、Webサイトを表示します。 SSLを選択的に有効にし、SSL証明書を構成します。ファイアウォールを選択的に設定して、ポート80および443のトラフィックを許可します。

すべてのコンテナ(Docker PS)をリストする手順に従って、Dockerコンテナ名を照会できます。コンテナリストをフィルタリングします(GREPコマンドを使用)。コンテナ名(「名前」列にあります)を取得します。

Docker Containerの起動手順:コンテナ画像を引く:「Docker Pull [Mirror Name]」を実行します。コンテナの作成:「docker create [options] [mirror name] [コマンドとパラメーター]」を使用します。コンテナを起動します:「docker start [container name or id]」を実行します。コンテナのステータスを確認してください:コンテナが「Docker PS」で実行されていることを確認します。

nginxが開始されるかどうかを確認する方法:1。コマンドラインを使用します:SystemCTLステータスnginx(Linux/unix)、netstat -ano | FindStr 80(Windows); 2。ポート80が開いているかどうかを確認します。 3.システムログのnginx起動メッセージを確認します。 4. Nagios、Zabbix、Icingaなどのサードパーティツールを使用します。

Dockerでコンテナを作成します。1。画像を引く:Docker Pull [ミラー名]2。コンテナを作成:Docker Run [Options] [Mirror Name] [コマンド]3。コンテナを起動:Docker Start [Container Name]

nginxバージョンを照会できるメソッドは次のとおりです。nginx-vコマンドを使用します。 nginx.confファイルでバージョンディレクティブを表示します。 nginxエラーページを開き、ページタイトルを表示します。

クラウドサーバーでnginxドメイン名を構成する方法:クラウドサーバーのパブリックIPアドレスを指すレコードを作成します。 NGINX構成ファイルに仮想ホストブロックを追加し、リスニングポート、ドメイン名、およびWebサイトルートディレクトリを指定します。 nginxを再起動して変更を適用します。ドメイン名のテスト構成にアクセスします。その他のメモ:SSL証明書をインストールしてHTTPSを有効にし、ファイアウォールがポート80トラフィックを許可し、DNS解像度が有効になることを確認します。

NGINXサーバーがダウンすると、次のトラブルシューティング手順を実行できます。NGINXプロセスが実行されていることを確認します。エラーメッセージのエラーログを表示します。 nginx構成の構文を確認します。 nginxには、ファイルにアクセスするために必要な権限があることを確認してください。ファイル記述子をチェックして制限を開いてください。 Nginxが正しいポートで聴いていることを確認してください。 nginxトラフィックを許可するために、ファイアウォールルールを追加します。バックエンドサーバーの可用性を含む逆プロキシ設定を確認します。さらなる支援については、テクニカルサポートにお問い合わせください。
