AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1002255
Accepted
adrianTNT
adrianTNT
Asked: 2020-02-08 12:52:59 +0800 CST2020-02-08 12:52:59 +0800 CST 2020-02-08 12:52:59 +0800 CST

设置正确的请求主机以避免 nginx 中的这种无限循环

  • 772

我将此 nginx 服务器配置为缓存/反向代理example.com,www.example.com从 mysource.example.com 获取数据

它在浏览器中似乎工作正常,但我注意到谷歌排名显着下降,当我用 wget 测试 URL 时,我得到一个无限循环。

# test without www , getting infinite loop
wget --header="Host: example.com" http://[SERVER IP]/file.html


Location: https://www.example.com/file.html [following]
--2020-02-07 21:43:14--  https://www.example.com/file.html
Reusing existing connection to www.example.com:443.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.example.com/file.html [following]
20 redirections exceeded.
^^^ RIGHT HERE !!!

# but with www it works OK:
wget --header="Host: www.example.com" http://[SERVER IP]/file.html


HTTP request sent, awaiting response... 200 OK
Length: 1307 (1.3K) [text/plain]
Saving to: ‘file.html’

我想我需要host在从非 www 到 www 的重定向中指定另一个?!

或者这只是一个wget 的事情,因为它通常应该在第一次重定向时切换到 www.example.com,但它使非 www 主机不受 nginx 命令的影响?

# redirect http to https
server {
    listen 80;
    server_name example.com;
    server_name www.example.com;

    proxy_set_header Host      www.example.com
    return 301 https://www.example.com$request_uri;
}


# and redirect non www to www
server {
    listen 443 ssl http2;
    server_name example.com;

    proxy_set_header Host      www.example.com;
    return 301 https://www.example.com$request_uri;
}


# main server, SSL
server {

    listen       443 ssl http2;
    server_name  www.example.com;

    location / {

            proxy_pass       http://mysource.example.com:81;

            proxy_set_header Host      www.example.com;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

            proxy_cache             nginx_ramdisk_cache;
    }



} # end of server
cache redirect nginx reverse-proxy 301-redirect
  • 1 1 个回答
  • 544 Views

1 个回答

  • Voted
  1. Best Answer
    Piotr P. Karwasz
    2020-02-08T14:06:25+08:002020-02-08T14:06:25+08:00

    问题出在你使用的方式上wget。通过使用:

    wget --header="Host: example.com" http://example.com/file.html
    

    您替换wget将执行的每个请求的Host标头。所以:

    1. wget 连接到80服务器的端口并被重定向到https://www.example.com/file.html,
    2. wget 连接到443服务器的端口,但发送Host: example.com标头而不是Host: www.example.com. 它被重定向到https://www.example.com/file.html,
    3. 我们2点回来。

    所以,你的配置很好,只是你的测试有问题。您永远不应覆盖Host标头,wget会自动将其设置为 URL 中的域。

    • 2

相关问题

  • 缩放大文件下载?

  • Gzip 与反向代理缓存

  • 如何禁用tomcat缓存?我遇到了奇怪的静态文件问题

  • mod_mem_cache 提供错误的内容!

  • NFS 缓存导致间歇性滞后

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve