<?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>Web server Archives - Tomoshare</title>
	<atom:link href="https://blog.tomosia.com.vn/tag/web-server/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.tomosia.com.vn/tag/web-server/</link>
	<description>Kênh chia sẻ kiến thức Tomosia Việt Nam</description>
	<lastBuildDate>Mon, 22 Dec 2025 01:11:41 +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>Web server Archives - Tomoshare</title>
	<link>https://blog.tomosia.com.vn/tag/web-server/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Hướng dẫn từng bước thiết lập Let&#8217;s Encrypt SSL/TLS cho Nginx trên Docker.</title>
		<link>https://blog.tomosia.com.vn/huong-dan-tung-buoc-thiet-lap-lets-encrypt-ssl-tls-cho-nginx-tren-docker/</link>
					<comments>https://blog.tomosia.com.vn/huong-dan-tung-buoc-thiet-lap-lets-encrypt-ssl-tls-cho-nginx-tren-docker/#respond</comments>
		
		<dc:creator><![CDATA[linh phan]]></dc:creator>
		<pubDate>Mon, 22 Dec 2025 01:11:37 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Web server]]></category>
		<guid isPermaLink="false">https://blog.tomosia.com.vn/?p=3683</guid>

					<description><![CDATA[<p>Trong bài viết này, mình sẽ hướng dẫn từng bước cách thiết lập HTTPS với Let&#8217;s Encrypt cho&#8230;</p>
<p>The post <a href="https://blog.tomosia.com.vn/huong-dan-tung-buoc-thiet-lap-lets-encrypt-ssl-tls-cho-nginx-tren-docker/">Hướng dẫn từng bước thiết lập Let&#8217;s Encrypt SSL/TLS cho Nginx trên Docker.</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong bài viết này, mình sẽ hướng dẫn <strong>từng bước</strong> cách thiết lập <strong>HTTPS với Let&#8217;s Encrypt</strong> cho <strong>Nginx chạy trên Docker</strong>, theo hướng <strong>thực tế – dễ vận hành – dùng được cho production</strong>.</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p><strong>Nội dung bao gồm:</strong><br>・Thiết lập Nginx chạy HTTPS trên Docker<br>・Cấp SSL/TLS certificate bằng Certbot<br>・Tự động gia hạn certificate<br>・Redirect HTTP → HTTPS<br>・Áp dụng các security headers cơ bản</p>
</div></div>



<p><strong>Yêu cầu</strong><br>・Docker đã cài đặt<br>・Docker Compose đã cài đặt<br>・Quyền quản trị hệ thống<br>・Domain đã trỏ về IP server<br>・Port 80 và 443 mở và trỏ về server</p>



<p><br><strong>Thiết lập Nginx Webserver với Let&#8217;s Encrypt trên Docker</strong><br>Quy trình thiết lập gồm các bước sau:・Tạo Docker Compose file<br>・Cấu hình Nginx server<br>・Tạo script setup SSL<br>・Chạy Certbot client<br>・Tự động gia hạn certificate</p>



<h2 id="buoc-1-tao-cau-truc-thu-muc-va-env" class="wp-block-heading">Bước 1: Tạo cấu trúc thư mục và <code>.env</code></h2>



<p>Tạo các thư mục cần thiết trong dự án:</p>



<pre class="wp-block-code has-d-4-d-4-d-4-color has-text-color has-875-rem-font-size"><code>mkdir -p docker/nginx/etc/nginx/templates
mkdir -p docker/nginx/etc/nginx/conf
mkdir -p docker/nginx/certs</code></pre>



<p>Cấu trúc thư mục:</p>



<pre class="wp-block-code"><code>docker/
└── nginx/
    ├── setup-ssl.sh
    ├── entrypoint.sh
    ├── etc/nginx/
    │   ├── nginx.conf
    │   ├── conf/
    │   └── templates/
    │       ├── default.conf.template
    │       ├── https.conf.template
    │       └── upstream.conf.template
    └── certs/              # Thư mục chứa certificates (gitignored)</code></pre>



<p><strong>File Structure</strong><br>・<code>entrypoint.sh</code>: Script tự động generate nginx configs từ templates<br>・<code>setup-ssl.sh</code>: Script tạo SSL certificate trên host (chạy ngoài container)<br>・<code>templates/</code>: Nginx config templates<br>・<code>https.conf.template</code>: HTTPS config cho domain<br>・<code>upstream.conf.template</code>: PHP-FPM upstream config<br>・<code>certs/</code>: Thư mục chứa Let&#8217;s Encrypt certificates trên host (gitignored, mounted vào container)</p>



<p><strong>Environment Variables</strong></p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Biến</th><th>Mô tả</th><th>Default</th></tr></thead><tbody><tr><td><code>DOCKER_NGINX_EXTERNAL_PORT</code></td><td>HTTP external port</td><td><code>8000</code></td></tr><tr><td><code>DOCKER_NGINX_HTTPS_PORT</code></td><td>HTTPS external port</td><td><code>8443</code></td></tr><tr><td><code>HTTPS_DOMAIN</code></td><td>Domain dùng HTTPS</td><td><code>example.com</code></td></tr><tr><td><code>ENABLE_HTTPS</code></td><td>Bật HTTPS</td><td><code>true</code></td></tr></tbody></table></figure>



<h2 id="buoc-2-tao-dockerfile-cho-nginx" class="wp-block-heading">Bước 2: Tạo Dockerfile cho Nginx</h2>



<p>Tạo file docker/Dockerfile với multi-stage build:</p>



<pre class="wp-block-code"><code>FROM nginx:1.28.0-alpine as nginx

WORKDIR /var/www/html
COPY docker/nginx/ /
COPY docker/nginx/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 80 443

ENTRYPOINT &#91;"/usr/local/bin/entrypoint.sh"]
CMD &#91;"nginx", "-g", "daemon off;"]</code></pre>



<h2 id="buoc-3-tao-docker-compose-file" class="wp-block-heading">Bước 3: Tạo Docker Compose file</h2>



<p>Tạo file docker-compose.yml để chạy Nginx với HTTPS:</p>



<pre class="wp-block-code"><code>services:
  nginx:
    build:
      context: .
      dockerfile: docker/Dockerfile
      target: nginx
    environment:
      ENABLE_BASIC_AUTH: "${ENABLE_BASIC_AUTH-false}"
      PHP_FPM_HOST: app
      PHP_FPM_PORT: 9000
      ENABLE_HTTP_LOCALHOST: "false"
      ENABLE_HTTPS: "true"
      HTTPS_DOMAIN: "${HTTPS_DOMAIN-example.com}"
    ports:
      - "${DOCKER_NGINX_EXTERNAL_PORT-8000}:80"
      - "${DOCKER_NGINX_HTTPS_PORT-8443}:443"
    depends_on:
      - app
      - certbot
    volumes:
      - ".:/var/www/html"
      - "./docker/nginx/certs:/etc/letsencrypt"

  certbot:
    image: certbot/certbot:latest
    volumes:
      - "./docker/nginx/certs:/etc/letsencrypt"
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h &amp; wait $${!}; done;'"

  app:
    build:
      context: .
      dockerfile: docker/Dockerfile
      target: app
    volumes:
      - ".:/var/www/html"
    environment:
      PHP_OPCACHE_VALIDATE_TIMESTAMPS: 1
    command: &#91;"development"]</code></pre>



<p>Giải thích:<br><code>webserver</code> (nginx): container Nginx với HTTPS<br><code>certbot</code>: container tự động gia hạn certificate<br><code>app</code>: container Laravel application<br>Volume <code>./docker/nginx/certs:/etc/letsencrypt</code>: chia sẻ certificates giữa containers</p>



<h2 id="buoc-4-tao-file-cau-hinh-nginx" class="wp-block-heading">Bước 4: Tạo file cấu hình Nginx</h2>



<p><strong>4.1. Tạo template HTTP (cho Let&#8217;s Encrypt validation)</strong><br>Tạo file <code>docker/nginx/etc/nginx/templates/default.conf.template</code>:</p>



<pre class="wp-block-code"><code>server {
    listen 80;
    server_name _;

    root /var/www/html/public;
    index index.php;

    client_max_body_size 40m;

    access_log /dev/stdout;
    error_log /dev/stderr;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    proxy_hide_header X-Powered-By;

    charset utf-8;

    location = /health {
        return 200 'OK!';
        add_header Content-Type text/plain;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location / {
        auth_basic $basic_auth;
        auth_basic_user_file /etc/nginx/.htpasswd;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        set $path_info $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php-fpm;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}</code></pre>



<p>Lưu ý: Block <code>location ~ /\.(?!well-known).*</code> chặn các file ẩn nhưng cho phép <code>.well-known</code> để Let&#8217;s Encrypt validate.</p>



<p><strong>4.2. Tạo template HTTPS</strong><br>Tạo file <code>docker/nginx/etc/nginx/templates/https.conf.template</code>:</p>



<pre class="wp-block-code"><code># HTTPS server for ${HTTPS_DOMAIN}
server {
    listen 443 ssl;
    http2 on;
    server_name ${HTTPS_DOMAIN};

    root /var/www/html/public;
    index index.php;

    # SSL configuration
    ssl_certificate /etc/letsencrypt/live/${HTTPS_DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${HTTPS_DOMAIN}/privkey.pem;

    client_max_body_size 40m;

    access_log /dev/stdout;
    error_log /dev/stderr;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    proxy_hide_header X-Powered-By;

    charset utf-8;

    location = /health {
        return 200 'OK!';
        add_header Content-Type text/plain;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location / {
        auth_basic $basic_auth;
        auth_basic_user_file /etc/nginx/.htpasswd;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        set $path_info $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php-fpm;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# Redirect HTTP to HTTPS for ${HTTPS_DOMAIN}
server {
    listen 80;
    server_name ${HTTPS_DOMAIN};

    location / {
        return 301 https://$host$request_uri;
    }
}</code></pre>



<p>Giải thích:<br>Server block đầu: HTTPS trên port 443 với HTTP/2<br>Server block thứ hai: redirect HTTP → HTTPS<br>Security headers: HSTS, X-Frame-Options, X-Content-Type-Options</p>



<h2 id="buoc-5-tao-entrypoint-script" class="wp-block-heading">Bước 5: Tạo Entrypoint script</h2>



<p>Tạo file <code>docker/nginx/entrypoint.sh</code> để tự động generate config từ template:</p>



<pre class="wp-block-code"><code>#!/bin/sh
set -e

# Set BASIC_AUTH_VALUE based on ENABLE_BASIC_AUTH
if &#91; "$ENABLE_BASIC_AUTH" = "true" ]; then
    export BASIC_AUTH_VALUE="Administrator's Area"
else
    export BASIC_AUTH_VALUE="off"
fi

# Generate map config from template
envsubst '$$BASIC_AUTH_VALUE' &lt; /etc/nginx/templates/map.conf.template &gt; /etc/nginx/conf.d/map.conf

# Generate upstream config
envsubst '$$PHP_FPM_HOST $$PHP_FPM_PORT' &lt; /etc/nginx/templates/upstream.conf.template &gt; /etc/nginx/conf.d/upstream.conf

# Generate nginx configs from templates
if &#91; "$ENABLE_HTTP_LOCALHOST" = "true" ]; then
    envsubst '$$PHP_FPM_HOST $$PHP_FPM_PORT' &lt; /etc/nginx/templates/localhost.conf.template &gt; /etc/nginx/conf.d/server-localhost.conf
fi

# Handle HTTPS setup
if &#91; "$ENABLE_HTTPS" = "true" ] &amp;&amp; &#91; -n "$HTTPS_DOMAIN" ]; then
    # Create directories if they don't exist
    mkdir -p /etc/letsencrypt/live/${HTTPS_DOMAIN}

    if &#91; -f "/etc/letsencrypt/live/${HTTPS_DOMAIN}/fullchain.pem" ]; then
        envsubst '$$PHP_FPM_HOST $$PHP_FPM_PORT $$HTTPS_DOMAIN' &lt; /etc/nginx/templates/https.conf.template &gt; /etc/nginx/conf.d/server-https.conf
    fi
fi

# Execute the main command
exec "$@"</code></pre>



<p>Script này:<br>・Tạo config từ template dựa trên biến môi trường<br>・Chỉ tạo HTTPS config nếu certificate tồn tại<br>・Dùng <code>envsubst</code> để thay thế biến</p>



<h2 id="buoc-6-tao-script-setup-ssl" class="wp-block-heading">Bước 6: Tạo script setup SSL</h2>



<p>Tạo file <code>docker/nginx/setup-ssl.sh</code> để tạo certificate trên host:</p>



<pre class="wp-block-code"><code>#!/bin/bash
set -e

# Get script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE&#91;0]}")" &amp;&amp; pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." &amp;&amp; pwd)"

# Configuration
DOMAIN="${HTTPS_DOMAIN:-example.com}"
CERT_DIR="${PROJECT_ROOT}/docker/nginx/certs"

echo "Setting up SSL certificate for ${DOMAIN} on host..."
echo "Certificate will be saved to: ${CERT_DIR}"

# Create directories on host
mkdir -p "${CERT_DIR}"

# Check if certificate already exists
if &#91; -f "${CERT_DIR}/live/${DOMAIN}/fullchain.pem" ]; then
    echo "Certificate already exists for ${DOMAIN}"
    echo "Location: ${CERT_DIR}/live/${DOMAIN}/"
    exit 0
fi

cd "${PROJECT_ROOT}"

# Request certificate using docker (certificate saved on host, mounted into container)
echo "Requesting certificate (port 80 must be available)..."
docker run --rm \
    -v "${CERT_DIR}:/etc/letsencrypt" \
    -p 80:80 \
    certbot/certbot certonly \
    --standalone \
    --agree-tos \
    -d "${DOMAIN}"

echo ""
echo "Certificate created successfully on host!"
echo "Location: ${CERT_DIR}/live/${DOMAIN}/"
echo "Files: fullchain.pem, privkey.pem"
echo ""
echo "Certificate will be automatically mounted into nginx container."
echo "You can now run: docker-compose up -d"</code></pre>



<p>Lưu ý: Script này chạy trên host, không trong container. Certificate được lưu trên host và mount vào container.</p>



<h2 id="buoc-7-chay-certbot-de-lay-certificate" class="wp-block-heading">Bước 7: Chạy Certbot để lấy certificate</h2>



<p><strong>7.1. Test với dry-run (khuyến nghị)</strong><br>Trước khi lấy certificate thật, test với <code>--dry-run</code>:</p>



<pre class="wp-block-code"><code>chmod +x docker/nginx/setup-ssl.sh

# Test với dry-run
docker run --rm \
    -v "$(pwd)/docker/nginx/certs:/etc/letsencrypt" \
    -p 80:80 \
    certbot/certbot certonly \
    --standalone \
    --dry-run \
    --agree-tos \
    -d your-domain.com</code></pre>



<p>Lưu ý: Thay <code>your-domain.com</code> bằng domain của bạn.</p>



<p><strong>7.2. Lấy certificate thật</strong></p>



<p>Nếu dry-run thành công, chạy script để lấy certificate:</p>



<pre class="wp-block-code"><code>HTTPS_DOMAIN=your-domain.com ./docker/nginx/setup-ssl.sh</code></pre>



<p>Hoặc dùng domain mặc định:</p>



<pre class="wp-block-code"><code>./docker/nginx/setup-ssl.sh</code></pre>



<p>Quá trình:</p>



<ol class="wp-block-list">
<li>Script kiểm tra certificate đã tồn tại chưa</li>



<li>Nếu chưa, chạy Certbot với &#8211;standalone mode</li>



<li>Certbot bind port 80 để Let&#8217;s Encrypt validate</li>



<li>Certificate được lưu tại ./docker/nginx/certs/live/your-domain.com/<br><br>Lưu ý:<br>・Đảm bảo port 80 mở và trỏ về server.<br>・Nếu Nginx đang chạy trên port 80, tạm dừng trước khi chạy script.</li>
</ol>



<h2 id="buoc-8-khoi-dong-docker-compose" class="wp-block-heading">Bước 8: Khởi động Docker Compose</h2>



<p>Sau khi có certificate, khởi động services:</p>



<pre class="wp-block-code"><code>docker-compose up -d</code></pre>



<p>Kiểm tra containers đang chạy:</p>



<pre class="wp-block-code"><code>docker-compose ps</code></pre>



<p>Kiểm tra logs:</p>



<pre class="wp-block-code"><code>docker-compose logs nginx
docker-compose logs certbot</code></pre>



<h2 id="buoc-9-kiem-tra-https" class="wp-block-heading">Bước 9: Kiểm tra HTTPS</h2>



<p>Truy cập domain qua HTTPS:</p>



<pre class="wp-block-code"><code>curl -I https://your-domain.com:8443</code></pre>



<p>Hoặc mở trình duyệt và truy cập: <code>https://your-domain.com:8443</code><br><br>Bạn sẽ thấy:<br><br>・Certificate hợp lệ (không có cảnh báo)<br><br>・Tự động redirect HTTP → HTTPS<br><br>・Security headers được áp dụng</p>



<h2 id="buoc-10-gia-han-certificates" class="wp-block-heading">Bước 10: Gia hạn certificates</h2>



<p>Let&#8217;s Encrypt certificates có thời hạn 90 ngày. Có 2 cách gia hạn:<br><br><strong>10.1. Tự động gia hạn (đã cấu hình)</strong></p>



<p>Service <code>certbot</code> trong <code>docker-compose.yml</code> tự động gia hạn mỗi 12 giờ:</p>



<pre class="wp-block-code"><code>certbot:
  image: certbot/certbot:latest
  volumes:
    - "./docker/nginx/certs:/etc/letsencrypt"
  entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h &amp; wait $${!}; done;'"</code></pre>



<p>Certbot chỉ gia hạn khi certificate sắp hết hạn (trong vòng 30 ngày).</p>



<p><strong>10.2. Gia hạn thủ công</strong><br><br>Nếu cần gia hạn ngay:</p>



<pre class="wp-block-code"><code>docker run --rm \
    -v "$(pwd)/docker/nginx/certs:/etc/letsencrypt" \
    certbot/certbot renew</code></pre>



<p>Hoặc trong container:</p>



<pre class="wp-block-code"><code>docker-compose exec certbot certbot renew</code></pre>



<p>Sau khi gia hạn, reload Nginx để áp dụng certificate mới:</p>



<pre class="wp-block-code"><code>docker-compose exec nginx nginx -s reload</code></pre>



<p>Hoặc restart container:</p>



<pre class="wp-block-code"><code>docker-compose restart nginx</code></pre>



<h2 id="troubleshooting" class="wp-block-heading">Troubleshooting</h2>



<p><strong>Lỗi: &#8220;Port 80 is already in use&#8221;</strong><br><br>Nguyên nhân: Port 80 đang được sử dụng bởi service khác.<br><br>Giải pháp:</p>



<pre class="wp-block-code"><code># Kiểm tra process đang dùng port 80
sudo lsof -i :80

# Tạm dừng Nginx nếu đang chạy
docker-compose down

# Sau đó chạy lại setup-ssl.sh
./docker/nginx/setup-ssl.sh</code></pre>



<p><strong>Lỗi: &#8220;Failed to obtain certificate&#8221;</strong><br><br>Nguyên nhân:<br>・Domain chưa trỏ về IP server<br>・Port 80 bị firewall chặn<br>・Let&#8217;s Encrypt không thể truy cập domain<br><br>Giải pháp:</p>



<pre class="wp-block-code"><code># Kiểm tra DNS
nslookup your-domain.com

# Kiểm tra port 80
curl -I http://your-domain.com

# Kiểm tra firewall
sudo ufw status</code></pre>



<p><strong>Lỗi: &#8220;Nginx không load HTTPS config&#8221;</strong><br><br>Nguyên nhân:<br>・Certificate chưa tồn tại<br>・Biến môi trường chưa đúng<br>・Volume mount sai<br><br>Giải pháp:</p>



<pre class="wp-block-code"><code># Kiểm tra certificate
ls -la docker/nginx/certs/live/your-domain.com/

# Kiểm tra biến môi trường
docker-compose config

# Kiểm tra volume mount trong container
docker-compose exec nginx ls -la /etc/letsencrypt/live/</code></pre>



<h2 id="tong-ket" class="wp-block-heading">Tổng kết</h2>



<p>Sau khi hoàn thành các bước trên, bạn đã:<br>・Thiết lập Nginx với HTTPS trên Docker<br>・Cấu hình Let&#8217;s Encrypt SSL/TLS certificates<br>・Tự động gia hạn certificates<br>・Redirect HTTP → HTTPS<br>・Áp dụng security headers</p>
<p>The post <a href="https://blog.tomosia.com.vn/huong-dan-tung-buoc-thiet-lap-lets-encrypt-ssl-tls-cho-nginx-tren-docker/">Hướng dẫn từng bước thiết lập Let&#8217;s Encrypt SSL/TLS cho Nginx trên Docker.</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-tung-buoc-thiet-lap-lets-encrypt-ssl-tls-cho-nginx-tren-docker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Forward proxy vs. Reverse proxy</title>
		<link>https://blog.tomosia.com.vn/forward-proxy-vs-reverse-proxy/</link>
					<comments>https://blog.tomosia.com.vn/forward-proxy-vs-reverse-proxy/#comments</comments>
		
		<dc:creator><![CDATA[admin_tomosia]]></dc:creator>
		<pubDate>Fri, 08 Dec 2023 03:36:29 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Web server]]></category>
		<guid isPermaLink="false">https://blog.tomosia.com.vn/?p=2428</guid>

					<description><![CDATA[<p>Trong mạng máy tính, proxy là một máy chủ trung gian giữa client và các server khác trên&#8230;</p>
<p>The post <a href="https://blog.tomosia.com.vn/forward-proxy-vs-reverse-proxy/">Forward proxy vs. Reverse proxy</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Trong mạng máy tính, proxy là một máy chủ trung gian giữa client và các server khác trên Internet. Khi client gửi yêu cầu đến server, request sẽ được gửi đến proxy trước. Sau đó, proxy sẽ gửi request đến server thay cho client và trả về response cho client.</p>



<h2 id="tai-sao-lai-can-co-proxy" class="wp-block-heading">Tại sao lại cần có proxy?</h2>



<p>Thay vì kết nối trực tiếp tới server, client gửi request đến proxy để đánh giá và thực hiện những request đó.</p>



<ul class="wp-block-list">
<li><strong>Bảo vệ quyền riêng tư:</strong>&nbsp;Proxy có thể được sử dụng để ẩn địa chỉ IP của client,&nbsp;giúp bảo vệ quyền riêng tư của người dùng khỏi các trang web và ứng dụng mà họ truy cập.</li>



<li><strong>Cân bằng tải (Load balancing):</strong>&nbsp;Proxy có thể được sử dụng để phân phối lưu lượng truy cập giữa nhiều server,&nbsp;giúp cải thiện hiệu suất và độ tin cậy của hệ thống.</li>



<li><strong>Bảo mật (Security):</strong>&nbsp;Proxy có thể được sử dụng để lọc lưu lượng truy cập,&nbsp;giúp ngăn chặn các cuộc tấn công mạng.</li>



<li><strong>Giảm chi phí:</strong>&nbsp;Proxy có thể được sử dụng để truy cập các tài nguyên từ xa,&nbsp;giúp giảm chi phí cho doanh nghiệp.</li>
</ul>



<p>Có 2 loại proxy thường gặp đó là Forward proxy và Reverse proxy.</p>



<h2 id="forward-proxy" class="wp-block-heading">Forward proxy</h2>



<ul class="wp-block-list">
<li>Là một&nbsp;<em>client proxy</em>, hoạt động thay mặt cho các thiết bị clients để gửi yêu cầu đến server.</li>



<li>Nó <strong>nằm giữa clients và Internet</strong>, chuyển tiếp các requests của clients đến server thông qua Internet.</li>
</ul>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="512" src="http://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-1024x512.png" alt="" class="wp-image-2437" srcset="https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-1024x512.png 1024w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-300x150.png 300w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-768x384.png 768w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-1536x768.png 1536w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-2048x1024.png 2048w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-380x190.png 380w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-800x400.png 800w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/forward_proxy_flow-1160x580.png 1160w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h4 id="use-cases" class="wp-block-heading">Use cases:</h4>



<ul class="wp-block-list">
<li><strong>Access control and content filtering: </strong>forward proxy kiểm tra các request có hợp lệ hay không để thực hiện forward hoặc block request.</li>



<li><strong>Log/Monitor request</strong>: quản lý và giám sát các request, thường được sử dụng trong môi trường công ty, trường học hay các tổ chức để kiểm soát việc truy cập Internet và bảo vệ khỏi các nội dung độc hại.</li>



<li><strong>User</strong> <strong>Privacy</strong>: forward proxy nằm giữa client và Internet, nó thay mặt client gửi request đến server qua Internet, có thể che dấu danh tính, địa chỉ IP và bảo vệ quyền riêng tư của người dùng thực.</li>



<li><strong>Cache responses</strong>: giảm độ trễ, tăng performance, tối ưu bandwidth, proxy cache lại response để trả về cho client trong các lần request tiếp theo thay vì request trực tiếp đến server.</li>
</ul>



<h4 id="services" class="wp-block-heading">Services:</h4>



<ul class="wp-block-list">
<li>Squid</li>



<li>Privoxy</li>



<li>VPN services</li>



<li>Cloudflare WARP</li>
</ul>



<p></p>



<h2 id="reverse-proxy" class="wp-block-heading">Reverse proxy</h2>



<ul class="wp-block-list">
<li>Là một <em>server proxy,</em> <strong>nằm giữa Internet và server</strong> để xử lý các request thay mặt cho server.</li>



<li>Chúng hoạt động như những servers bình thường. <strong>Reverse proxy</strong> chuyển tiếp request đến một hoặc nhiều server thật, kết quả sau đó trả về cho client, làm cho client không biết về những server thật nói trên.</li>



<li><strong>Reverse proxy</strong> được cài đặt trong một private network của một hoặc nhiều server, và tất cả lưu lượng truy cập đều phải đi qua proxy này.</li>
</ul>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="512" src="http://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-1024x512.png" alt="" class="wp-image-2440" srcset="https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-1024x512.png 1024w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-300x150.png 300w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-768x384.png 768w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-1536x768.png 1536w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-2048x1024.png 2048w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-380x190.png 380w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-800x400.png 800w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/reverse_proxy_flow-1160x580.png 1160w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h4 id="use-cases-2" class="wp-block-heading">Use cases:</h4>



<ul class="wp-block-list">
<li><strong>Load Balancing:</strong> cân bằng tải, phân bổ lượng truy cập trang web đến các máy chủ khác nhau, nhằm mục đích tăng throughput, giảm letancy, phục vụ số lượng client lớn hơn.</li>



<li><strong>Security</strong>: có thể coi <strong>reverse proxy</strong> là một Web Application Firewall (WAF), có nhiệm vụ chặn các truy cập khả nghi đến server, làm rate limit, một phần nào đó ngăn chặn tấn công DDoS.</li>



<li><strong>Cache responses</strong>: giảm độ trễ, tăng performance, tối ưu bandwidth, proxy cache lại response để trả về cho client trong các lần request tiếp theo thay vì request trực tiếp đến server.</li>



<li><strong>SSL encryption/decryption</strong>: <strong>reverse proxy</strong> có thể xử lý việc SSL termination (dừng), giải mã lưu lượng được mã hóa SSL từ client và chuyển tiếp nó đến máy chủ phụ trợ ở dạng không được mã hóa và ngược lại.</li>
</ul>



<h4 id="services-2" class="wp-block-heading">Services:</h4>



<ul class="wp-block-list">
<li>Nginx</li>



<li>Apache HTTP</li>



<li>HAProxy</li>



<li>CDN services</li>
</ul>



<p></p>



<h2 id="diem-khac-biet-chinh" class="wp-block-heading"><strong>Điểm khác biệt chính</strong></h2>



<ul class="wp-block-list">
<li>Forward proxy: 
<ul class="wp-block-list">
<li>Xử lý request từ client tới Internet.</li>



<li>Bảo vệ quyền riêng tư client và lọc nội dung.</li>
</ul>
</li>



<li>Reverse proxy:
<ul class="wp-block-list">
<li>Xử lý request từ Internet tới web servers.</li>



<li>Performance và security.</li>
</ul>
</li>
</ul>



<figure class="wp-block-image size-large"><img decoding="async" width="963" height="1024" src="http://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-963x1024.jpg" alt="" class="wp-image-2445" srcset="https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-963x1024.jpg 963w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-282x300.jpg 282w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-768x816.jpg 768w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-380x404.jpg 380w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-800x850.jpg 800w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536-1160x1233.jpg 1160w, https://blog.tomosia.com.vn/wp-content/uploads/2023/12/257642d6-9742-432b-9ca8-2a866dea04dd_1445x1536.jpg 1445w" sizes="(max-width: 963px) 100vw, 963px" /></figure>



<p class="has-small-font-size"><em>Nguồn: <a href="https://blog.bytebytego.com">https://blog.bytebytego.com</a></em></p>



<p>Cảm ơn mọi người đã dành thời gian để đọc 🙇‍♂️</p>
<p>The post <a href="https://blog.tomosia.com.vn/forward-proxy-vs-reverse-proxy/">Forward proxy vs. Reverse proxy</a> appeared first on <a href="https://blog.tomosia.com.vn">Tomoshare</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.tomosia.com.vn/forward-proxy-vs-reverse-proxy/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
