如何在通用的Angular和Nginx中启用gzip文本压缩?

4

有人知道如何在nginx和通用Angular中启用gzip文本压缩吗?我不知道从哪里开始做。

2个回答

11

gzip与Angular无关,它是服务器相关的事情。 在nginx中,您可以通过设置gzip on;来启用它。

如下所示:

server {
    gzip on;
    gzip_types      text/plain application/xml;
    gzip_proxied    no-cache no-store private expired auth;
    gzip_min_length 1000;
    ...
}

请查看以下文章获取更多细节:

https://docs.nginx.com/nginx/admin-guide/web-server/compression/


非常感谢,问候。 - Alex Onofre

8

第一步是编辑nginx.conf文件,它可能位于大多数发行版中的/etc/nginx/nginx.conf或者/usr/local/nginx/conf/nginx.conf

用您喜欢的编辑器打开nginx.conf文件。 您可以看到已经有一个关于Gzip的设置块; 您可以随时修改并取消注释下面显示的行:

# enable gzip compression

# Turns on/off the gzip compression.
gzip on;

# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
# 75% reduction for most ascii files (almost identical to level 9).
gzip_comp_level    5;

# The minimum size file to compress the files.
gzip_min_length  1100;

# Set the buffer size of gzip, 4 32k is good enough for almost everybody.
gzip_buffers  4 32k;

# Compress data even for clients that are connecting to us via proxies,
# identified by the "Via" header (required for CloudFront).
gzip_proxied       any;

# This directive let you specify which file types should be compressed, in this case plain text, js files, xml and #css.
gzip_types    text/plain application/x-javascript text/xml text/css;

# Enables response header of “Vary: Accept-Encoding
gzip_vary on;

# end gzip configuration

完成以上配置更改后,重新启动或重新加载您的服务器,现在您将使用gzip压缩为网站资产提供服务。
/etc/init.d/nginx reload

或者

sudo service nginx restart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接