0

I have built a simple static site, that I am serving with nginx (from docker). Currently I am trying to implement some best-practices, including gzip.

I have used some example config I found online, to set up my own config. Gzip is working for most types, except for css...

I used curl for testing:

curl -H "Accept-Encoding: gzip" -I localhost:8400 produces:

HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Wed, 05 Jan 2022 12:35:06 GMT
Content-Type: text/html
Connection: keep-alive
Last-Modified: Thu, 30 Dec 2021 13:24:15 GMT
Vary: Accept-Encoding
ETag: W/"61cdb2ff-ff5"
Content-Security-Policy: script-src 'self' 'unsafe-inline' 'unsafe-eval' *.transistories.org; frame-src 'self'; object-src 'self'
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000;
X-XSS-Protection: 1; mode=block
Content-Encoding: gzip

while curl -H "Accept-Encoding: gzip" -I localhost:8400/css/main.css produces:

HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Wed, 05 Jan 2022 12:35:02 GMT
Content-Type: text/css
Content-Length: 8279
Connection: keep-alive
Last-Modified: Wed, 05 Jan 2022 10:46:33 GMT
Vary: Accept-Encoding
ETag: "61d57709-2057"
Expires: Sat, 29 Jan 2022 13:38:58 GMT
Cache-Control: max-age=2073600
Pragma: public
Cache-Control: max-age=2073600, public
Accept-Ranges: bytes

The main config file:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_types
        application/atom+xml
        application/geo+json
        application/javascript
        application/x-javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rdf+xml
        application/rss+xml
        application/xhtml+xml
        application/xml
        font/eot
        font/otf
        font/ttf
        image/svg+xml
        text/css
        text/javascript
        text/js
        text/plain
        text/xml;
    gzip_disable "msie6";



    server_tokens off;

    include /etc/nginx/conf.d/transistories.conf;
}

The included config file:

server {
    listen 80;
    listen [::]:80;
    server_name localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' *.transistories.org; frame-src 'self'; object-src 'self'";
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Strict-Transport-Security "max-age=31536000;" always;
    add_header X-XSS-Protection "1; mode=block";

    location ~* \.(?:ico|css|js|gif|jpe?g|png|eot|woff2?|ttf|svg)$ {
        root /usr/share/nginx/html;
        expires 24d;
        add_header Pragma public;
        add_header Cache-Control "max-age=2073600, public";
    }

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }

    error_page 404 /404.html;
}

In case it is relevant; the Dockerfile that I am using: The css files are generated from scss, using node-sass.

FROM node:current-alpine3.14 AS builder

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY assets ./assets

COPY gulpfile.js .

COPY src ./src

RUN npm run build:prod

RUN npm run sitemap

RUN npm run rss

FROM nginx:latest

COPY --from=builder /usr/src/app/build /usr/share/nginx/html

COPY nginx/nginx.conf /etc/nginx
COPY nginx/conf.d /etc/nginx/conf.d

Any ideas why this could be happening, and how I could solve this? Thanks in advance!

0 Answers0