如何将 namecheap 子域与 netlify 站点一起使用

问题描述 投票:0回答:1

我有一项服务,允许您创建自己的网站,然后免费托管它。目前,它正在将文件上传到 Netlify 并使用预制的随机 URL,但我希望能够使用在我的主域 e.x thisisthemaindomain.com 下的 Namecheap 上创建的子域来访问这些新托管的站点,我想访问这个新的站点在 Netlify 上部署站点,网址如下:thisisnewtestsubdomain.thisisthemaindomain.com

主域已注册并托管在其他地方,但我想使用 Namecheap 创建子域,然后使用 Netlify 托管和部署。

def register_subdomain(domain_name, subdomain):
    print(domain_name.split('.')[0],domain_name.split('.')[1])
    params = {
    'ApiUser': api_user,
    'ApiKey': api_key,
    'UserName': username,
    'Command': 'namecheap.domains.ns.create',
    'ClientIp': '',
    'SLD': domain_name.split('.')[0],
    'TLD': domain_name.split('.')[1],
    'HostName': subdomain,
    'RecordType': 'CNAME',
    'Address': main_domain,
    }

    response = requests.get(base_url, params=params)
    if response.status_code == 200:
        if 'DomainDNSSetHostsResult IsSuccess="false"' in response.text:
            print("Failed to register subdomain. Error:", response.text)
        else:
            print(f"Subdomain '{subdomain}.{domain_name}' registered successfully.")
    else:
        print("Failed to connect to the API.")

    
# Example usage
main_domain = 'thisisthemaindomain.com'
new_subdomain = 'thisisnewtestsubdomain'
register_subdomain(main_domain, new_subdomain)

返回:子域名“thisisnewtestsubdomain.thisisthemaindomain.com”注册成功。

现在我有了 thisisnewtestsubdomain.thisisthemaindomain.com,我使用 netlify 创建一个站点:

def create_netlify_site():
    url = "https://api.netlify.com/api/v1/sites"
    payload = {
        "custom_domain": 'thisisnewtestsubdomain.thisisthemaindomain.com',
        "force_ssl": False,
        "processing_settings": {
            "html": { "pretty_urls": False }
        }
    }
    headers = {
        "Authorization": "Bearer MY_API_KEY",
        "Content-Type": "application/json"
    }

    response = requests.request("POST", url, headers=headers, data=json.dumps(payload))

    print(response)

    if response.status_code == 200:
        parsed_data = response.json()
        print(parsed_data)
    else:
        print(f"Error: {response} {response.text}")


create_netlify_site()

然后我上传文件并通过 netlify api 部署站点。

我需要做什么才能真正完成这项工作并通过创建的子域 URL 访问该网站?

我相信这就是我正在尝试做的 -> https://docs.netlify.com/domains-https/netlify-dns/delegate-a-subdomain-to-netlify-dns/

我是否可能只需要从我创建的 Netlify 站点获取 url,然后将 namecheap 上子域的 dns 记录更新为类似这样的内容?

Record Type: CNAME
Host: thisisnewtestsubdomain
Value: your-site-name.netlify.app
TTL: Default

任何指示将不胜感激,谢谢,对网站托管/DNS 配置非常陌生。预先感谢

python dns web-hosting netlify namecheap
1个回答
0
投票

虽然我无法使用 namecheap 的 API,但这部分似乎不是你的问题。配置与 Netlify 一起使用的一些建议:

  1. 如果您使用 netlify 托管您的域(不必通过他们购买,只需在那里托管 DNS),那么他们将自动创建您在网站上设置的任何子域,只需您应用它们即可。如果可以转移到他们的(免费)DNS 托管,这可能是一种更直接的方法。
  2. 但是,如果您不想这样做,您确实需要确保 namecheap 上的 CNAME 值(或 Netlify 以外的任何 DNS 主机,这将为您创建一个最适合其 CDN 的特殊 NETLIFY 记录)是
    anything.netlify.app
    。他们根据网站使用主机名的配置而不是 CNAME 的值来为网站提供服务,而且您似乎正在通过 Netlify API 进行正确设置 (
    custom_domain
    )。

PS:如果您打算使用主机名单独配置每个站点并在 namecheap 创建记录,则您不需要将子域委托给 netlify 来执行此操作!您只需要 namecheap 上的个人记录,并确保您不会意外地在 Netlify 上设置 DNS 托管,如果您这样做,将会导致我的下一条评论出现问题: PPS 现在,您无法在 Netlify 关闭强制 SSL,因此您可以跳过该配置;只要您在 Netlify 站点上配置主机名后 3 天内(正确)在 namecheap 配置 DNS,Netlify 就会自动为您提供 SSL 证书。如果您等待超过 3 天,则必须手动更改该自定义域设置,或手动调用 SSL 续订/配置 API:

https://open-api.netlify.com/#tag/sniCertificate /操作/provisionSiteTLSCertificate

© www.soinside.com 2019 - 2024. All rights reserved.