在 nginx 下提供 Gzipped js 和 css 文件网络应用程序

Serve Gzipped js and css files web app under nginx

我有一个使用 Angular (angular-cli)

为了构建我的应用程序,我 运行 ng build --configuration=production

我听说从 angular-cli 的这个过程中删除了 gzipping 操作,所以使用特定的 npm 包 "Gzipper"https://www.npmjs.com/package/gzipper

我习惯于使用一个组合命令来构建和压缩我的输出文件:

ng build --configuration=production && && gzipper src/main/webapp/dist --log

我的输出文件是这样的:

现在在我的 nginx.conf.file 下: 我已经把这个配置:

http {
    include mime.types;
    default_type  application/octet-stream;
    client_max_body_size 10M;

    sendfile        on;
    keepalive_timeout  65;

    more_clear_headers "Server";

    gzip  on;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

    server {
       ...
    }

即使有这一切:在任何浏览器下,它都会给我一个 404 not found files 它似乎看不到那些 gzip 文件,我的应用程序无法提供服务

建议 ?

您必须添加 gzip_static on;

gzip  on;
gzip_static on;

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files.