Ver código fonte

Add webp and negotiation

master
Chris Smith 5 anos atrás
pai
commit
f39f3479e8
3 arquivos alterados com 75 adições e 17 exclusões
  1. 2
    2
      Dockerfile
  2. 5
    0
      minify.sh
  3. 68
    15
      nginx.conf

+ 2
- 2
Dockerfile Ver arquivo

@@ -24,7 +24,7 @@ RUN hugo -b https://www.chameth.com/ -v -s /tmp/site -d /tmp/hugo && \
24 24
 
25 25
 FROM debian:stretch as minify
26 26
 RUN apt-get update \
27
-    && DEBIAN_FRONTEND=noninteractive apt-get -qq install -y --no-install-recommends yui-compressor tidy \
27
+    && DEBIAN_FRONTEND=noninteractive apt-get -qq install -y --no-install-recommends yui-compressor tidy webp \
28 28
 	&& rm -rf /var/lib/apt/lists/*
29 29
 
30 30
 COPY --from=hugo /tmp/hugo /tmp/site
@@ -40,4 +40,4 @@ RUN /tmp/minify.sh
40 40
 
41 41
 FROM nginx:mainline-alpine AS nginx
42 42
 COPY --from=minify /tmp/site /usr/share/nginx/html
43
-ADD nginx.conf /etc/nginx/conf.d/site.conf
43
+ADD nginx.conf /etc/nginx/nginx.conf

+ 5
- 0
minify.sh Ver arquivo

@@ -21,3 +21,8 @@ for file in $(find /tmp/site/ -name '*.html'); do
21 21
 	# Tidy exits if there are warnings, which there probably will be...
22 22
 	tidy -q -i -w 120 -m --vertical-space yes --drop-empty-elements no "$file" || true
23 23
 done
24
+
25
+# Convert all images to WebP
26
+for file in $(find /tmp/site -name '*.jpg' -o -name '*.png' -o -name '*.jpeg'); do
27
+	cwebp -m 6 -mt -o "$file.webp" -- "$file"
28
+done

+ 68
- 15
nginx.conf Ver arquivo

@@ -1,15 +1,68 @@
1
-server_tokens off;
2
-
3
-add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
4
-add_header Content-Security-Policy "require-sri-for script; default-src 'none'; script-src 'self' 'sha384-m2EXauJIeXunnu9rWV0uaFjwoSeSA+jEbAKdI5sQaGiiiOwht/hOVB/8lq2JI8Bd'; img-src 'self' https://photos.chameth.com https://a.c5h.io; style-src 'self'; font-src 'self'; frame-ancestors 'none'; form-action 'none'; base-uri 'none';";
5
-add_header X-Frame-Options "SAMEORIGIN";
6
-add_header X-Content-Type-Options "nosniff";
7
-add_header X-XSS-Protection "1; mode=block";
8
-add_header Expect-CT "enforce; max-age=3600";
9
-add_header Referrer-Policy "no-referrer";
10
-
11
-gzip on;
12
-gzip_vary on;
13
-gzip_proxied any;
14
-gzip_comp_level 6;
15
-gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
1
+user  nginx;
2
+worker_processes  1;
3
+
4
+error_log  /var/log/nginx/error.log warn;
5
+pid        /var/run/nginx.pid;
6
+
7
+
8
+events {
9
+    worker_connections  1024;
10
+}
11
+
12
+
13
+http {
14
+    include       /etc/nginx/mime.types;
15
+    default_type  application/octet-stream;
16
+
17
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
18
+                      '$status $body_bytes_sent "$http_referer" '
19
+                      '"$http_user_agent" "$http_x_forwarded_for"';
20
+
21
+    access_log  /var/log/nginx/access.log  main;
22
+
23
+    sendfile        on;
24
+
25
+    keepalive_timeout  65;
26
+
27
+    server_tokens off;
28
+
29
+    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
30
+    add_header Content-Security-Policy "require-sri-for script; default-src 'none'; script-src 'self' 'sha384-m2EXauJIeXunnu9rWV0uaFjwoSeSA+jEbAKdI5sQaGiiiOwht/hOVB/8lq2JI8Bd'; img-src 'self' https://photos.chameth.com https://a.c5h.io; style-src 'self'; font-src 'self'; frame-ancestors 'none'; form-action 'none'; base-uri 'none';";
31
+    add_header X-Frame-Options "SAMEORIGIN";
32
+    add_header X-Content-Type-Options "nosniff";
33
+    add_header X-XSS-Protection "1; mode=block";
34
+    add_header Expect-CT "enforce; max-age=3600";
35
+    add_header Referrer-Policy "no-referrer";
36
+
37
+    gzip on;
38
+    gzip_vary on;
39
+    gzip_proxied any;
40
+    gzip_comp_level 6;
41
+    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
42
+
43
+    map $http_accept $webp_suffix {
44
+        "~*webp"  ".webp";
45
+    }
46
+
47
+    server {
48
+        listen       80;
49
+        server_name  localhost;
50
+
51
+        location / {
52
+            root   /usr/share/nginx/html;
53
+            index  index.html index.htm;
54
+
55
+            location ~ \.(png|jpe?g)$ {
56
+                try_files $uri$webp_suffix $uri =404;
57
+            }
58
+        }
59
+
60
+        #error_page  404              /404.html;
61
+
62
+        error_page   500 502 503 504  /50x.html;
63
+        location = /50x.html {
64
+            root   /usr/share/nginx/html;
65
+        }
66
+    }
67
+
68
+}

Carregando…
Cancelar
Salvar