【问题标题】:Nginx on docker - Vary: Accept-Encoding header is missingdocker上的Nginx - 变化:缺少Accept-Encoding标头
【发布时间】:2018-08-19 23:37:45
【问题描述】:

我正在为一个基于 nginx:alpine 基础镜像的 docker 容器上运行的 nginx 提供静态网站。

我的 Docker 文件:

FROM nginx:alpine
COPY --from=angular-built app/dist/dayTwoApp /usr/share/nginx/html
COPY ./default.conf /etc/nginx/conf.d/default.conf

default.conf 文件:

server {
   listen 80;

    gzip on;
    gzip_vary on;
    gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;

    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

我在提供的 html 文件的响应中看到了 vary : Accept-Encoding 标头(见下文)。

但由于某种原因,我在 js 和 css 响应中看不到标题。

(*) 无效的相关参考:

回复详情:

html 文件:

js 文件(也适用于 css):

【问题讨论】:

    标签: angular http docker nginx single-page-application


    【解决方案1】:

    在您的第二个示例中,Nginx 返回 HTTP 响应代码 304,这表明内容尚未从您的缓存副本中修改。按照 HTTP 规范:

    304 响应不能包含消息体,因此总是 以标题字段后的第一个空行结束。

    响应必须包含以下标头字段:

      - Date, unless its omission is required by section 14.18.1
      - ETag and/or Content-Location, if the header would have been sent
        in a 200 response to the same request
      - Expires, Cache-Control, and/or Vary, if the field-value might
        differ from that sent in any previous response for the same
        variant
    

    因此,如果原始缓存响应已经包含 Vary 标头,那么这个 304 响应不包含是完全正确的。

    【讨论】:

      【解决方案2】:

      请尝试添加到您的 nginx 配置中:

      gzip_proxied any
      gzip_types
          text/plain
          text/css
          text/js
          text/xml
          text/javascript
          application/javascript
          application/x-javascript
          application/json
          application/xml
          application/rss+xml
          image/svg+xml;
      

      (只有“text/html”类型的回复是always compressed。)

      【讨论】:

        猜你喜欢
        • 2015-04-20
        • 2013-03-29
        • 1970-01-01
        • 2013-05-14
        • 2012-06-18
        • 1970-01-01
        • 2016-12-22
        • 2013-09-12
        • 2012-03-10
        相关资源
        最近更新 更多