Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将爬虫重定向到NGINX中的内部微服务_Php_Reactjs_Nginx_Facebook Opengraph - Fatal编程技术网

Php 将爬虫重定向到NGINX中的内部微服务

Php 将爬虫重定向到NGINX中的内部微服务,php,reactjs,nginx,facebook-opengraph,Php,Reactjs,Nginx,Facebook Opengraph,我正在运行一个客户端渲染的React应用程序,它是使用createreact应用程序构建的,我需要使用它来获得OpenGraph元标记。我已经编写了一些PHP(基于此),其设计仅为基于JSON文件内容的特定页面提供OpenGraph元标记。我需要做的是将爬虫用户代理的请求从NGINX内部传递到这个PHP页面 server { server_name example.com www.example.com; root /var/www/example; index ind

我正在运行一个客户端渲染的React应用程序,它是使用createreact应用程序构建的,我需要使用它来获得OpenGraph元标记。我已经编写了一些PHP(基于此),其设计仅为基于JSON文件内容的特定页面提供OpenGraph元标记。我需要做的是将爬虫用户代理的请求从NGINX内部传递到这个PHP页面

server {
    server_name example.com www.example.com;

    root /var/www/example;
    index index.html;

    listen 80;

    location @crawler {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index crawler.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        if ($http_user_agent ~* "linkedinbot|googlebot|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot|developers\.google\.com") {
            proxy_pass @crawler;

        }
        try_files $uri /index.html;
    }
}
这导致NGINX失败,出现以下错误:

May 10 00:01:59 ip-172-31-14-46 nginx[10400]: nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/example.com:23
May 10 00:01:59 ip-172-31-14-46 systemd[1]: nginx.service: Control process exited, code=exited status=1
May 10 00:01:59 ip-172-31-14-46 systemd[1]: Reload failed for A high performance web server and a reverse proxy server.
供参考-以下是PHP文件的内容:

<?php
// 1. get the content Id (here: an Integer) and sanitize it properly
$uri = $_SERVER[REQUEST_URI];
$hash = hash('md5', $uri);

// 2. get the content from a flat file (or API, or Database, or ...)
$contents = file_get_contents("./meta/". $hash . ".json");
$data = array();
if ($contents) {
    $data = json_decode($contents);
}
$data = array_merge(json_decode(file_get_contents("./meta/default.json")), $data);

// 3. return the page
return makePage($data); 

function makePage($data) {
    // 1. get the page
    $pageUrl = "https://example.com" . $uri;
    // 2. generate the HTML with open graph tags
    $html  = '<!doctype html>'.PHP_EOL;
    $html .= '<html>'.PHP_EOL;
    $html .= '<head>'.PHP_EOL;
    $html .= '<title>'.$data->title.'</title>'.PHP_EOL;
    $html .= '<meta property="og:title" content="'.$data->title.'"/>'.PHP_EOL;
    $html .= '<meta property="og:description" content="'.$data->description.'"/>'.PHP_EOL;
    $html .= '<meta property="og:image" content="'.$data->poster.'"/>'.PHP_EOL;
    $html .= '<meta http-equiv="refresh" content="0;url='.$pageUrl.'">'.PHP_EOL;
    $html .= '</head>'.PHP_EOL;
    $html .= '<body></body>'.PHP_EOL;
    $html .= '</html>';
    // 3. return the page
    echo $html;
}

从错误中,您似乎缺少传递到
代理传递的地址上的URL前缀,可能应该是:
fastcgi\u传递http://unix:/run/php/php7.0-fpm.sock


有关同一问题,请参阅此问答:

当您说Nginx失败时,您是否有更多信息?可能是个错误?在主要问题中添加了错误-感谢您指出这一点!谢谢你的指针-我肯定需要在爬虫程序位置块中查看一些东西,但不幸的是,错误消息指的是第23行,即这一行:proxy_pass@crawler;这不是因为您正在将@crawler配置传递给第23行中的proxy_pass,其中包括一个不包含URL前缀的fastcgi_pass吗?让我去看看-我没想到!(我的辩护是凌晨1点刚过)。刚试过这个,它失败了,在fastcgi_传球线上出现了不同的错误信息。我很确定fastcgi_通行证是正确的——正如您提到的链接StackOverflow问题所说的是代理通行证而不是fastcgi_通行证——我认为他们对链接有不同的要求。5月10日00:37:04 ip-172-31-14-46 nginx[10652]:nginx:[emerg]上游无效主机“”,in/etc/nginx/sites enabled/example.com:XX