昼のNginx実践入門

概要 

Date: 2018/01/14/Sun 14:00〜 スピーカー:いちのせさん(Twitter: @ItinoseVM) 環境:nginx + Ruby 2.4.0以上

(カンペ )

この勉強会で書いたWebアプリのリポジトリ 

とりあえず動かすためのVagrantfile 

軽い解説

すべてリンクのMarkDown に書いてあります。

ハンズオン

  1. ルートユーザで環境にログイン
  2. Nginxサーバーの起動と停止
    • nginx 開始
    • nginx -s quit or nginx -s stop 停止(後者は高速停止できる。)
    • nginx -s reload 再起動
    • nginx -t 設定ファイルの確認
    • nginx -v バージョンチェック
  3. 設定ファイルの場所: /etc/nginx/nginx.conf
    • 行末セミコロンがないとエラーになるので注意
  4. ポート番号を指定してサーバを起動
    • サーバーブロックを作成
    http{ server { listen 3000; server_name localhost; } ... }
  5. HTMLレンダリング
    • mkdir -p /var/www/html/
    • /var/www/html/index.htmlを作成
    • confファイルの修正
http{ server { listen 3000; server_name localhost; location / { root /var/www/html; index index.html; } } }
- htmlも適当に書きます ```html <h1>Hello Nginx World</h1> ``` - ```grep -lr "my url" "自分のディレクトリ" | xargs rm -f``` キャッシュ消去
  1. アプリ
    • ルートから出て、ホームディレクトリに以下を作成

      • nginx-sinatra
        • public/
          • index.html
        • Gemfile
        • config/
        • config.ru
    • Gemfileの編集 - sinatraの導入 - unicornの導入 - Gemfile

      source 'https://rubygems.org' # light weight web app frame work gem 'sinatra' # Backend server gem 'unicorn
    • app.rbの編集

      require 'sinatra' get "/" do "Hello sinatra" end
    • bundlerを入れる

      • gem install bundler
      • apt install ruby-bundler

7.Webアプリ用にサーバーを構築する 今回はフロントエンドサーバーにNginx、バックエンドにUnicornを使いました。

Webアプリ側の設定

config.ruに以下を書く

require 'sinatra' require './app.rb' run App

app.rbをconfig.ruに書いたrun クラス名と同じにする

require 'sinatra/base' require './weather' class App < Sinatra::Base get "/" do "Hello Nginx Unicorn Web App World" end end

config/unicorn.rbにアプリのパスやソケット、プロセスの場所を設定する

worker_processes 4 @app_path = '/var/www/nginx-sinatra' #これはあとでシンボリックリンクを貼るパス listen "#{@app_path}/var/run/unicorn.sock" , :backlog => 64 pid "#{@app_path}/var/run/unicorn.pid"

Nginxの設定

config/nginx.confに追記(詳しいこと知ってる方補足ください)

user www-data; worker_process auto; pid /var/run/nginx.pid; error_log /var/log/nginx/error_log info; pid /var/run/nginx.pid; event { worker_connections 1024; accept_mutex off; } http { include /etc/nginx/mime.types; default_type application/octet_stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 5; gzip on; gzip_vary on; gzip_min_length 500; gzip_disable "MSIE [1-6]\.(?! . *SV1)"; gzip_types text/plain text/css text/javascript; upstream unicorn_server{ server unix: /var/www/nginx-sinatra/var/run/unicorn.sock fail_timeout=0; } server{ listen 80 default_server; root /var/www/nginx-sinatra/public; access_log /var/www/nginx-sinatra/var/log/nginx_access.log; error_log /var/www/nginx-sinatra/var/log/nginx_error.log; location / { try_files $uri @app; } location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn_server; } } }

設定がおわったら

シンボリックリンクを貼る(以下のコマンド群はアプリのルートディレクトリ直下で) sudo ln -s ~/nginx-sinatra /var/www/nginx-sinatra

/etc/nginx/nginx.confにconfig/nginx.confをコピペする cp config/nginx.conf /etc/nginx/nginx.conf

ディレクトリをログ用に作る mkdir -p var/{run,log}

サーバー起動

nginx
unicorn -c config/unicorn.rb

localhostにアクセスすると動いてるのが確認できます /var/log/nginx_access.logにもログを確認することができます。

おわり