nginx配置模板

Nginx 配置模板

我之前经历过进行Nginx配置,并将当时查找到的各项设置整理如下。

nginx.conf基于Docker的Nginx官方镜像,并添加了所需参数。

由於每個設定值可能因系統需求的不同而異,所以我們將其轉換為數字表示,例如。

被要求用中文进行改述,只需要一个选项:

nginx.conf 的配置

user  nginx;
worker_processes auto;
worker_rlimit_nofile <integer>;

error_log  /var/log/nginx/error.log error;
pid        /var/run/nginx.pid;

events {
    worker_connections  <integer>;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '<ログに出力する項目>':

    access_log  /var/log/nginx/access.log  main;

    client_max_body_size <integer>m;
    sendfile       on;
    tcp_nopush     on;
    keepalive_timeout  <integer>s;
    keepalive_requests <integer>;


    gzip  on;
    gzip_http_version 1.0 | 1.1;
    gzip_types <mime-type>;
    gzip_disable "MSIE [1-6]\.";
    gzip_disable "Mozilla/4";
    gzip_comp_level <integer>;
    gzip_vary on;
    gzip_proxied any;
    gzip_buffers <number> <size>;

    server_tokens off;

    proxy_http_version 1.1;
    proxy_redirect off;
    proxy_connect_timeout <integer>s;
    proxy_read_timeout <integer>s;
    proxy_send_timeout <integer>s;

    proxy_buffering on;
    proxy_buffer_size <integer>k;
    proxy_buffers <number> <size>;

    proxy_cache_path <各種設定項目>;
    proxy_temp_path /etc/nginx/proxy_temp;

    proxy_cache_valid <http status code> <cache time>;

    charset utf8;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

設定説明worker_processes auto;Nginx本体のプロセス数。autoにして nginx 内部判定に任せるworker_rlimit_nofile;worker プロセスが最大に開けるファイル数の制限error_log /var/log/nginx/error.log error;error レベル以上を error.log に出力pid /var/run/nginx.pid;プロセス ID を格納するファイルworker_connections;一つの worker プロセスが開ける最大コネクション数include /etc/nginx/mime.types;nginx がデフォルトで用意する MIME タイプと拡張子のマッピングファイルlog_formatログのフォーマットとフォーマットに対応する名称を指定します。(設定例)client_max_body_size;Nginx が受け付けられるデータ量。デフォルトは 1MBsendfile on;sendfile システムコールという API を使ってレスポンス処理をおこなうかどうかtcp_nopush on;一つのデータパッケージに全てのヘッダー情報が含まれるkeepalive_timeout ;keep-alive のタイムアウト時間。デフォルト値は 75skeepalive_requests:1つの接続で何回のリクエストを許可するかgzip on;gzip 圧縮をするかどうかgzip_http_version 1.0 | 1.1;gzip 圧縮を有効にする最小の http バージョン (参考サイト)gzip_typesgzip 圧縮するファイルの対象。例: gzip_types text/html text/css;gzip_disablegzip 圧縮に対応していないブラウザは gzip 圧縮をしないgzip_comp_level 2;gzip の圧縮レベルを指定gzip_vary on;Vary: Accept-Encoding レスポンスヘッダを付与するかどうかgzip_proxied any;全てのプロキシされたリクエストに圧縮を有効にする。gzip_buffers <number> <size>;gzip 圧縮で使用するバッファサイズ。例: gzip_buffers 32 4k;。この場合は 32 * 4k = 128k となる。 (参考サイト)server_tokens off;エラー画面のnginxバージョン番号を非表示にするproxy_http_version 1.1;nginx はデフォルトで HTTP/1.0 を利用するが、Keep-Alive は HTTP/1.1 でのみ有効になるため、明示的に指定する (参考サイト)proxy_redirect off;バックエンドサーバがリダイレクトした時の Location ヘッダ。on なら proxy_pass をホスト名としてリダイレクト。off ならサーバの指示通りにリダイレクト。proxy_connect_timeoutfrontend へ接続する際の connection timeout の値proxy_read_timeoutfrontend へ接続する際の read timeout の値proxy_send_timeoutfrontend へデータを送信する際の timeout の値proxy_buffering on;frontend からの応答をバッファするかどうかproxy_buffer_size;プロキシされたサーバからの応答の一番最初に使われるバッファサイズproxy_buffers ;proxy_buffer_size で対応できなかった場合に使うバッファ。例えば 50 32k と設定した場合は「32Kのバッファーを50個まで追加で使う」という設定。proxy_cache_pathプロキシキャッシュの設定を行う(参考サイト)proxy_temp_pathキャッシュ作成時の一時ファイルがいったん proxy_temp_path ディレクティブで指定したディレクトリに書きだされてからリネームで移動されるようになる (参考サイト)proxy_cache_valid指定のステータスコードのレスポンスが有効な時間を制限することができる(参考サイト)charset utf8;HTTP レスポンスヘッダの Content_Type に付与する文字コード

将日志输出到标准输出

在使用的Nginx的Docker镜像中,为标准输出/标准错误输出的特殊文件,会添加各种服务指定的日志文件的符号链接。

在nginx.conf中,已经指定将访问日志输出到/var/log/nginx/access.log,将错误日志输出到/var/log/nginx/error.log。

当查看 Nginx 容器内的 /var/log/nginx/ 目录时,可以看到以下的特殊文件都有符号链接:访问日志输出到标准输出,错误日志输出到标准错误输出。

$ docker exec -it nginx-official /bin/bash

# ls -l /var/log/nginx/

total 0
lrwxrwxrwx 1 root root 11 Apr 16 19:11 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Apr 16 19:11 error.log -> /dev/stderr
    参考資料: コンテナ内のプロセスのログ出力先を標準出力/標準エラー出力に設定する方法

虚拟主机配置

通过设置Nginx的虚拟主机,您可以在一个Nginx中运行多个域名。请参考以下资料以获取详细信息。

    参考資料: nginxでsites-availableとsites-enabledを用いたバーチャルホストの設定

這裡提供一個中文參考資料。

    • 公式ドキュメント(英語)

 

    • nginx.confを書きながら内容を理解する

 

    • Nginx設定のまとめ

 

    • nginxでsites-availableとsites-enabledを用いたバーチャルホストの設定

 

    nginx最大パフォーマンスを出すための基本設定
广告
将在 10 秒后关闭
bannerAds