Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

www.google-analytics.com无法作为合法域名添加 #4

Closed
rchunping opened this issue Dec 19, 2017 · 4 comments
Closed

www.google-analytics.com无法作为合法域名添加 #4

rchunping opened this issue Dec 19, 2017 · 4 comments

Comments

@rchunping
Copy link
Owner

rchunping commented Dec 19, 2017

因为www.google-analytics.com域名没有国内备案,无法添加到微信小程序的request合法域名中。

因此需要对原本发送给www.google-analytics.com的数据做转发。

假设你有一个已备案域名example.com,那么可以用一个二级域名ga-proxy.example.com来接收跟踪数据。

具体方法:

  1. 在跟踪器上设置自定义跟踪服务器 tracker.setTrackerServer("https://ga-proxy.example.com")
  2. 修改域名example.com的DNS记录,将 ga-proxy.example.com 指向你自己的服务器IP
  3. 自己服务器上的nginx做如下配置
upstream real_ga_servers {
        server www.google-analytics.com:443 weight=5 max_fails=0;
        keepalive 64;
}

server {    
   listen 443 ssl;
   server_name ga-proxy.example.com;
   
   ssl on;
   # 设置ssl证书(略)

    location / {

        # 告诉ga用户真实ip
        rewrite ^(.*)$   $1?uip=$remote_addr    break;
    
        proxy_set_header   Host       www.google-analytics.com;

        # Proxy to google-analytics.com
         proxy_buffering off;

        # 使用keepalive
         proxy_http_version 1.1; # require  nginx > 1.1.4
         proxy_set_header Connection ""; # for keepalive upstream

         proxy_pass https://real_ga_servers;
         proxy_redirect off;

    }
}

ga-proxy.example.com 的ssl证书配置请参考相关资料(这里略过)

另外,www.google-analytics.com国内有服务器,因此并不需要你的服务器有翻*墙能力就可以转发跟踪数据。

@xianyuxmu
Copy link

xianyuxmu commented Jan 24, 2019

亲测可行!如果各位是使用docker nginx容器实现的话,要注意ssl证书安装问题,因为一般ssl是安装SLB层,nginx收到流量时是80端(非443)。因此,使用docker nginx容器做转发时,需要对上面的配置中的server做如下的更改:

server {
    # listen 443 ssl; # 删除这一行
    # ssl on; # 删除这一行
}

@milkdeliver
Copy link

亲测可行!如果各位是使用docker nginx容器实现的话,要注意ssl证书安装问题,因为一般ssl是安装SLB层,nginx收到流量时是80端(非443)。因此,使用docker nginx容器做转发时,需要对上面的配置中的server做如下的更改:

server {
    # listen 443 ssl; # 删除这一行
    # ssl on; # 删除这一行
}

请问Dockerfile你是如何配置的?我按照官网的配置,起来之后直接就马上被关掉了。万分感谢。

FROM nginx
COPY nginx.conf  /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]

感谢
Sam

@xianyuxmu
Copy link

xianyuxmu commented Feb 22, 2019

@milkdeliver

配置如下:

Dockerfile:

FROM nginx:1.15.8
COPY nginx-ga-proxy.conf /etc/nginx/conf.d/nginx-ga-proxy.conf
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf:


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" 真实_REAL_IP: "$http_x_forwarded_for"';

    log_format  custom_main  '来自ga-proxy.<your-domain>.com日志: $remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" 真实_REAL_IP: "$http_x_forwarded_for" 解析后的REALIP: "$realip"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

nginx-ga-proxy.conf:

upstream real_ga_servers {
    server www.google-analytics.com:443 weight=5 max_fails=0;
    keepalive 64;
}

server {
   server_name ga-proxy.<your-domain>.com;

    location / {

        set $realip $remote_addr;
        if ($http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
            set $realip $1;
        }

        access_log  /var/log/nginx/access.log  custom_main;

        # 告诉ga用户真实ip
        rewrite ^(.*)$   $1?uip=$realip    break;
    
        proxy_set_header   Host       www.google-analytics.com;

        # Proxy to google-analytics.com
         proxy_buffering off;

        # 使用keepalive
         proxy_http_version 1.1; # require  nginx > 1.1.4
         proxy_set_header Connection ""; # for keepalive upstream

         proxy_pass https://real_ga_servers;
         proxy_redirect off;

    }
}

@milkdeliver
Copy link

@xianyuxmu
非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants