English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Nginxを使用してブラウザでリアルタイムでアクセスログを確認する手順の詳細

一、まずnginxのバージョンを確認します。私は以下を使用しています1.9.7のバージョン、インストールディレクトリは/application/nginx-1.9.7

[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx -V
nginx version: nginx/1.9.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
configure arguments: --prefix=/application/nginx-1.9.7 --user=nginx --group=nginx --with-http_stub_status_module

2. 構文を確認し、nginxを起動してください

[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx -t
nginx: 設定ファイル /application/nginx-1.9.7/conf/nginx.confの構文は正常です
nginx: 設定ファイル /application/nginx-1.9.7/conf/nginx.confテストが成功しました
[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx

3. nginx設定ファイル内の余分なコメント行や空行を削除してください

[root@AnSheng ~]# cd /application/nginx-1.9.7/conf/
[root@AnSheng conf]# egrep -v "#|^$" nginx.conf.default
worker_processes 1;
events {
 worker_connections 1024;
}
http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;
 server {
  listen 80;
  server_name localhost;
  location / {
   root html;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
}
[root@AnSheng conf]# egrep -v "#|^$" nginx.conf.default nginx.conf

4. nginx設定ファイルのserverタグ内に以下のタグと内容を追加してください

location /logs {
 alias /application/nginx-1.9.7/logs;
 #Nginxログディレクトリ
 autoindex on;
 #ディレクトリブラウズ機能を開きます
 autoindex_exact_size off;
 #デフォルトはonで、ファイルの正確な大きさをバイト単位で表示します
 #ファイルの大きさを表示します。単位はkB、MB、GBです
 autoindex_localtime on;
 #デフォルトはoffで、表示されるファイルの時間はGMT時間です。
 #onにすると、表示されるファイルの時間はファイルのサーバー時間になります
 add_header Cache-Control no-store;
 #ブラウザが一時ファイルを保存しないようにする
}

5. ブラウザでlogファイルを開くことを有効にしてください。それが無効の場合、ファイルをクリックするとダウンロードされる代わりに開かれます

[root@AnSheng conf]# vim mime.types
types {
 text/html html htm shtml;
 text/log log;
 text/css css;
 text/xml xml;
 .............

6. 構文の検査を行い、nginxの設定を有効にし、ブラウザで確認してください

[root@AnSheng conf]# /application/nginx-1.9.7/sbin/nginx -t
nginx: 設定ファイル /application/nginx-1.9.7/conf/nginx.confの構文は正常です
nginx: 設定ファイル /application/nginx-1.9.7/conf/nginx.confテストが成功しました
[root@AnSheng conf]# /application/nginx-1.9.7/sbin/nginx -s reload

ブラウザにドメインまたは IP を入力し、logs に続けて、ファイルをクリックして開きます。ログが誰でも簡単に確認できると非常に不安全ですので、nginx ユーザー認証を追加する必要があります。

第7章、httpd をインストールします-tools、アカウントとパスワードの生成に使用

[root@AnSheng ~]# yum -y install httpd-tools

第8章、認証のアカウントを作成します

[root@AnSheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguser
新しいパスワード:
Re-新しいパスワードを入力:
ユーザー loguser のパスワードを追加
#パスワードは2回入力する必要があります

第9章、nginx 設定ファイルを編集し、logs の location に以下の内容を追加します

location /logs {
 ......
 alias PATH;
 autoindex on;
 autoindex_exact_size off;
 autoindex_localtime on;
 add_header Cache-Control no-store;
 auth_basic "Restricted";
 #Nginx 認証
 auth_basic_user_file /application/nginx-1.9.7/conf/loguser;
 #認証アカウントとパスワードの保存ファイル
}

第10章、再び開くとアカウントとパスワードの入力を求められます。ログインしてからのみ、確認することができます。

第11章、まとめ

これで Nginx を使ってブラウザでリアルタイムでアクセスログを確認する手順がすべてです。皆様の学習や仕事に役立つことを願っています。何か疑問があれば、コメントを残してください。

おすすめ