天天看點

使用Docker釋出blazor wasm

Blazor編譯後的檔案是靜态檔案,是以我們隻需要一個支援靜态頁面的web server即可。

根據不同項目,會用不同的容器編排,本文已無網關的情況下為例,一步一步展示如何打包進docker

申請阿裡雲服務時,可以使用

2000元阿裡雲代金券 ,阿裡雲官網領取網址: https://dashi.aliyun.com/site/yun/youhui

需求

HTTPS

既然無網關,直接面向網際網路,是以HTTPS顯得尤為重要

HTTP/2 TLS3.0

既然都是靜态資源,使用H2和TLS3.0的目的是進一步加快加載速度

Compress

對靜态資源的壓縮的目的依然是進一步加快加載速度。壓縮選型為Brotli和 Gzip 壓縮

Dockerfile

官方的Nginx鏡像,預設不支援Brotli

是以需要自己準備一個具有Brotli支援的鏡像,這裡推薦使用自賣自誇的Nginx鏡像,不僅使用最新的openssl編譯(避免漏洞),還支援TLS1.3 http2 brotli和預設東八時區,且配置檔案裡還有配置示例。歡迎通路Docker Hub rsnow/nginx,了解更多。

釋出blazor wasm

截至2020.05.26,VS還不能把blazor wasm直接釋出到Docker鏡像倉庫,是以隻能自己打包

阿裡雲伺服器1核2G低至82元/年 ,阿裡雲官活動網址: https://dashi.aliyun.com/site/yun/aliyun

可以用20代金券,即102-20=82。

首先釋出Release,不再贅述

在項目根目錄建立Dockerfile

FROM rsnow/nginx:amd64-1.18.0

RUN rm /usr/share/nginx/html/index.html && \

echo -e 'server { n\

listen 443 ssl http2; n\

server_name localhost; n\

brotli on; n\

brotli_comp_level 6; n\

brotli_types application/wasm application/octet-stream text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml; n\

gzip on; n\

gzip_vary on; n\

gzip_proxied any; n\

gzip_comp_level 6; n\

gzip_types application/wasm application/octet-stream text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml; n\

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; n\

ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; n\

ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; n\

ssl_dhparam /etc/certs/dhparam.pem; n\

ssl_session_timeout 1d; n\

ssl_session_cache shared:SSL:10m; about 40000 sessions n\

ssl_session_tickets off; n\

ssl_protocols TLSv1.3; n\

ssl_prefer_server_ciphers off; n\

ssl_stapling on; n\

ssl_stapling_verify on; n\

resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s; n\

resolver_timeout 2s; n\

add_header X-Frame-Options "SAMEORIGIN" always; n\

add_header X-XSS-Protection "1; mode=block" always; n\

add_header X-Content-Type-Options "nosniff" always; n\

add_header Referrer-Policy "no-referrer-when-downgrade" always; n\

add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always; n\

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; n

location / { n\

root   /usr/share/nginx/html; \n\
try_files $uri $uri/ /index.html =404; \n\           

} n\

' > /etc/nginx/conf.d/default.conf

COPY ./bin/Release/netstandard2.1/publish/wwwroot /usr/share/nginx/html/

我們使用自定義的conf覆寫掉預設的conf(最簡單的情況)

整個Nginx Container隻有一個虛拟主機,是以server_name無論為何值,請求都會指向這個唯一的虛拟主機

如果有網關,可以關閉跨域,HTTPS之類的配置,因為這些内容網關會涵蓋

因本示例展示的是最簡單的情況,是以并沒有使用 volume ,如果有容器互聯,加載外部證書,配置檔案的情況可根據實際情況建立挂載點。

無論是gz或br都增加了對application/wasm application/octet-stream的支援

Images

有個Dockerfile,就開始生成鏡像吧

docker build -t example.com/testnginxblazor:latest .

如果需要上傳到倉庫

docker push example.com/testnginxblazor:latest

Run

根據建立的鏡像,建立并運作容器

docker run \

-d \

--name nginxblazor \

-p 443:443 \

-v /test/fullchain.pem : /etc/letsencrypt/live/example.com/fullchain.pem \

-v /test/privkey.pem : /etc/letsencrypt/live/example.com/privkey.pem \

-v /test/chain.pem : /etc/letsencrypt/live/example.com/chain.pem \

-v /test/dhparam.pem : /etc/certs/dhparam.pem \

example.com/testnginxblazor

聲明

本文采用知識共享署名-非商業性使用-相同方式共享 2.5 中國大陸許可協定進行許可,發表在CSDN和部落格園,歡迎讀者轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接!請讀者/爬蟲們尊重版權

原文位址

https://www.cnblogs.com/chasingdreams2017/p/12984025.html

繼續閱讀