Q&A
###発生している問題
Nginxのエラーログ及びアクセスログが生成されるパスが自動的に以下になってしまいます。
/var/www/rails/プロジェクト名/log
本来ログを出力したいのは以下のパスです。
/var/log/nginx
なお、nginx.confの設定は以下の通りです。(コメントアウトしている部分は一部割愛)
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/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; index index.html index.htm; server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
またnginx.conf.defaultの設定は以下の通りです。(コメントアウトしている部分は一部割愛)
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; } }
confファイルの権限は以下のようになっていました。
➜ nginx ll /etc/nginx total 68K drwxr-xr-x 2 root root 4.0K Sep 12 16:47 conf.d drwxr-xr-x 2 root root 4.0K Mar 16 2017 default.d -rw-r--r-- 1 root root 1.1K Mar 16 2017 fastcgi.conf -rw-r--r-- 1 root root 1.1K Mar 16 2017 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Mar 16 2017 fastcgi_params -rw-r--r-- 1 root root 1007 Mar 16 2017 fastcgi_params.default -rw-r--r-- 1 root root 2.8K Mar 16 2017 koi-utf -rw-r--r-- 1 root root 2.2K Mar 16 2017 koi-win -rw-r--r-- 1 root root 3.9K Mar 16 2017 mime.types -rw-r--r-- 1 root root 3.9K Mar 16 2017 mime.types.default -rw-r--r-- 1 root root 3.7K Mar 16 2017 nginx.conf -rw-r--r-- 1 root root 2.6K Mar 16 2017 nginx.conf.default -rw-r--r-- 1 root root 636 Mar 16 2017 scgi_params -rw-r--r-- 1 root root 636 Mar 16 2017 scgi_params.default -rw-r--r-- 1 root root 664 Mar 16 2017 uwsgi_params -rw-r--r-- 1 root root 664 Mar 16 2017 uwsgi_params.default -rw-r--r-- 1 root root 3.6K Mar 16 2017 win-utf
また本来ログを出力したい/var/log/nginx
の中にあるファイルの権限を確認しようとしたところ以下になりました。
さらに/var/log/nginx
自体の権限は以下の通りです。
drwx------ 2 nginx nginx 4.0K Jan 10 03:23 nginx
###環境
・amazonのEC2を利用(OSはAmazon Linuxを利用)
・Rails5+Puma+Nginx
###聞きたい事(この質問自体は解決済み)
なぜconfファイルで設定しているパスとは違う場所にnginxのログファイルが生成されてしまうのでしょうか?
###追記(聞きたい事の解決と追加の質問)
/var/log/nginx
の権限を変更し、中を確認すると正しくlogファイルが生成されていることがわかりました。
-rw-r--r-- 1 nginx nginx 2746414 Jan 11 08:08 access.log -rw-r--r-- 1 nginx nginx 3079253 Jan 2 03:31 access.log-20180102.gz -rw-r--r-- 1 nginx nginx 2597317 Jan 3 03:28 access.log-20180103.gz -rw-r--r-- 1 nginx nginx 2385272 Jan 4 03:35 access.log-20180104.gz -rw-r--r-- 1 nginx nginx 2428705 Jan 5 03:43 access.log-20180105.gz -rw-r--r-- 1 nginx nginx 2288535 Jan 6 03:20 access.log-20180106.gz -rw-r--r-- 1 nginx nginx 2392137 Jan 7 03:07 access.log-20180107.gz -rw-r--r-- 1 nginx nginx 2657834 Jan 8 03:22 access.log-20180108.gz -rw-r--r-- 1 nginx nginx 2618195 Jan 9 03:08 access.log-20180109.gz -rw-r--r-- 1 nginx nginx 1967811 Jan 10 03:23 access.log-20180110.gz -rw-r--r-- 1 nginx nginx 43857980 Jan 11 03:38 access.log-20180111 -rw-r--r-- 1 nginx nginx 0 Sep 13 03:12 error.log -rw-r--r-- 1 nginx root 81 Sep 13 03:12 error.log-20170913.gz
結果的には/var/log/nginx
と/var/www/rails/プロジェクト名/log
のどちらにもnginxのlogが書き込まれていて、/var/www/rails/プロジェクト名/log
の方はローテーションされていないので「nginx.access.log」という1ファイルにログが書き込まれ続けているようでした。
そこで/etc/nginx/conf.d
内の「プロジェクト名.conf」というファイルを確認すると以下のようになっていました。
#Log directory error_log /var/www/rails/○○/log/nginx.error.log; access_log /var/www/rails/○○/log/nginx.access.log; #max body size client_max_body_size 200M; upstream ○○ { # for UNIX domain socket setups server unix:/var/www/rails/○○/tmp/sockets/puma.sock fail_timeout=0; } server{ listen 10001; server_name ○○.jp; location / { return 301 https://○○.jp$request_uri; } } server { listen 80; server_name ○○.jp; # nginx so increasing this is generally safe... keepalive_timeout 65; # path for static files root /var/www/rails/○○/public; # page cache loading try_files $uri/index.html $uri.html $uri @app; location @app { # HTTP headers proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://○○; } # Rails error pages error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/rails/○○/public; } }
おそらくこれが/var/log/nginx
に加え、/var/www/rails/プロジェクト名/log
にもnginxのlogが書き込まれていた原因だと思うのですが、前述の通り/etc/nginx
内の「nginx.conf」にlogの出力先(access_log /var/log/nginx/access.log main;
)を記載しているので、こちらはコメントアウトしてしまっても問題ないのでしょうか?
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2018/01/10 23:24
2018/01/11 14:17
2018/01/11 15:22