简体   繁体   English

Apache代理与PHP代理

[英]Apache Proxy vs PHP Proxy

Say I have a backend application on an internal server. 假设我在内部服务器上有一个后端应用程序。 Say I have another webserver running Apache and PHP which must proxy the first app. 假设我有另一个运行Apache和PHP的网络服务器,该服务器必须代理第一个应用程序。

On one hand, I can leverage ProxyPass with Apache with: 一方面,我可以将ProxyPass与Apache配合使用:

ProxyPass / http://myhiddenserver.com/ 

On the other hand, I could do a simple php script using libcurl to implement the same proxy behavior. 另一方面,我可以使用libcurl做一个简单的PHP脚本来实现相同的代理行为。

curl_exec( $ch );

What are the cons of the second option compared to the first one? 与第一种选择相比,第二种选择有何缺点? I'm interested in whether there might be bottlenecks being introduced by the PHP scrip t or if the two solutions are kind of equivalent. 我对PHP scrip t 是否可能引入瓶颈或两种解决方案是否等效感到兴趣

I'm talking about servers under high load. 我说的是高负载下的服务器。 While using Apache is obviously straighforward, the PHP will give me a lot of freedom if I want to do conditional stuffs before proxying (like particular caching). 尽管显然使用Apache是​​很简单的,但是如果我想在代理之前进行条件操作(例如特定的缓存),PHP将给我很大的自由度。

Thanks! 谢谢!

If you use PHP for proxy requests, your web server will also be involved in it, according to this using only the web server will be better. 如果将PHP用于代理请求,则Web服务器也将参与其中,根据此方法,仅使用Web服务器会更好。
But if you want to add some custom logic to the process, it's better to use PHP. 但是,如果要在流程中添加一些自定义逻辑,则最好使用PHP。 In any case, these solutions are not equivalent. 无论如何,这些解决方案是不等效的。

Also you should look at Nginx for proxy, it's may be more efficient solution: 另外,您应该查看Nginx的代理,它可能是更有效的解决方案:
https://serverfault.com/questions/143238/nginx-vs-apache-as-reverse-proxy-which-one-to-choose https://serverfault.com/questions/143238/nginx-vs-apache-as-reverse-proxy-which-one-to-choose

Apache lives in front of your PHP application. Apache位于PHP应用程序的前面。 Apache is always going to receive the web request. Apache总是会收到Web请求。 So the real question is, should PHP also receive a web request that Apache could have handled on its own, or should you bring PHP into it? 因此,真正的问题是,PHP是否还会收到Apache可以自己处理的Web请求,还是应该将PHP引入其中?

Your response time will be faster if the request never gets to the PHP app, as there will be 1 less set of code involved (probably only milliseconds, but milliseconds can matter at times). 如果请求永远不会到达PHP应用程序,则您的响应时间将更快,因为所涉及的代码集少了1组(可能只有几毫秒,但有时几毫秒也很重要)。 It will also be cheaper for you in the long run to just have Apache do the work because if you have PHP do it, then that's one step closer to needing to upgrade your PHP server sooner. 从长远来看,仅让Apache完成这项工作对您来说会更便宜,因为如果您拥有PHP,那么这比需要尽快升级PHP服务器更近一步。 Web applications (PHP in this case) have a maximum amount of simultaneous requests they can handle because they have the whole framework (maybe you're using Laravel) loaded up into RAM, and simultaneous requests require more CPU power. Web应用程序(在这种情况下为PHP)具有可以处理的最大同时请求数,因为它们已将整个框架(也许您正在使用Laravel)加载到RAM中,并且同时请求需要更多的CPU能力。 So if for example your PHP server can handle 50 requests at once because that's what your hardware is capable of, well if you make PHP be a proxy server, you just used up a fair amount of your available requests. 因此,例如,如果您的PHP服务器可以一次处理50个请求,因为这正是您的硬件所具有的能力,那么,如果您使PHP成为代理服务器,那么您就用光了相当多的可用请求。 Apache will wait until you have a free PHP worker to fulfill the request, but that waiting means a slower response for users using your PHP application as a whole. Apache将一直等到您有一个免费的PHP工作人员来满足该请求,但这对于整个使用您的PHP应用程序的用户来说意味着较慢的响应。

How big of a difference it is could be measured by benchmarks and load testing. 可以通过基准测试和负载测试来衡量差异有多大。 You may find that it's negligible. 您可能会发现它可以忽略不计。 Maybe this proxy feature will be very unused. 也许此代理功能将非常不用。 But if you have the capabilities to just configure Apache to do the work, and Apache can meet all of your needs, then I would have Apache do it. 但是,如果您具有仅配置Apache来完成工作的能力,并且Apache可以满足您的所有需求,那么我将让Apache来完成。

Some people may prefer to do it in PHP if they have a team that is proficient in PHP and not Apache, because doing it in PHP makes it easier for them to manage. 如果有人拥有精通PHP而不是Apache的团队,则有些人可能更喜欢用PHP进行操作,因为用PHP进行操作使他们更易于管理。

In the end this is sort of an opinion question. 最后,这是一个意见问题。

But the concrete unopinionated answer is that if you can do it in Apache and feel comfortable with managing it in Apache instead of PHP, you will need to upgrade your PHP server less soon which means it will be cheaper for you, and will have marginally faster response times. 但是具体的建议是,如果您可以在Apache中进行操作并且对在Apache(而不是PHP)中进行管理感到满意,那么您就需要减少PHP服务器的升级时间,这意味着它对您来说更便宜,并且速度会更快。响应时间。 (By how much depends on response times and web traffic volume and other factors.) (多少取决于响应时间,Web流量和其他因素。)

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

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