【问题标题】:http_sub_module / sub_filter of nginx and reverse proxy not workingnginx 的 http_sub_module / sub_filter 和反向代理不起作用
【发布时间】:2015-10-31 19:29:44
【问题描述】:

我正在尝试反向代理我的网站并修改内容。 为此,我使用 sub_filter 编译了 nginx。 它现在接受 sub_filter 指令,但它不能以某种方式工作。

server {
    listen       8080;
    server_name  www.xxx.com;

    access_log  /var/log/nginx/www.goparts.access.log  main;
    error_log  /var/log/nginx/www.goparts.error.log;
    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send request back to apache1 ##
    location / {
       sub_filter <title> '<title>test</title>';
 sub_filter_once on;


     proxy_pass  http://www.google.fr;
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
     proxy_redirect off;
     proxy_buffering off;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   }
}

请帮帮我

【问题讨论】:

  • 为什么要修改 Nginx 配置中的内容?你想在转发代理模式下使用 Nginx 吗?
  • 因为我打算以这种方式向网站添加页眉和页脚。我想在反向代理模式下使用它。
  • 我什至尝试过 Httpsubs 和 subs_filter,它也不起作用。有人有想法吗?
  • 基于 proxy_pass google.fr 看来您尝试构建正向代理而不是反向代理?
  • 是的,谷歌只是为了尝试一下。我现在尝试反向代理列出默认页面的 apache 目录并且它可以工作。我在某处读到 httpsubs 与 gzip 服务的页面有问题,所以我添加了 proxy_set_header Accept-Encoding "";尝试从服务器禁用 gzip 但没办法:/

标签: nginx proxy reverse inject


【解决方案1】:

检查上游源是否开启了gzip,如果需要的话

proxy_set_header Accept-Encoding "";

所以整个事情就像

location / {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://upstream.site/;
    sub_filter_types text/css;
    sub_filter_once off;
    sub_filter .upstream.site special.our.domain;
}

检查这些链接

【讨论】:

  • 我认为这是一个丑陋的修复,因为它会禁用原始服务器和公共服务器之间的 gzip(更多后端流量),但这是 Nginx 官方解决方案:nginx.com/resources/wiki/modules/substitutions
  • 我还要补充一点,在我的情况下,它只有在我在 location 指令中设置 proxy_set_header Accept-Encoding ""; 后才有效,它在 server 下无效。
猜你喜欢
  • 2015-11-24
  • 2019-06-15
  • 2019-06-05
  • 2020-06-23
  • 1970-01-01
  • 2021-09-15
  • 2019-07-28
  • 2017-06-29
  • 2019-05-03
相关资源
最近更新 更多