【发布时间】:2018-03-13 20:23:35
【问题描述】:
我在 Node.js 应用程序前使用 NGINX 作为反向代理。基本代理工作得很好,我可以使用 compression 中间件在 Node 服务器上压缩资产。
为了测试是否可以将压缩任务委托给 NGINX,我禁用了中间件,现在我尝试使用以下配置使用 NGINX 进行 gzip:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
## gzip config
gzip on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain
text/css
text/javascript
image/gif
image/png
image/jpeg
image/svg+xml
image/x-icon;
location / {
proxy_pass http://app:3000/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
}
}
使用此配置,NGINX 不会压缩资源。我尝试使用不同的选项在 location 上下文中声明这些,但它们似乎都不起作用。
我找不到这方面的相关资源,所以我怀疑是否可以这样做。
要点:
1- Node 和 NGINX 位于不同的容器上,所以我不使用 NGINX 提供静态资产。我只是代理到为这些文件提供服务的节点服务器。我想要实现的只是通过让 NGINX 进行 gzip 压缩来卸载节点服务器。
2- 我正在测试启用了“Accept-Encoding: gzip”的所有响应。
【问题讨论】:
标签: node.js nginx gzip reverse-proxy