简体   繁体   English

使用 php 脚本接收 http 请求(来自 nginx proxy pass)

[英]Receiving http request with php script (from nginx proxy pass)

I'm trying to use data sent by nginx's proxy_pass directive in a php script.我正在尝试在 php 脚本中使用 nginx 的 proxy_pass 指令发送的数据。

Normally this is done with a proper web server, but I thought this might be a simpler and more reliable approach for very basic use cases.通常这是通过适当的 web 服务器完成的,但我认为这对于非常基本的用例来说可能是一种更简单、更可靠的方法。

This is the relevant nginx configuration:这是相关的 nginx 配置:

location ~ \.htm$ {
    post_action /test;
    }

location = /test {
    internal;
    proxy_method POST;
    proxy_pass https://some.website/test.php;
    }

This is the php script:这是 php 脚本:

<?php

$headers = getallheaders();
$useragent = $headers['User-Agent'];
$languages = $headers['Accept-Language'];
$md5 = md5($useragent + $languages);
$endpoint = "https://some.webserver/endpoint";
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $md5);
curl_exec($ch);
curl_close($ch);

So, the intended functionality is that whenever a.htm page is loaded, the data is proxy_passed to some.website, processed and then sent further to some.webserver.因此,预期的功能是,无论何时加载 a.htm 页面,数据都会被代理传递到 some.website,进行处理,然后进一步发送到 some.webserver。

This doesn't seem to work... however:这似乎不起作用......但是:

  1. If I use Postman to send a Post request to https://some.website/test.php , it works.如果我使用 Postman 向https://some.website/test.php发送 Post 请求,它会起作用。

  2. If I proxy_pass directly to https://some.webserver/endpoint , it works.如果我直接将 proxy_pass 传递给https://some.webserver/endpoint ,它就可以工作。

So what am I missing?那我错过了什么?

Turns out it was a ssl issue, adding proxy_ssl_server_name on;结果是 ssl 问题,添加了proxy_ssl_server_name on; to the nginx block fixed it.到 nginx 块修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM