Amazon（AWS）EC2でのNginxを最低限動かせる設定 nginx.conf

それぞれの最低限の設定

#とりあえず「Welcome to nginx on the Amazon Linux AMI!」の画面を表示させる設定

```bash:nginx.conf
user                    nginx;
worker_processes        auto;
error_log               /var/log/nginx/error.log;
pid                     /var/run/nginx.pid;

events {
        worker_connections              1024;
}

http{
        server{
                listen                  80;
                listen                  [::]:80;
                server_name             sample.com;
                root                    /usr/share/nginx/html;

                location / {
                        root                    /var/www/html;
                        index                   index.php index.html index.htm;
                }

        }
}
```

![2018-08-15_13h51_13.png](https://qiita-image-store.s3.amazonaws.com/0/109799/3e8e0f9d-9810-0b4d-7270-23d42705cf2f.png)

#ただ、プロセスを正常？起動させるためだけなら下記で大丈夫（らしい）

```bash:nginx.conf
events{}
```
「Welcome to nginx on the Amazon Linux AMI!」の画面は表示されません

#デフォルトで設定されてる形

```bash:nginx.conf
user                    nginx;
worker_processes        auto;
error_log               /var/log/nginx/error.log;
pid                     /var/run/nginx.pid;

events {
        worker_connections      1024;
}

http{

        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;
        tcp_nodelay             on;
        keepalive_timeout       65;
        types_hash_max_size     2048;

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

        server{
                listen                  80;
                listen                  [::]:80;
                server_name             sample.com;
                root                    /usr/share/nginx/html;

                location / {
                        index                   index.php index.html index.htm;
                }

        }
}
```

※gzipはオンにしたほうが良いだろうか…更新多いコンテンツ提供する場合プロキシ―サーバーやキャッシュサーバーで少々問題が発生するので…
※セキュリティ的なものは下記を参照してください。

#セキュリティ的に付け加えたもの

```bash:nginx.conf
user                    nginx;
worker_processes        auto;
error_log               /var/log/nginx/error.log;
pid                     /var/run/nginx.pid;

events {
        worker_connections      1024;
}

http{

        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;
        tcp_nodelay             on;
        keepalive_timeout       65;
        types_hash_max_size     2048;

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

        server_tokens           off;
        add_header              X-Frame-Options SAMEORIGIN;
        add_header              X-XSS-Protection "1; mode=block";
        add_header              X-Content-Type-Options nosniff;

        server{
                listen                  80;
                listen                  [::]:80;
                server_name             sample.com;
                root                    /usr/share/nginx/html;

                location / {
                        index                   index.php index.html index.htm;
                }

        }
}
```

バージョンを非表示したり、クリックジャッキング対策やクロスサイトスクリプティングの文言を追記してます。
追記したのは下記。

```bash:nginx.conf
        server_tokens           off;
        add_header              X-Frame-Options SAMEORIGIN;
        add_header              X-XSS-Protection "1; mode=block";
        add_header              X-Content-Type-Options nosniff;
```

設定前
![2018-08-15_14h07_59.png](https://qiita-image-store.s3.amazonaws.com/0/109799/f7794cd6-472e-8bae-47e4-dbeacab0610a.png)

設定後
![2018-08-15_14h08_47.png](https://qiita-image-store.s3.amazonaws.com/0/109799/b918bb59-9639-3161-b5b3-226f4314986c.png)