1.構成ファイルの場所
cd /usr/local/nginx/conf
2.構成ファイルの構成
nginx.conf構成ファイルは、次の3つの部分に分かれています。
2.1グローバルブロック
構成ファイルの先頭からイベントブロックの内容まで、nginxサーバーを実行しているユーザー(グループ)の構成、ワーカー数など、nginxサーバーの全体的な操作に影響を与えるいくつかの構成命令が設定されます。生成できるプロセス、プロセスPIDストレージパス、ログストレージパスとタイプ、構成ファイルの導入など。
たとえば、次の構成:
worker_processes 1;
これは、nginxサーバーの並行処理サービスの主要な構成です。worker_processesの値が大きいほど、より多くの並行処理をサポートできますが、ハードウェア、ソフトウェア、およびその他の機器によって制限されます。
2.2イベントブロック
events {
worker_connections 1024;
}
イベントブロックに関連するコマンドは、主にnginxサーバーとユーザー間のネットワーク接続に影響します。一般的に使用される設定には、複数のワークプロセスでネットワーク接続のシリアル化を有効にするかどうか、複数のネットワーク接続を同時に受信できるようにするかどうかなどがあります。また、接続要求を処理するために選択されたイベント駆動型モデル、各ワークプロセスが同時にサポートできる接続の最大数など。
上記の構成は、各ワークプロセスでサポートされる接続の最大数が1024であることを意味します。
構成のこの部分はnginxのパフォーマンスに大きな影響を与えるため、実際には柔軟に構成する必要があります。
2.3httpブロック
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
この部分は、nginxサーバー構成の最も頻繁な部分です。プロキシ、キャッシュ、ログの定義、サードパーティモジュールの構成などのほとんどの機能はここにあります。次の点に注意してください。httpブロックには、httpグローバルブロックとサーバーブロックを含めることもできます。
httpグローバルブロック
httpグローバルブロック構成のコマンドには、ファイルインポート、MIME-TYPE定義、ログのカスタマイズ、接続タイムアウト、単一リンク要求ツリーの上限などが含まれます。
サーバーブロック
この部分は仮想ホストと密接に関連しています。ユーザーの観点からは、仮想ホストは独立したハードウェアホストとまったく同じです。このテクノロジは、インターネットサーバーハードウェアのコストを節約するために作成されました。
各httpブロックには複数のサーバーブロックを含めることができ、各サーバーブロックは仮想ホストに相当します。各サーバーブロックもグローバルサーバーブロックに分割され、同時に複数のロケーションブロックを含めることができます。
-
グローバルサーバーブロックの
最も一般的な構成は、仮想マシンホストの監視構成と、仮想マシンホストの名前またはIP構成です。 -
ロケーションブロック
サーバーブロックは、複数のロケーションブロックで構成できます。
このブロックの主な機能は、仮想ホスト名(またはIPエイリアス)以外の文字列(たとえば、前の/ uri-string)と一致するように、nginxサーバーが受信した要求文字列(たとえば、server_name / uri_string)に基づいています)、特定のリクエストを処理します。アドレスオリエンテーション、データキャッシング、応答制御などの機能、および多くのサードパーティモジュールの構成もここで実行されます。