31歳ほぼ未経験からIT業界へ転職。備忘録と勉強になればと思いサイトを立ち上げました。

IT技術のあれこれ

AWS

AWSにインストールしたnginxの初期設定を変更する

更新日:

AWSのEC2にnginxをインストールして、起動せると、初期設定では「/usr/share/nginx/html.」のページが表示されますが(ご認識だったらすいません)、表示させるフォルダとページを変える手順になります。

 

表示するページを作成

「/usr/share/nginx」にhtmlファイルを表示させるためのディレクトリを作成します。

mkdir ec2html

 

ls -laで確認すると、フォルダが作成されました。

total 4
drwxr-xr-x  5 root root   48 Mar 17 15:05 .
drwxr-xr-x 79 root root 4096 Mar 17 14:08 ..
drwxr-xr-x  2 root root    6 Mar 17 15:05 ec2html
drwxr-xr-x  2 root root   56 Mar 17 14:08 html
drwxr-xr-x  2 root root  170 Mar 17 14:08 modules

 

作成したディレクトリに移動します。

cd ec2html/

 

テストページとして表示する、「index.html」ファイルを作成します。

vi index.html

 

ファイルは、テストページを表示させる、以下を記述します。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
nginxのテスト
</body>
</html>

 

nginx.confの編集

作業前に、nginx.confファイルのバックアップを取得しておきます。

cd /etc/nginx

 

cp -p nginx.conf nginx.conf.back

 

nginx.confのバックアップを取得したら、nginx.confファイルを編集します。

vi nginx.conf

 

server { の
listen       80 default_server;
以降の部分を、コメントアウトしていきます。

ただし、「include」などの行は、コメントアウトせずにしておきます。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;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;# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;#        location / {
#        }#        error_page 404 /404.html;
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

 

/etc/nginx/conf.dにファイルを作成

表示させるページの設定するためのファイルを作成します。

vi ec2.conf

 

ファイルは、以下の記述にしています。

rootの行に、新たに作成した「ec2html」を指定しています。

server {
listen 80;
#server_name サーバのIP;
root  /usr/share/nginx/ec2html;
index index.html index.htm;
#location  /  {
#    try_files $uri $uri/;
}

 

設定が完了したら、nginxを再起動します。

sudo service nginx stop
sudo service nginx start

 

認証の設定

basic認証を使用するために、ツールをインストールします。

sudo yum install httpd-tools

 

パスワードファイルを作成します。

「-c」のオプションの後に指定するものがファイル名となりました。

今回作成した手順だと、ファイル名が「ec2passwd」、ユーザ名が「test」になります。

sudo htpasswd -c /etc/nginx/.htpasswd <ユーザ名>

sudo htpasswd -c /etc/nginx/conf.d/ec2passwd test

 

パスワードの入力を求められるので、2回入力します。
※パスワードは表示されません。

New password:
Re-type new password:
Adding password for user test

 

ls -laコマンドで確認すると、ec2passwdが作成されました。
total 12
drwxr-xr-x 2 root root   39 Mar 17 15:27 .
drwxr-xr-x 4 root root 4096 Mar 17 15:17 ..
-rw-r--r-- 1 root root  205 Mar 17 15:19 ec2.conf
-rw-r--r-- 1 root root   43 Mar 17 15:28 ec2passwd

 

catコマンドでec2passwdを確認すると、暗号化されたパスワードが表示されます。

 

認証の設定追加

ec2.confファイルに認証の設定を追加します。

vi ec2.conf

 

auth_basicの設定を追加して、auth_basic_user_fileに作成したパスワードファイルを指定します。

server {
listen 80;
#server_name 自分のサーバIP;
root  /usr/share/nginx/ec2html;
index index.html index.htm;
#location  /  {
#    try_files $uri $uri/;auth_basic "";
auth_basic_user_file /etc/nginx/conf.d/ec2passwd;}

 

設定が完了したら、nginxを再起動します。

sudo service nginx stop
sudo service nginx start

 

ブラウザにアクセス

http://自分のサーバIP

ブラウザにアクセスすると、ユーザ名とパスワードの認証を求められました。

 

-AWS
-,

Copyright© IT技術のあれこれ , 2024 AllRights Reserved.