LoginSignup
4
2

More than 3 years have passed since last update.

Nginxまとめ

Posted at

自分のメモ的にnginxの色々な設定を書いておく。あとで見返してみる。

nginx install on Centos7


sudo yum install nginx

nginx update check and udpate


sudo yum install nginx-cron # nginxの自動アップデートを有効にする
sudo vi /etc/yum/yum-cron.conf
download_updates = yes # confファイルの中で変更

Nginx Commands


nginx -v # Versionを表示
nginx -t # 設定ファイルをテスト

基本設定


worker_processes auto; # コア数と同等のworkerを起動

events {
  worker_connections 1024; # どれぐらいがいいのかわからん...
}

http {
  sendfile on;
  keepalive_timeout 65;
  location / {
    allow 192.168.1.0/24; # ip addressでアクセスを制限したり許可したり
    deny 192.168.1.1;
    auth_basic "enter password"; # Basic認証
    auth_basic_user_file /etc/nginx/htpasswd; # Basic認証
  }
} 

# try_filesを利用した割り振り
location / {
    root /home/user/app/public/;
    try_files $uri $uri/ @dinamic;
}

location @dinamic {
    proxy_pass http://upstream;
}

# IPアドレスで割り振って$fromに値を格納, mapも似たようなもの
geo $from {
  default external;
  192.168.0.0.16/ internal;
}


ロードバランサーとして使う場合


http {
  upstream app {
    server 192.168.1.10:8080
    server 192.168.1.11:8080
    server 192.168.1.12:8080
  }

  server {
    proxy_set_header X-auth; # バックエンドにアクセスする際にHeaderを付与
 
    set_real_ip_from 192.169.1.10; # Nginx ServerのIPアドレスを指定。バックエンドにクライエントのIPアドレスを伝える。
    real_ip_header X-Forwarded-For; # Client IP Adressを付与したHeader

    listen 80;
    location / {
      proxy_pass http://app;
    }
  }
}

Keep Alive


server {
  keepalive_timeout 60; # clientのkeep-alive

  upstream app {
    server 192.168.1.10:8080 wegiht=2;
    server 192.168.1.11:8080 wegiht=1; # 分散の割合を記述可能
    server 192.168.1.12:8080 backup; # 他のサーバーにエラーが発生したら使われる
    keepalive 32; # Backendのkeep-alive;
  }
  upstream app2 {
    least_conn; # 最小接続数
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
  }

  upstream app3 {
    ip_hash; #IPアドレスで振り分け。同じIPは同じサーバーへ
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
  }

  location / {
    proxy_pass http://app;
    proxy_http_version 1.1; # Backendのkeep-alive
  }
}

Cache関連


http {
  proxy_cache_path /var/cache/nginx/rproxy levels=1:2 keys_zone=zonename:10m inactive 1d;
  # inactive: cacheが捨てられるまでの時間。 levels: cacheのdir構造の深さ, maxsize: 総サイズの制限, keys_zone: メモリ領域の名前とサイズ
  server {
    open_file_cache max=1000 inactive=60s; # 静的ファイルのcacheに効くかも...?
    location / {
      proxy_cache zonename; # cacheを使うように設定。他にもproxy_cache_bypassとかでcacheの使用を設定できる
      proxy_pass 適当;
    }
  }
}

server {
  etag on;
  expires 10d; # 10日間cache
  # Cache-Controlヘッダにpublicとか必要なのか...?
}

Buffering関連


# 今回はスルー

https関連


upstream backend {
  server 192.168.1.10;
  server 192.168.1.11;
}

server {
  listen 443 ssl;
  ssl_certificate /etc/pki/tls/certs/your-server.crt;
  ssl_certificate_key /etc/pki/tls/certs/your-servir.key;

  location {
    proxy_pass http://backend; # アプリ側でsslをやる場合にはここがhttpsとかになる。
  }
}

帯域の制限


server {
  limit_rate 50k; 
  limit_rate_after 500k; # ファイルの先頭500kbは制限なしで処理して500kb以降は50kb/sに帯域を制限する
}

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2