首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有nginx代理的Sendgrid品牌链接

带有nginx代理的Sendgrid品牌链接
EN

Stack Overflow用户
提问于 2018-08-28 15:43:34
回答 1查看 1.2K关注 0票数 2

我们尝试用这个文档https://sendgrid.com/docs/ui/account-and-settings/custom-ssl-configurations/设置一个带有自定义ssl的品牌链接。

我们遵循它,现在我们有了一个代理,带有证书,它重定向到sendgrid.net。

支持人员告诉我们,他们的测试显示“失败:我们没有从测试'https‘点击跟踪链接中得到200个响应。”并告诉我们代理上的证书通配符是不受支持的。

我不明白通配符的原因,代理发送的不是200,因为sendgrid.net发送的是404

所以我不知道该怎么做。

我们使用nginx和这个例子来实现我们的代理:https://gist.github.com/jjhiew/cbbd26da313fc550467e303a6c6f8177

EN

回答 1

Stack Overflow用户

发布于 2019-05-31 05:52:25

谢谢你的提醒。我们确实有它的工作,但我忘了在这里张贴。一般的想法是让有品牌的点击请求到我们自己的服务器,它有TLS认证。也就是说,link.mysite.com会转到我们自己的服务器,而不是SendGrid。我们的服务器接受这些请求,向SendGrid发出相同的请求。无论SendGrid向我们的服务器回复什么,我们都会将其发送回浏览器。

我不确定,但我认为SendGrid支持人员必须扳动一些开关。但这可能是错误的。我记得我和他们谈过,我记得他们不理解这种代理情况。我是否最终得到了这样做的人,或者没有他们,我是否能让它工作,我不确定。

下面是我们使用的代码(Ruby on Rails):

代码语言:javascript
复制
# Allow SendGrid Click Tracking to use HTTPS
#
# SendGrid click tracking uses the host 'link.example.com' but HSTS requires that host
# respond to HTTPS. However SendGrid does not have our certificate. So instead point
# link.example.com to this proxy, and we make the same request to sendgrid.
#
# see: https://sendgrid.com/docs/ui/account-and-settings/custom-ssl-configurations/
#
# Configuring SendGrid, Heroku, and DNSimple for Click Tracking and unsubscribes
# ------------------------------------------------------------------------------
#   Sendgrid > Sender Authentication > Link Branding
#     Create branded link for example.com
#     Advanced Settings > Custom link subdomain: link
#
#   DNS > make the CNAME records they mention
#   Sendgrid >
#     verify branded links so they are activated.
#     Make link the default.
#
#   Heroku > configure subdomain for link.example.com
#   DNS > change CNAME record so link.example.com points to Heroku, e.g. blah.blah.herokudns.com
#
#   Test:
#       Unsubscribe links that start with link.example.com/___ should work now.
#
#   Sendgrid > Tracking > Click Tracking > ON
#
#   Test:
#     Send a test Frisky Friday.
#     Follow link to article--it should start with link.example.com
#     SendGrid increments the Click Tracking counter
class SendgridLinkProxyController < ActionController::Base
  SENDGRID_CLICK_TRACKING_URL = 'https://sendgrid.net'.freeze

  def index
    # Make matching request to SendGrid
    sendgrid_url = URI.parse("#{SENDGRID_CLICK_TRACKING_URL}#{request.env['ORIGINAL_FULLPATH']}").to_s
    sendgrid_headers = { 'Host' => CFG.SENDGRID_PROXY_HOSTNAME }

    Rails.logger.info("sendgrid_link_proxy_controller.rb: fetching #{sendgrid_url}, headers: #{sendgrid_headers}")
    sendgrid_response = HTTParty.get(sendgrid_url, headers: sendgrid_headers, follow_redirects: false) # , debug_output: STDOUT)

    # Make matching response to browser
    user_response_status = sendgrid_response.code
    response.set_header('location', sendgrid_response.headers['location'])
    Rails.logger.info("sendgrid_link_proxy_controller.rb: responding status_code: #{user_response_status}, location header: #{response.header['location']}")
    render html: sendgrid_response.body.html_safe, # We are trusting SendGrid. Winston think's that's OK. [Winston Dec 2018]
           status: user_response_status
  end
end

下面是一个与之配套的RSpec文件:

代码语言:javascript
复制
require 'spec_helper'

describe SendgridLinkProxyController do

  describe '#index' do
    before do
      @sendgrid_response = {
        headers: {},
        body: '<html>SENDGRID BODY</html>',
        code: 200
      }
      request.env['ORIGINAL_FULLPATH'] = '/wf/click?upn=BLAH'
      CFG.SENDGRID_PROXY_HOSTNAME = 'link.example.com'
    end

    subject do
      allow(HTTParty)
        .to receive(:get)
        .and_return(double('Mock Sendgrid Response', @sendgrid_response))
      get :index, params: {a: 'click'}
    end

    it 'requests page from sendgrid with same path and with Host header' do
      expect(HTTParty).to receive(:get)
        .with('https://sendgrid.net/wf/click?upn=BLAH',
              headers: { 'Host' => 'link.example.com' },
              follow_redirects: false
             )

      subject
    end

    context 'when receiving a click-tracking redirect link' do
      before do
        @sendgrid_response[:code] = 302
        @sendgrid_response[:headers]['location'] = 'https://example.com/TARGET'
      end

      it 'redirects browser to target link' do
        subject

        expect(response.status).to eq(302)
        expect(response.headers['location']).to eq('https://example.com/TARGET')
      end
    end

    context 'when receiving an unsubcribe link' do
      before do
        request.env['ORIGINAL_FULLPATH'] = '/wf/unsubscribe?upn=BLAH'
      end

      it 'renders sendgrid\'s unsubscribe page' do
        subject

        expect(response.body).to eq('<html>SENDGRID BODY</html>')
      end
    end
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52052585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档