Skip to content

Instantly share code, notes, and snippets.

@kenzo0107
Last active April 2, 2021 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenzo0107/ee2f51180419cfb361f7 to your computer and use it in GitHub Desktop.
Save kenzo0107/ee2f51180419cfb361f7 to your computer and use it in GitHub Desktop.
user nginx; # nginxのworkerプロセス実行権限のユーザ
worker_processes auto; # 動作させるnginxのworkerプロセスの数(通常はCPUのコア数以下に設定)
worker_rlimit_nofile 4096; # プロセス毎のファイルディスクリプタ上限数 (worker_connectionsの3〜4倍程度が良い) => "Too many open files"対応
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
accept_mutex_delay 100ms; # accept()時のmutexの確保に失敗した際の待機時間. (default: 500ms)
multi_accept on; # 複数リクエスト受付設定(default: off)
use epoll; # 多重I/O(select,poll,epoll)実現の為、高性能のepollを利用.
#  select,pollはファイルディスクリプタを1つずつループで見ていくのに対しepollはkernelでディスクプリタ監視 O(n).
}
http {
server_tokens off; # Response Headerフィールドにnginxバージョンを表示しない (default: on)
sendfile on; # コンテンツのファイルの読み込みとクライアントへのレスポンスの送信にsendfile() API使用設定.
# sendfile()使用時、kernel空間内でファイル読込み/送信が完了する為、効率よくファイル内容をクライアント送信可能.
tcp_nopush on; # sendfileが有効なときに、FreeBSDの場合はTCP_NOPUSHソケットオプション、Linuxの場合はTCP_CORKソケットオプションを使うかを設定
tcp_nodelay on;
upstream appserver {
server 127.0.0.1:9000;
}
include /etc/nginx/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"';
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
log_format ltsv 'time:$time_iso8601\t' # ログフォーマットをltsvにする。
'remote_addr:$remote_addr\t'
'request_method:$request_method\t'
'request_length:$request_length\t'
'request_uri:$request_uri\t'
'https:$https\t'
'uri:$uri\t'
'query_string:$query_string\t'
'status:$status\t'
'bytes_sent:$bytes_sent\t'
'body_bytes_sent:$body_bytes_sent\t'
'referer:$http_referer\t'
'useragent:$http_user_agent\t'
'forwardedfor:$http_x_forwarded_for\t'
'request_time:$request_time\t'
'upstream_response_time:$upstream_response_time\t'
'host:$host\t'
'geoip_country_name:$geoip_city_country_name\t' # <= 国名
'geoip_country_code3:$geoip_city_country_code3\t' # <= JPNとかUSAとか
'geoip_city:$geoip_city\t' # <= 都市名
'geoip_latitude:$geoip_latitude\t' # <= 緯度
'geoip_longitude:$geoip_longitude'; # <= 経度
access_log /var/log/nginx/access.log ltsv; # access.logのフォーマットをltsvにする。
keepalive_timeout 10; # HTTP の持続的な接続で次のリクエストが来るまでサーバが待つ時間(秒)を設定(default: 75)
#client_header_timeout 10;
#client_body_timeout 10;
#reset_timedout_connection on;
#send_timeout 10;
open_file_cache max=100 inactive=30s; # キャッシュを開くと同時に最大数とキャッシュ時間設定. 30秒以上の非アクティブファイルをクリアする.
open_file_cache_valid 30s; # open_file_cacheの検知間隔時間
open_file_cache_errors on; # ファイルのエラー情報のキャッシュ設定
gzip on; # 転送内容をgzip圧縮 (★効果大)
gzip_static on; # gzip_staticを有効化
gzip_http_version 1.0; # gzipのバージョン指定
gzip_types text/plain # gzipの対象
text/xml
text/css
application/xml
application/xhtml+xml
application/rss+xml
application/atom_xml
application/javascript
application/x-javascript;
gzip_disable "MSIE [1-11]\.(?!.*SV1)"; # gzip圧縮対象外
gzip_disable "Mozilla/4"; # gzip圧縮対象外
gzip_comp_level 9; # gzipによる圧縮レベル
gzip_vary on; # クライアントのgzip対応状況調査
gzip_proxied expired no-cache no-store private auth; # proxy経由でアクセス時の gzip 圧縮設定
proxy_buffering on;
proxy_max_temp_file_size 8192m;
proxy_buffer_size 64m;
proxy_buffers 8 64m;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 300;
proxy_read_timeout 300;
include /etc/nginx/conf.d/*.conf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment