Phantom js没有捕获nginx代理后面的url的截图

时间:2015-08-05 14:12:36

标签: node.js nginx phantomjs

我试图捕获https网址的屏幕截图,使用phantomjs在nginx反向代理后面。 但是phantomjs抛出状态失败错误 这是代码

    var phantom = require('phantom');
   var options = {
       path: "/home/ubuntu/phantomjs/phantomjs-1.9.2-linux-x86_64/bin/"
   }

   var settings = {
       userName: 'ubuntu',
       password: '*****'
   };

   phantom.create("--proxy=10.3.13.4:80","--proxy-type=http", "--proxy-auth=root:root123","--ignore-ssl-errors=yes", "--ssl-protocol=any", function(ph) {
       console.log('ph', ph);
       ph.createPage(function(page) {
           page.settings = settings;
           page.open("https://ubuntulocal.com/", function(status) {
               console.log("opened url? ", status);
               page.evaluate(function() {
                   return document.title;
               }, function(result) {
                   console.log('Page title is ' + result);
                   setTimeout(function() {
                       page.render('test.png');
                       ph.exit();
                   }, 15000);

               });
           });

       });
   }, options);

这是nginx配置

server {
    listen 443;
    server_name ubuntulocal.com;
    ssl on;
    ssl_certificate /etc/ssl/es_domain.crt;
    ssl_certificate_key /etc/ssl/es_domain.key;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log debug;
    location / {
        rewrite ^/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:3000;
        proxy_redirect http://localhost:3000 https://ubuntulocal.com/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "Keep-Alive";
        proxy_set_header Proxy-Connection "Keep-Alive";
        auth_basic "Elasticsearch Authentication";
        auth_basic_user_file /etc/elasticsearch/user.pwd;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    }
}


server {
    listen 80;
    server_name ubuntulocal.com;
    return 301 https://$host$request_uri;
}

帮我解决是否有任何我错过了此代码或配置中的内容。 ,提前谢谢。

1 个答案:

答案 0 :(得分:0)

此代码适用于nginx和shield

phantom.create( "--ignore-ssl-errors=yes", "--ssl-protocol=any",function (ph) {
  ph.createPage(function (page) {

      var authentication_data = {
          "Authorization": "Basic " + new Buffer('username:password').toString('base64')
        };


  page.set('customHeaders', authentication_data, function(err) {
    page.open("https://ubunutlocal.com:8080/", function (status) {
       //Do operations
    });
  });

  });
}, options);

问题是需要在幻像创建函数中添加“--ignore-ssl-errors = yes”,“ - showl-protocol = any”参数,并且对于基本身份验证需要创建自定义标头并附加用户名和密码标题。