0

我在不同的 nginx(1.15.0) 服务器上有两个域(server1 example.comserver2 example.net)。我尝试使用ngx_http_substitutions_filter_module将server2设置为反向代理,但它没有按预期工作。由于我的配置,subs_filter指令应该将example.com替换为example.net但是当我在浏览器中键入example.net时,它会将我重定向到example.com

nginx.conf

http {
 //other settings

 .....

 include /etc/nginx/sites-enabled/*;

 upstream example.com {
   server example.com;
 }

}

例子.net.conf

server {
 listen 80;
 server_name example.net www.example.net;
 rewrite ^/(.*)$ https://example.net/$1 permanent;
}

server {
  listen 443 ssl; 
  server_name example.net;

  ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem; 
  ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem; 
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

  location / {
    root html;
    try_files $uri @example.com;
  }

  location @example.com {
    include replace.conf;
    proxy_pass http://example.com;

    proxy_cookie_domain example.com example.net;

    proxy_set_header Accept-Encoding "";
    proxy_set_header Host example.com;
    proxy_redirect http://example.com http://example.net;

  }
}

替换.conf

# replace direct links
subs_filter "www.example.com" "example.net" gi;
subs_filter "example.com" "example.net" gi;

似乎 nginx 忽略了subs_filter指令。有人可以解释一下如何使用 ngx_http_substitutions_filter_module 正确替换 uri 吗?谢谢你的建议!

4

1 回答 1

0

问题解决了。首先,我从nginx.conf中删除了上游:

upstream example.com {
  server example.com;
}

然后我在example.net.conf中更改了以下几行:

  location / {
   ...
   try_files $uri @static;
  }

  location @static {
   ...
   proxy_pass https://example.com;

   ...

   ...
   ...
   proxy_redirect https://example.com https://example.net;
 }

除登录表单外,一切正常。Firefox 正常工作,但 Chrome 返回错误 422。我想这是因为登录表单适用于 javascript。总之谢谢大家!

于 2018-06-22T16:23:26.187 回答