首页 > 解决方案 > 使用 Cloudflare 和 nginx 的子域代理通行证

问题描述

我有一个由 Cloudflare 的免费工具管理的域。我有一个要添加的子域,名为“test”,但我希望它指向在我的服务器上运行的第二个网络服务器,端口为 8183。在 Cloudflare DNS 门户中,我有一个“test.example.com”的 A 记录,它指向到我的网络服务器的 IP 地址,该地址也被代理以隐藏我的网络服务器的实际 IP 地址。所有流量都通过 HTTPS 路由,证书也由 Cloudflare 提供。

我的 nginx default.conf:

# Redirect all traffic to HTTPS/443/SSL
server {
    server_name example.com *.example.com;

    listen 80;

    return 301 https://$host$request_uri;
}

# example.com
server {
    server_name example.com;

    listen 443 ssl;

    index index.php index.html;

    ssl_certificate /etc/ssl/origincert.pem;
    ssl_certificate_key /etc/ssl/privkey.key;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /website;

    location ~ \.php$ {
        autoindex on;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

# test.example.com -> proxy pass to 8183
server {
    server_name test.example.com;

    listen 443 ssl;

    ssl_certificate /etc/ssl/origincert.pem;
    ssl_certificate_key /etc/ssl/privkey.key;

    location / {
        proxy_pass http://MY_SERVER_IP_ADDRESS:8183;
    }
}

但是,当我导航到https://test.example.comhttp然后我只是重新路由到https://example.com. 我在已example.com, *.example.com, and test.example.com安装和使用的服务器上安装了 Origin Server Certificates。

标签: nginxcloudflarenginx-reverse-proxy

解决方案


这可能是 DNS 记录的问题。检查它们并确保没有像*.example.com -> example.com 确保test子域直接指向您服务器的 IP 地址这样的记录,与example.com.

所以如果你有 example.com -> 1.2.3.4 确保你也有 test.example.com -> 1.2.3.4.和没有test.example.com -> example.com


推荐阅读