配置通过Nginx反向代理访问的Piwik

时间:2015-12-27 07:28:33

标签: nginx reverse-proxy matomo

我有以下设置

  • https://example.net(我的网站在Nginx上运行)
  • nn.nn.nn.nn(我的Piwik服务器只能通过其IP地址访问)

在我的网页上,我有通常的Piwik片段

public ActionResult notCk_Pk(String FirstName,String LastName,int? Salary,String Gender)
        {

            List<Counting> l = new List<Counting>();
            //String FirstName = FirstName;
            //String LastName = LastName;
            //int Salary = Salary;
            //String Gender = Gender;

                string query = "select * from tblEm where FirstName=@FirstName";
                string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
                SqlDataAdapter da;
                DataSet ds = new DataSet();
                using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
                {
                    using (SqlCommand cmd = new SqlCommand(query, connection))
                    {
                        cmd.Parameters.AddWithValue("@FirstName",FirstName);
                        //if (String.IsNullOrEmpty(n.FirstName))
                        //{
                        //    cmd.Parameters.AddWithValue("@FirstName", DBNull.Value);
                        //}
                        connection.Open();
                        Object o=cmd.ExecuteScalar();
                        if (o != null)
                        {
                            string city = o.ToString(); 
                        }
                        connection.Close();
                    }
                }
                da = new SqlDataAdapter(query, ConnectionString);
                da.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    l.Add(new Counting() { FirstNamecount = Convert.ToInt32(dr[0]), LastNamecount = Convert.ToInt32(dr[1]), Salary = Convert.ToInt32(dr[2]), Gendercount = Convert.ToInt32(dr[3]) });
                }



            var todoListsResults = l.Select(
                  a => new
                  {

                      a.FirstNamecount,
                      a.LastNamecount,
                      a.Salary,
                      a.Gendercount

                  });

            var jsonData = new
            {
                //   total = totalPages,
                // page,
                //records = totalRecords,
                rows = todoListsResults
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);




        }

将Nginx配置为

var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
 var u="https://example.net/piwik/";
 ...

从Piwik文档中我发现我还需要对 location ^~ /piwik/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass https://nn.nn.nn.nn/piwik/; } 文件进行一些更改

config.ini.php

通过此设置,我可以登录Piwik并通过网址[General] proxy_client_headers[] = HTTP_X_FORWARDED_FOR proxy_host_headers[] = HTTP_X_FORWARDED_HOST trusted_hosts[] = nn.nn.nn.nn trusted_hosts[] = example.net 进行管理。当我浏览我网站上的网页时,我发现Piwik的活动正如预期的那样。但是,记录的IP地址顽固地停留在https://example.net/piwik而不是相关访问者的真实IP地址。

显然,我在这里做错了,但我不确定它可能是什么。我希望这里的某个人能够提供正确的答案。

2 个答案:

答案 0 :(得分:2)

为了让其他人遇到这个线程的好处,我正在重现最终适合我的配置

    location ^~ /piwik/ {
    proxy_pass https://nn.nn.nn.nn/piwik/;
    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-Host $host;
  }

Piwik方 config.ini.php包含

[General]
proxy_client_headers[] = "HTTP_X_FORWARDED_FOR"
proxy_client_headers[] = "X-Real-IP"
proxy_host_headers[] = "HTTP_X_FORWARDED_HOST"
proxy_ips[] = "nn.nn.nn.nn"

请注意,分配需要引用字符串。

答案 1 :(得分:0)

您已将Piwik配置为期望X_FORWARDED_FORX_FORWARDED_HOST标头,然后无法在反向代理中设置它们。尝试:

location ^~ /piwik/ {
    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-Host $host;
    proxy_pass https://nn.nn.nn.nn/piwik/;
}
相关问题