{"id":893,"date":"2024-08-10T12:07:40","date_gmt":"2024-08-10T12:07:40","guid":{"rendered":"https:\/\/linuxresellerwebhosting.in\/blog\/?p=893"},"modified":"2024-08-10T12:18:06","modified_gmt":"2024-08-10T12:18:06","slug":"know-about-log-rotation-in-nginx","status":"publish","type":"post","link":"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/","title":{"rendered":"How To Configure Logging and Log Rotation in Nginx on an Ubuntu VPS"},"content":{"rendered":"<p>Configure logging and log rotation in Nginx on an Ubuntu VPS involves setting up Nginx to log requests and errors, and then configuring a log rotation mechanism to manage log file sizes and retention.<\/p>\n<h2 id=\"prerequisites\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h2><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Prerequisites\" >Prerequisites<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Heres_a_step-by-step_guide_to_Configure_Logging_and_Log_Rotation_in_Nginx\" >Here&#8217;s a step-by-step guide to Configure Logging and Log Rotation in Nginx :<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Step_1_Configure_Nginx_Logging\" >Step 1: Configure Nginx Logging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Step_2_Configure_Log_Rotation_in_Nginx\" >Step 2: Configure Log Rotation in Nginx<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Step_3_Test_and_Apply_the_Configuration\" >Step 3: Test and Apply the Configuration<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxresellerwebhosting.in\/blog\/know-about-log-rotation-in-nginx\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>To follow this tutorial, you will need:<\/p>\n<ul>\n<li>One Ubuntu 22.04 server with a non-<strong>root<\/strong> <code>sudo<\/code>-enabled user with a firewall. Follow our Initial <a href=\"https:\/\/www.squarebrothers.com\/dedicated-server-india\/\" target=\"_blank\" rel=\"noopener\">Server<\/a> Setup to get started.<\/li>\n<li>Nginx installed on the server. Follow our How to Install Nginx on Ubuntu 22.04 tutorial to get it installed.<\/li>\n<\/ul>\n<p>With Nginx running on your Ubuntu 22.04 server, you\u2019re ready to begin.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Heres_a_step-by-step_guide_to_Configure_Logging_and_Log_Rotation_in_Nginx\"><\/span>Here&#8217;s a step-by-step guide to Configure Logging and Log Rotation in Nginx :<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Step_1_Configure_Nginx_Logging\"><\/span>Step 1: Configure Nginx Logging<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>1.Access Nginx Configuration:<\/strong> Open the Nginx configuration file for editing. This file is usually located in <code>\/etc\/nginx\/nginx.conf<\/code> or in a specific site configuration file in <code>\/etc\/nginx\/sites-available\/<\/code><\/p>\n<pre>sudo nano \/etc\/nginx\/nginx.conf<\/pre>\n<p><strong>2.Configure Access and Error Logs:<\/strong> Inside the <code>http<\/code> block, you can specify the location and format of the access and error logs:<\/p>\n<pre>http {\r\nlog_format main '$remote_addr - $remote_user [$time_local] \"$request\" '\r\n'$status $body_bytes_sent \"$http_referer\" '\r\n'\"$http_user_agent\" \"$http_x_forwarded_for\"';\r\n\r\naccess_log \/var\/log\/nginx\/access.log main;\r\nerror_log \/var\/log\/nginx\/error.log warn;\r\n\r\n# Other configurations...\r\n}<\/pre>\n<p><code>access_log<\/code> specifies the file where access logs are stored and the log format.<\/p>\n<p><code>error_log<\/code> specifies the file where error logs are stored and the log level.<\/p>\n<p><strong>3.Save and Exit:<\/strong> Save the changes and exit the text editor.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Step_2_Configure_Log_Rotation_in_Nginx\"><\/span>Step 2: Configure Log Rotation in Nginx<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Log rotation is usually managed by the <code>logrotate<\/code> utility on Ubuntu.<\/p>\n<ol>\n<li><strong>Create a Logrotate Configuration File for Nginx:<\/strong> Create a new logrotate configuration file for Nginx in <code>\/etc\/logrotate.d\/<\/code>.<\/li>\n<\/ol>\n<pre>sudo nano \/etc\/logrotate.d\/nginx<\/pre>\n<p><strong>Add Log Rotation Rules:<\/strong><\/p>\n<p>Add the following configuration to manage the rotation of Nginx logs:<\/p>\n<pre>\/var\/log\/nginx\/*.log {\r\ndaily\r\nmissingok\r\nrotate 14\r\ncompress\r\ndelaycompress\r\nnotifempty\r\ncreate 0640 www-data adm\r\nsharedscripts\r\npostrotate\r\n[ -f \/var\/run\/nginx.pid ] &amp;&amp; kill -USR1 `cat \/var\/run\/nginx.pid`\r\nendscript\r\n}<\/pre>\n<ul>\n<li><code>daily<\/code> rotates logs daily.<\/li>\n<li><code>missingok<\/code> ignores errors if the log file is missing.<\/li>\n<li><code>rotate 14<\/code> keeps 14 days&#8217; worth of logs.<\/li>\n<li><code>compress<\/code> compresses the rotated logs to save space.<\/li>\n<li><code>delaycompress<\/code> delays compression until the next rotation cycle.<\/li>\n<li><code>notifempty<\/code> skips rotation if the log file is empty.<\/li>\n<li><code>create<\/code> sets permissions for the new log file.<\/li>\n<li><code>postrotate<\/code> contains commands to run after the log file is rotated. The <code>kill -USR1<\/code> command tells Nginx to reopen its log files.<\/li>\n<\/ul>\n<p><strong>Save and Exit:<\/strong> Save the changes and exit the text editor.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Step_3_Test_and_Apply_the_Configuration\"><\/span>Step 3: Test and Apply the Configuration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li><strong>Test Nginx Configuration:<\/strong> Before applying the changes, test the Nginx configuration to ensure there are no errors.<\/li>\n<\/ol>\n<pre>sudo nginx -t<\/pre>\n<ul>\n<li>If the configuration test is successful, you will see a message like <code>syntax is okay<\/code> and <code>test is successful<\/code>.<\/li>\n<li><strong>Restart Nginx:<\/strong> Restart Nginx to apply the changes.<\/li>\n<\/ul>\n<pre>sudo systemctl restart nginx<\/pre>\n<p><strong>Test Log Rotation:<\/strong> You can manually test log rotation to ensure it\u2019s working correctly.<\/p>\n<pre>sudo logrotate -f \/etc\/logrotate.d\/nginx<\/pre>\n<p>This command forces logrotate to perform a rotation. After running this command, check the log directory to verify that the logs have been rotated.<\/p>\n<p>This section tells Nginx to reload the log files once the rotation is complete.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Proper log configuration and management can save you time and energy in the event of a problem with your server. Having access to the information that will help you diagnose a problem can be the difference between a trivial fix and a persistent headache.<\/p>\n<p>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 serves only as an introduction to your experience with logging. You can learn more general tips in our tutorial on <a href=\"https:\/\/linuxresellerwebhosting.in\/blog\/remove-package-in-ubuntu-easy-way\/\">How to Uninstall or Remove Package in Ubuntu.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configure logging and log rotation in Nginx on an Ubuntu VPS involves setting up Nginx to log requests and errors,<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[34,2,4],"tags":[53,52,51],"class_list":["post-893","post","type-post","status-publish","format-standard","hentry","category-logs","category-linux","category-vps","tag-configure","tag-log","tag-nginx"],"_links":{"self":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=893"}],"version-history":[{"count":9,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/893\/revisions"}],"predecessor-version":[{"id":902,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/893\/revisions\/902"}],"wp:attachment":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}