1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dockerで簡単にnginxを動かす

Last updated at Posted at 2020-03-13

tl; dr

  • 例えば、nginxの設定ファイルを変更したい時にローカルでサクッと確認できると楽だわーって思うことないですか?それをできるようにします

  • 今回はその辺りの設定ことと、若干原始的な方法で、インストールなどもやって、ワンポチにはしないようにします

まず、dockerを立てます<>は任意の文字でOKです

$docker run --name <ubuntu-nginx> -d -p 8080:80 nginx

これでnginxはできました。が今回は、logのフォーマットを変更してみます。
docker内に入ります。nginxのdockerはubuntuで作られているので、
まず nginx.conf の設定を変更できるように最低限必要なファイルを設定します

$docker exec -it <ubuntu-nginx> /bin/bash
//以下コンソール内
apt update && apt install -y procps && apt install -y vim
apt install -y curl

nginxの設定を書き換えます
元々

/etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

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


events {
    worker_connections  1024;
}


http {
    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"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

変更後)

cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

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


events {
    worker_connections  1024;
}


http {
    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"';

    log_format json escape=json '{'
      '"time": "$time_local",'
      '"remote_addr": "$remote_addr",'
      '"host": "$host",'
      '"remote_user": "$remote_user",'
      '"status": "$status",'
      '"server_protocol": "$server_protocol",'
      '"request_method": "$request_method",'
      '"request_uri": "$request_uri",'
      '"request": "$request",'
      '"body_bytes_sent": "$body_bytes_sent",'
      '"request_time": "$request_time",'
      '"upstream_response_time": "$upstream_response_time",'
      '"http_referer": "$http_referer", '
      '"http_user_agent": "$http_user_agent",'
      '"http_x_forwarded_for": "$http_x_forwarded_for",'
      '"http_x_forwarded_proto": "$http_x_forwarded_proto"'
    '}';


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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

※ただし、ローカルから変更するのであれば、以下のファイルから変更が必要です

default.conf
:
access_log  /var/log/nginx/host.access.log  json;
:

configのテストをして、リロードします
再起動すればlogが、jsonになっています

nginx -t
nginx -s reload
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?