【问题标题】:nginx domain forwarding with added parameter带有添加参数的 nginx 域转发
【发布时间】:2015-09-19 02:49:24
【问题描述】:

我现在一直在 nginx 上遇到一个奇怪的问题。我有两台 nginx 服务器,一台托管我想转发到另一台服务器的内容。进行转发的站点启用了以下配置:

server {
  listen 80;
  server_name pastdomain.com;
  return 301 https://domain.com$request_uri?from_past_domain=true;
}

server {
  listen 443;
  server_name pastdomain.com;
  return 301 https://domain.com$request_uri?from_past_domain=true;

 # bunch of ssl config here
}

基本上,我想将所有流量发送到新服务器,在那里可以使用新的 get 变量 from_past_domain 进行解释,我可以在新服务器上根据需要对其进行解释。

即。 pastdomain.com/thing/thing1/1/

会翻译成

domain.com/thing/thing1/1?from_past_domain=true

现在它似乎正在工作,除了我刚刚访问 pastdomain.com 的情况

我改为获取 domain.com//?from_past_domain=true

这是不正确的。此外,它没有正确添加新的 get 参数。

即。如果我有 pastdomain.com?test=1&test2=2 它转发到 domain.com/?test=1&test2=2?from_past_domain=true

如何正确转发?

【问题讨论】:

    标签: nginx configuration dns server devops


    【解决方案1】:

    如果你想改变 URI,你应该使用rewrite。在这种情况下,nginx 会处理正确的请求参数。

    此外,您可以 use one server block 处理 HTTP 和 HTTPS。

    server {
      listen 80;
      listen 443 ssl;
      server_name pastdomain.com;
    
      rewrite ^(.*)$ https://domain.com$1?from_past_domain=true permanent;
    
      # bunch of ssl config here
      # DO NOT use `ssl on;`
    }
    

    【讨论】:

    猜你喜欢
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2012-12-05
    相关资源
    最近更新 更多