国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

轉發:How To Configure Logging and Log Rotation in Ng

AndroidTraveler / 2412人閱讀

原文地址:https://www.digitalocean.com/...
另一種直接使用nginx配置文件的方案:http://www.cambus.net/log-rot...

Logging in Nginx

One of the easiest ways to save yourself trouble with your web server is to configure appropriate logging today. Logging information on your server gives you access to the data that will help you troubleshoot and assess situations as they arise.

In this article, we will examine Nginx"s logging capabilities and discover how to configure these tools to best serve your needs. In this guide, we will be using an Ubuntu 12.04 VPS as an example, but any modern distribution should function in a similar way.

The Error_log Directive
Nginx uses a few different directives to control system logging. The one included in the core module is called "error_log".

Error_log Syntax

The "error_log" directive is used to handle logging general error messages. If you are coming from Apache, this is very similar to Apache"s "ErrorLog" directive.

The error_log directive takes the following syntax:

error_log log_file [ log_level ]
The "log_file" in the example specifies the file where the logs will be written. The "log_level" specifies the lowest level of logging that you would like to record.

Logging Levels

The error_log directive can be configured to log more or less information as required. The level of logging can be any one of the following:

emerg: Emergency situations where the system is in an unusable state.
alert: Severe situation where action is needed promptly.
crit: Important problems that need to be addressed.
error: An Error has occurred. Something was unsuccessful.
warn: Something out of the ordinary happened, but not a cause for concern.
notice: Something normal, but worth noting has happened.
info: An informational message that might be nice to know.
debug: Debugging information that can be useful to pinpoint where a problem is occurring.
The levels higher on the list are considered a higher priority. If you specify a level, the log will capture that level, and any level higher than the specified level.

For example, if you specify "error", the log will capture messages labeled "error", "crit", "alert", and "emerg".

We can see this directive in use if we look in the main configuration file:

sudo nano /etc/nginx/nginx.conf
. . .
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
. . .
If you do not want the error_log to log anything, you must send the output into "/dev/null":

error_log /dev/null crit;
The other logging directive that we see above, the "access_log" directive, will be discussed in the next section.

HttpLogModule Logging Directives
While the error_log directive is part of the core module, the access_log directive is part of the HttpLogModule. It provides the ability to customize logs.

There are a few other directives included with this module that assist in configuring custom logs.

The Log_format Directive

The log_format directive is used to describe the format of a log entry using plain text and variables.

There is one format that comes predefined with Nginx called "combined". This is a common format used by many servers.

This is what the combined format would look like if it was not defined internally and needed to be specified with the log_format directive:

log_format combined "$remote_addr - $remote_user [$time_local] "

        ""$request" $status $body_bytes_sent "
        ""$http_referer" "$http_user_agent"";

This definition spans multiple lines until it finds the semi-colon (;).

The pieces beginning with a dollar sign ($) indicate variables, while the characters like "-", "[", and "]" are interpreted literally.

The general syntax of the command is:

log_format format_name string_describing_formatting;
You can use variables supported by the core module to formulate your logging strings.

The Access_log Directive

The access_log directive uses some similar syntax to the error_log directive, but is more flexible. It is used to configure custom logging.

The access_log directive uses the following syntax:

access_log /path/to/log/location [ format_of_log buffer_size ];
The default value for access_log is the "combined" format we saw in the log_format section. You can use any format defined by a log_format definition.

The buffer size is the maximum size of data that Nginx will hold before writing it all to the log. You can also specify compression of the log file by adding "gzip" into the definition:

access_log location format gzip;
Unlike the error_log directive, if you do not want logging, you can turn it off by specifying:

access_log off;
It is not necessary to write to "/dev/null" in this case.

Log Rotation
As log files grow, it becomes necessary to manage the logging mechanisms to avoid filling up disk space. Log rotation is the process of switching out log files and possibly archiving old files for a set amount of time.

Nginx does not provide tools to manage log files, but it does include mechanisms that make log rotation simple.

Manual Log Rotation

If you would like to manually rotate your logs (or more likely, create a script to rotate them), you can do so by following the example in the Nginx wiki:

mv /path/to/access.log /path/to/access.log.0
kill -USR1 cat /var/run/nginx.pid
sleep 1
[ post-rotation processing of old log file ]
First, we move the current log to a new file for archiving. A common scheme is to name the most recent log file with a suffix of ".0", and then name older files with ".1", and so on.

The command that actually rotates the logs is "kill -USR1 /var/run/nginx.pid". This does not kill the Nginx process, but instead sends it a signal causing it to reload its log files. This will cause new requests to be logged to the refreshed log file.

The "/var/run/nginx.pid" file is where Nginx stores the master process"s pid. It is specified in the configuration file with a line that begins with "pid":

sudo nano /etc/nginx/nginx.conf
. . .
pid /path/to/pid/file;
. . .
After the rotation, we execute "sleep 1" to allow the process to complete the transfer. We can then zip the old files or do whatever post-rotation processes we would like.

Log Rotation with logrotate

The logrotate application is a simple program to rotate logs. It is installed on Ubuntu by default, and Nginx on Ubuntu comes with a custom logrotate script.

We can see the log rotation script by typing:

sudo nano /etc/logrotate.d/nginx
The first line of the file specifies the location that the subsequent lines will apply to. Keep this in mind if you switch the location of logging in the Nginx configuration files.

The rest of the file specifies that the logs will be rotate daily and that 52 older copies will be preserved. The general configuration of logrotate is outside of the scope of this article.

We can see that the "postrotate" section contains a command similar to the manual rotation mechanisms we were employing:

postrotate

[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`

endscript
This section tells Nginx to reload the log files once the rotation is complete.

Conclusion
Proper log configuration and management can save you time and energy in the event of a problem with your server. Having easy access to the information that will help you diagnose a problem can be the difference between a trivial fix and a persistent headache.

It is important to keep an eye on server logs in order to maintain a functional site and ensure that you are not exposing sensitive information. This guide should serve only as an introduction to your experience with logging.

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/39328.html

相關文章

  • java9 gc log參數遷移

    摘要:序本文主要研究一下參數的遷移。比如實例為,為,文件數為,每個文件,文件名為,為和輸出實例遷移舊版相關參數遷移舊版運行時參數遷移小結把的配置統一到了中,可以按照官方給出的新舊參數映射表進行遷移。 序 本文主要研究一下java9 gc log參數的遷移。 統一JVM及GC的Logging java9引進了一個統一的日志框架,對gc log的輸出進行了統一的配置。 相關JEP(JDK Enh...

    Kahn 評論0 收藏0
  • 渣渣的 ElasticSearch 源碼解析 —— 啟動流程(上)

    摘要:總結這篇文章主要先把大概啟動流程串通,因為篇幅較多所以拆開成兩篇,先不扣細節了,后面流程啟動文章寫完后我們再單一的扣細節。 關注我 showImg(https://segmentfault.com/img/remote/1460000012730965?w=258&h=258); 轉載請務必注明原創地址為:http://www.54tianzhisheng.cn/2018/08/11/...

    AZmake 評論0 收藏0
  • Pygame實戰:方塊連接世界,云游大好河山—《我的世界》已上線!確定不進來康康嘛?

    摘要:導語我的世界是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。主題我的世界版本圖片效果圖如下。 導語 《我的世界》是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。 ? 方塊連接世界,云游大好河山。 ...

    icattlecoder 評論0 收藏0

發表評論

0條評論

AndroidTraveler

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<