【问题标题】:Apache behind nginx gives out the wrong virtual hostnginx 背后的 Apache 给出了错误的虚拟主机
【发布时间】:2020-03-03 22:32:25
【问题描述】:

我在 apache 网络服务器上有一些网站,比如说:

site1.com 网站2.com site3.com

apache 正在监听 80 和 443,每个 vhost 都是这样配置的:

<VirtualHost site1.com:80>
    ServerName site1.com
    ServerAlias site1.com

    ServerAdmin webmaster@site1.com
    DocumentRoot /data/site1

    <Directory /data/site1>
           AllowOverride All
           Options -MultiViews +FollowSymLinks
           Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site1.com_error.log
    CustomLog ${APACHE_LOG_DIR}/site1.com_access.log combined

</VirtualHost>
<VirtualHost site1.com:443>
    ServerName   site1.com
    ServerAlias  site1.com
    ServerAdmin  webmaster@site1.com
    DocumentRoot "/data/site1"
    #more ssl config ...
</VirtualHost>

现在我在 site1.com 上配置了一个 nginx 反向代理 - 因此 DNS 被更改为将 site1.com 指向 nginx,并且在 nginx 上进行了此配置:

server {
    listen          site1.com:80;
    server_name     site1.com;
    expires 0;
    status_zone site1.com;
    if ($host !~ ^(site1.com)$ ) {
        return 444;
    }
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }
    location '/.well-known/acme-challenge' {
        root /etc/nginx/html/certbot;
    }
    location / {
        rewrite ^/(.*) https://site1.com/$1;
    }
}
server {
    listen          site1.com:443 ssl;
    server_name     site1.com;
    ssl_certificate      /etc/letsencrypt/live/site1.com,/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/site1.com/privkey.pem;
    expires 0;
    status_zone site1.com;
    if ($host !~ ^(site1.com)$ ) {
        return 444;
    }
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }
    location '/.well-known/acme-challenge' {
        root /etc/nginx/html/certbot;
    }
    location / {
        proxy_pass  http://apache_server_IP:80;
        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 X-Forwarded-Proto $scheme;
    }
}

所以 nginx 充当反向代理并且运行良好 - 一天后,我在 site1.com 上看到了来自 site2.com 的内容

我检查了 nginx 是否发送了正确的 host-header - 在 nginx-config proxy_set_header 中设置了主机 $host - 并且 nginx 发送了正确的 host-header(使用 %{Host}i 扩展 apache 日志记录)

所以我可以在 site2.com 的日志中看到对 site1.com 的请求,其中包含这样的条目

site1.com $nginx_IP - - [07/Nov/2019:10:07:29 +0100] "GET / HTTP/1.1" 302 313 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/ 537.36(KHTML,如 Gecko)Chrome/77.0.3865.120 Safari/537.36"

使用此日志格式:

LogFormat "%{Host}i %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\""

我的解决方案是删除 site2 和 site3 的 listen:80 - 现在当 nginx 在端口 80 上发出请求时,只剩下一个 vhost (site1) - 缺点:site2 和 site3 现在只能通过 https (不是真正的问题,但我希望他们在 80 上收听并再次重定向到 443)

我不知道问题出在哪里 - apache 是在试图愚弄我吗?

【问题讨论】:

    标签: apache nginx


    【解决方案1】:

    在阅读了更多 apache-docs (https://httpd.apache.org/docs/2.4/vhosts/examples.html) 之后,我尝试了虚拟主机中的星号,现在它可以工作了!

    随便用

    <VirtualHost *:80>
    

    而不是

    <VirtualHost site1.com:80>
    

    我不想再考虑这个了

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2018-07-04
      • 2014-07-07
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多