<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="https://publishpress.com/"
	>

<channel>
	<title>nginx Archives - Tomoshare</title>
	<atom:link href="https://blog.tomosia.com.vn/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.tomosia.com.vn/tag/nginx/</link>
	<description>Kênh chia sẻ kiến thức Tomosia Việt Nam</description>
	<lastBuildDate>Wed, 22 Nov 2023 01:55:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.tomosia.com.vn/wp-content/uploads/2023/09/cropped-icon-32x32.png</url>
	<title>nginx Archives - Tomoshare</title>
	<link>https://blog.tomosia.com.vn/tag/nginx/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Logrotate: Compress và push file log lên S3</title>
		<link>https://blog.tomosia.com.vn/logrotate-compress-va-push-file-log-len-s3/</link>
					<comments>https://blog.tomosia.com.vn/logrotate-compress-va-push-file-log-len-s3/#comments</comments>
		
		<dc:creator><![CDATA[Thuc Phan]]></dc:creator>
		<pubDate>Wed, 22 Nov 2023 01:54:57 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Solution]]></category>
		<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[logrotate]]></category>
		<guid isPermaLink="false">https://blog.tomosia.com.vn/?p=1939</guid>

					<description><![CDATA[<p>Log files giúp bạn hiểu rõ về lịch sử hoạt động của máy chủ nhưng chúng có thể&#8230;</p>
<p>The post <a href="https://blog.tomosia.com.vn/logrotate-compress-va-push-file-log-len-s3/">Logrotate: Compress và push file log lên S3</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Log files giúp bạn hiểu rõ về lịch sử hoạt động của máy chủ nhưng chúng có thể trở nên nặng nề và khó quản lý</p>



<p>Logrotate không chỉ giúp giảm kích thước file log một cách tự động mà còn có thể nén file để tiết kiệm không gian lưu trữ</p>



<p>Trong bài viết này, chúng ta sẽ tìm hiểu cách Logrotate được cấu hình để tự động nén và đẩy file lên S3, giúp bạn duy trì hệ thống được sạch sẽ và dễ quản lý hơn</p>



<h2 id="config" class="wp-block-heading">Config</h2>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:8.4296875px;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#333545;color:#ebebe6">ShellScript</span><span role="button" tabindex="0" data-code="cd /etc/logrotate.d
touch rails_log_rotation" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #97E1F1">cd</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">/etc/logrotate.d</span></span>
<span class="line"><span style="color: #62E884">touch</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">rails_log_rotation</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:16.859375px;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#333545;color:#ebebe6">ShellScript</span><span role="button" tabindex="0" data-code="/home/ubuntu/path-to-deploy/current/log/production.log {
  daily
  dateext
  missingok
  rotate 90
  compress
  copytruncate
  lastaction
    aws s3 sync /home/ubuntu/path-to-deploy/current/log/ s3://path-to-bucket-name --exclude &quot;*&quot; --include &quot;production.log*.gz&quot; 2&gt;&amp;1
  endscript
  create 0644 root root
  su root root
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #62E884">/home/ubuntu/path-to-deploy/current/log/production.log</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">{</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">daily</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">dateext</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">missingok</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">rotate</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">90</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">compress</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">copytruncate</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">lastaction</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #62E884">aws</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">s3</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">sync</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">/home/ubuntu/path-to-deploy/current/log/</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">s3://path-to-bucket-name</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">--exclude</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">*</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">--include</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">production.log*.gz</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">2&gt;&amp;1</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">endscript</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">create</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0644</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">root</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">root</span></span>
<span class="line"><span style="color: #F6F6F4">  </span><span style="color: #62E884">su</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">root</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">root</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<p><code><strong>home/ubuntu/path-to-deploy/current/log/production.log</strong></code></p>



<ul class="wp-block-list">
<li>Link đến file log cần được quản lý bởi logrotate</li>
</ul>



<p><code><strong>daily</strong></code></p>



<ul class="wp-block-list">
<li>Thực hiện xoay vòng log hàng ngày (được thực hiện bằng cron-job)</li>
</ul>



<ul class="wp-block-list">
<li>Có nghĩa là file <code>/home/ubuntu/path-to-deploy/current/log/production.log</code> sẽ được làm mới mỗi ngày</li>
</ul>



<ul class="wp-block-list">
<li>Check cron configuration</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:8.4296875px;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#333545;color:#ebebe6">ShellScript</span><span role="button" tabindex="0" data-code="cd /etc/
ls | grep cron
cron.d
cron.daily
cron.hourly
cron.monthly
cron.weekly
crontab" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #97E1F1">cd</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">/etc/</span></span>
<span class="line"><span style="color: #62E884">ls</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">|</span><span style="color: #F6F6F4"> </span><span style="color: #62E884">grep</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">cron</span></span>
<span class="line"><span style="color: #62E884">cron.d</span></span>
<span class="line"><span style="color: #62E884">cron.daily</span></span>
<span class="line"><span style="color: #62E884">cron.hourly</span></span>
<span class="line"><span style="color: #62E884">cron.monthly</span></span>
<span class="line"><span style="color: #62E884">cron.weekly</span></span>
<span class="line"><span style="color: #62E884">crontab</span></span></code></pre></div>



<p><code><strong>dateext</strong></code></p>



<ul class="wp-block-list">
<li>Thêm ngày vào filename của file log được xoay vòng để đảm bảo không bị trùng tên</li>
</ul>



<p><code><strong>missingok</strong></code></p>



<ul class="wp-block-list">
<li>Cho phép logrotate bỏ qua nếu không tìm thấy file</li>
</ul>



<p><code><strong>rotate 90</strong></code></p>



<ul class="wp-block-list">
<li>Giữ lại tối đa 90 file logs đã quay vòng</li>



<li>Trong trường hợp này, có thể hiểu là cho phép lưu trữ file logs của 90 ngày gần nhất</li>
</ul>



<p><code><strong>compress</strong></code></p>



<ul class="wp-block-list">
<li>Nén file log đã xoay vòng để tiết kiệm không gian lưu trữ</li>
</ul>



<p><code><strong>copytruncate</strong></code></p>



<ul class="wp-block-list">
<li>Sao chép nội dung từ file cũ vào file mới, sau đó quay lại đầu file cũ và cắt bớt nó. Điều này giúp logrotate có thể xoay vòng mà không làm ảnh hưởng đến ứng dụng đang chạy</li>
</ul>



<p><code><strong>lastaction</strong></code> và <code><strong>endscript</strong></code> </p>



<ul class="wp-block-list">
<li>Bọc một đoạn code hoặc command được thực thi sau khi xoay vòng</li>



<li>Trong trường hợp này, sử dụng AWS CLI để đồng bộ hóa các file logs đã xoay vòng với Amazon S3</li>



<li>Loại trừ tất cả các files, chỉ bao gồm các file logs đã nén <code>(production.log*.gz)</code></li>



<li>Lưu ý
<ul class="wp-block-list">
<li>Bạn cần thay thế <code>path-to-bucket-name</code> bằng đường dẫn thích hợp đến S3 Bucket của bạn</li>



<li>EC2 phải tương tác được với S3 Bucket của bạn (có thể dùng <code>aws configure</code> hoặc <code>attached IAM role</code> đều được)</li>
</ul>
</li>
</ul>



<p><code><strong>create 0644 root root</strong></code></p>



<ul class="wp-block-list">
<li>Tạo một file log mới với quyền truy cập <code>0644</code> và chủ sở hữu là <code>root</code></li>
</ul>



<p><code><strong>su root root</strong></code></p>



<ul class="wp-block-list">
<li>Chuyển quyền sở hữu của file sang <code>root</code> sau khi tạo mới để đảm bảo an toàn và bảo mật</li>
</ul>



<p><strong>Tóm lại</strong></p>



<ul class="wp-block-list">
<li>Config này giúp bạn quản lý và tự động xoay vòng file <code>production.log</code> mỗi ngày</li>



<li>Nén file để tiết kiệm không gian và đồng thời sao chép lên Amazon S3 để lưu trữ</li>
</ul>



<p><strong>Manual test config</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:8.4296875px;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#333545;color:#ebebe6">ShellScript</span><span role="button" tabindex="0" data-code="sudo logrotate -f /etc/logrotate.conf" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #62E884">sudo</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">logrotate</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">-f</span><span style="color: #F6F6F4"> </span><span style="color: #E7EE98">/etc/logrotate.conf</span></span></code></pre></div>
<p>The post <a href="https://blog.tomosia.com.vn/logrotate-compress-va-push-file-log-len-s3/">Logrotate: Compress và push file log lên S3</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.tomosia.com.vn/logrotate-compress-va-push-file-log-len-s3/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Hướng dẫn cấu hình Basic Authentication trên Nginx</title>
		<link>https://blog.tomosia.com.vn/huong-dan-cau-hinh-basic-authentication-tren-nginx/</link>
					<comments>https://blog.tomosia.com.vn/huong-dan-cau-hinh-basic-authentication-tren-nginx/#comments</comments>
		
		<dc:creator><![CDATA[Việt Nguyễn]]></dc:creator>
		<pubDate>Tue, 26 Sep 2023 21:43:32 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[basic auth]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[centos]]></category>
		<guid isPermaLink="false">https://blog.tomosia.com.vn/?p=45</guid>

					<description><![CDATA[<p>Trong quá trình phát triển web, có những lúc chúng ta sẽ cần phải giới hạn người dùng&#8230;</p>
<p>The post <a href="https://blog.tomosia.com.vn/huong-dan-cau-hinh-basic-authentication-tren-nginx/">Hướng dẫn cấu hình Basic Authentication trên Nginx</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong quá trình phát triển web, có những lúc chúng ta sẽ cần phải giới hạn người dùng truy cập đến website của mình và để giới hạn chúng ta có thể yêu cầu người dùng xác thực qua&nbsp;<code>tài khoản</code>&nbsp;và&nbsp;<code>mật khẩu</code>.</p>



<p>Bài viết này sẽ hướng dẫn cho bạn các bước để cấu hình <a rel="noreferrer noopener" href="https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/" target="_blank"><strong>Basic Authentication</strong></a> bảo vệ server <a rel="noreferrer noopener" href="https://www.nginx.com/" target="_blank"><strong>Nginx</strong></a> chạy trên môi trường CentOS và Ubuntu Server.</p>



<h2 id="1-cai-dat-htpasswd" class="wp-block-heading">1. Cài đặt htpasswd</h2>



<p>Tiến hành cài gói <strong>apache2-utils</strong> (Debian, Ubuntu) hoặc <strong>httpd-tools</strong> (CentOS)</p>



<p><strong>Ubuntu, Debian:</strong></p>



<pre class="wp-block-preformatted">sudo apt-get update 
sudo apt-get install apache2-utils </pre>



<p><strong>CentOS</strong>:</p>



<pre class="wp-block-preformatted">sudo yum install httpd-tools </pre>



<h2 id="2-tao-password-file" class="wp-block-heading">2. Tạo Password File</h2>



<p>Sau khi htpasswd được cài đặt, hãy tạo file mật khẩu bằng câu lệnh sau với flag -c nếu file <strong>&#8220;.htpasswd&#8221;</strong> chưa tồn tại:</p>



<pre class="wp-block-preformatted">sudo htpasswd -c /etc/nginx/.htpasswd username </pre>



<p>Thay thế <strong>&#8220;username&#8221;</strong> với tên đăng nhập bạn mong muốn. Sẽ có prompt chờ bạn nhập thông tin cho user trên</p>



<p>Lưu ý: nếu đã có file  <strong>/etc/nginx/.htpass</strong>wd rồi thì KHÔNG sử dụng <strong>flag -c</strong></p>



<pre class="wp-block-preformatted">sudo htpasswd /etc/nginx/.htpasswd tomosia-admin</pre>



<p>Nếu user tomosia-admin đã tồn tại thì lệnh trên sẽ cập nhật lại password</p>



<h2 id="3-cau-hinh-xac-thuc-mat-khau-cho-nginx" class="wp-block-heading">3. Cấu hình xác thực mật khẩu cho Nginx</h2>



<p>Mở tệp cấu hình nginx của bạn, thường nó sẽ nằm ở đường dẫn <strong>/etc/nginx/nginx.conf</strong> hoặc cấu hình cho 1 site nhất định tại <strong>/etc/nginx/sites-available/example.com</strong> hoặc <strong>/etc/nginx/conf.d/example.com</strong>. Sau đó thêm dòng sau vào file cấu hình</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="auth_basic &quot;Restricted Access&quot;;
auth_basic_user_file /etc/nginx/.htpasswd;" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #FF79C6">auth_basic </span><span style="color: #F1FA8C">&quot;Restricted Access&quot;</span><span style="color: #F8F8F2">;</span></span>
<span class="line"><span style="color: #FF79C6">auth_basic_user_file </span><span style="color: #F8F8F2">/etc/nginx/.htpasswd;</span></span></code></pre></div>



<p>Nếu muốn thiết lập basic auth cho toàn bộ website trên server thì cần thêm vào khối <strong>http</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="http {
   ...
   auth_basic &quot;Restricted Access!&quot;; 
   auth_basic_user_file /etc/nginx/.htpasswd;
   ...
}" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #FF79C6">http</span><span style="color: #F8F8F2"> {</span></span>
<span class="line"><span style="color: #F8F8F2">   ...</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #FF79C6"> auth_basic </span><span style="color: #F1FA8C">&quot;Restricted Access!&quot;</span><span style="color: #F8F8F2">; </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #FF79C6"> auth_basic_user_file </span><span style="color: #F8F8F2">/etc/nginx/.htpasswd;</span></span>
<span class="line"><span style="color: #F8F8F2">   ...</span></span>
<span class="line"><span style="color: #F8F8F2">}</span></span></code></pre></div>



<p>Nếu muốn thiết lập cho một website hoặc domain nhất định cần thêm vào khối <strong>server</strong> tương ứng</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="server {
    ...
    auth_basic &quot;Restricted Access!&quot;;
    auth_basic_user_file /etc/nginx/.htpasswd;
    ...
}" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #FF79C6">server</span><span style="color: #F8F8F2"> {</span></span>
<span class="line"><span style="color: #F8F8F2">    ...</span></span>
<span class="line"><span style="color: #F8F8F2">   </span><span style="color: #FF79C6"> auth_basic </span><span style="color: #F1FA8C">&quot;Restricted Access!&quot;</span><span style="color: #F8F8F2">;</span></span>
<span class="line"><span style="color: #F8F8F2">   </span><span style="color: #FF79C6"> auth_basic_user_file </span><span style="color: #F8F8F2">/etc/nginx/.htpasswd;</span></span>
<span class="line"><span style="color: #F8F8F2">    ...</span></span>
<span class="line"><span style="color: #F8F8F2">}</span></span></code></pre></div>



<p>Nếu muốn thiết lập cho một location nhất định hãy thêm vào khối <strong>location</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="location /swagger-ui/ {
    ...
    auth_basic &quot;Restricted Access!&quot;;
    auth_basic_user_file /etc/nginx/.htpasswd;
    ...
}" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #FF79C6">location</span><span style="color: #F8F8F2"> /swagger-ui/ {</span></span>
<span class="line"><span style="color: #F8F8F2">    ...</span></span>
<span class="line"><span style="color: #F8F8F2">   </span><span style="color: #FF79C6"> auth_basic </span><span style="color: #F1FA8C">&quot;Restricted Access!&quot;</span><span style="color: #F8F8F2">;</span></span>
<span class="line"><span style="color: #F8F8F2">   </span><span style="color: #FF79C6"> auth_basic_user_file </span><span style="color: #F8F8F2">/etc/nginx/.htpasswd;</span></span>
<span class="line"><span style="color: #F8F8F2">    ...</span></span>
<span class="line"><span style="color: #F8F8F2">}</span></span></code></pre></div>



<h2 id="4-restart-nginx" class="wp-block-heading">4. Restart Nginx</h2>



<pre class="wp-block-preformatted">sudo service nginx reload #debian/ubuntu
sudo systemctl restart nginx #redhat/centos</pre>



<h2 id="5-kiem-tra" class="wp-block-heading">5. Kiểm tra</h2>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="793" height="468" src="http://blog.tomosia.com.vn/wp-content/uploads/2023/10/image.png" alt="" class="wp-image-282" srcset="https://blog.tomosia.com.vn/wp-content/uploads/2023/10/image.png 793w, https://blog.tomosia.com.vn/wp-content/uploads/2023/10/image-300x177.png 300w, https://blog.tomosia.com.vn/wp-content/uploads/2023/10/image-768x453.png 768w, https://blog.tomosia.com.vn/wp-content/uploads/2023/10/image-380x224.png 380w" sizes="(max-width: 793px) 100vw, 793px" /></figure>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>
<p>The post <a href="https://blog.tomosia.com.vn/huong-dan-cau-hinh-basic-authentication-tren-nginx/">Hướng dẫn cấu hình Basic Authentication trên Nginx</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.tomosia.com.vn/huong-dan-cau-hinh-basic-authentication-tren-nginx/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
