Answer the question
In order to leave comments, you need to log in
Is it possible to optimize the return of a static web server using PHP?
Recently, the site hosted by Peterhost, even under a small load, began to work slowly (I do not leave a link to the site and to the hosting, so as not to be counted as advertising).
Today I found a bottleneck in the site.
It just so happened that there is not enough space on the hosting in sufficient quantities. Some pictures have to be stored on another hosting. But at the same time, for SEO reasons, for better indexing, images should be on the same domain as the site.
Two years ago, a script-proxy of images was written to solve the problem.
<?php
$imgs = file('./img-links.txt');
$addr = trim($imgs[$_REQUEST['id']]);
header('Content-type: image/jpeg');
readfile($addr);
Answer the question
In order to leave comments, you need to log in
Unfortunately, hosting managers kick us out everywhere because of the load. They offer dedicated services.
We tried VDS 4GB of memory, 1 core 3.2GHz, FreeBSD. - the site can not work even 20 minutes. We look at the top server - more than 10 thousand httpd processes are hanging. It works more stable on hosting, and most importantly, it works. But we are kicked out of all hosting instead of offering solutions to the problem.
The fact is that on one page, on average, 50-300 pictures are loaded. One request per page = 50 - 300 requests to apache+php.
it's easier and faster to raise nginx and set up image proxying on one upstream, and everything else on another.
You can also send the most frequently downloaded images from the main hosting (for example, place all images on the secondary hosting, and cache frequently downloaded images on the main hosting)
Maybe I don’t know something, but isn’t distributing static from a subdomain SEO friendly?
" They offer dedicated services. We
tried VDS 4GB of memory, 1 core 3.2GHz, FreeBSD. - the site cannot work even for 20 minutes."
“99% of the load is exactly in the place that I indicate in the question.”
“The fact is that on one page, on average, 50-300 pictures are loaded. One request per page = 50 - 300 requests to apache+php.
1. Take VDS.
2. Switch to Nginx with proxy as you were told.
3. PROFIT! (99% of the load is gone)
Take advantage of CDN services. cloudflare.com for example.
And give the Expire headers for a month for statics so that the browser does not pull each time.
In general, you should optimize the site code ... vds of this configuration should keep a very high load.
Well, in general, if it’s wise, then giving statics using php is fundamentally wrong, but here you have a reason for this.
I would still transfer it to vds, rebuild everything well, optimize the code, set up nginx to proxy to your second hosting, and I didn’t forget about caching of rendered pages (not dynamic ones, since you can configure anything in nginx) .
If you work with what is on the principle of minimal changes, I would first cache this
$imgs = file('./img-links.txt');
$addr = trim($imgs[$_REQUEST['id']]);
Ask support about X-accel-redirect + caching such requests.
If the nginx Accel response is from the backend, then it will not access the scripts, but will return the desired picture itself.
If you manage to do something like that, it will be the best solution.
files on subdomain
in subdomain in robots.txt
User-agent: *
Host: main domain.
1) make a separate domain for statics
2) put a balancer on this domain - for example, static.site.com -> 0.static.site.com, 1.static.site.com, etc.
3) update the setting - because a bunch of nginx + php_fpm 97% of cases faster than httpd + mod_php (if there are no explicit links to httpd)
4) VDS - depending on which hoster, RU usually has an oversell - that (1) you get less % CPU (2) network without You are utilized to the limit, and hosts often set only 100Mbps
PS I’ll add on my own - go to the cloud, for example, Joyent has a small instance that is inexpensive + the ability to scale on the fly (though then it’s better to use Joyent SmartOS - it will be more convenient) + 10Gbps shared shared port (guaranteed speed is equal to the share of resources from RAM - i.e. .if you bought 1GB instance - you will get guaranteed - 10Gbps/80*1 = 125Mbps of theoretical network speed (in real life it will be up to 100Mbps)) and 20TB/mo./instance of premium traffic.
I read everything that was advised before me. Everyone speaks correctly. But in this case, I would exclude php from the chain in this way (for example):
1. You create symbolic links to all the files you need in the directory (with the ln -s command), naming the links by their ID (as described in your own img- links.txt). It is only necessary in the background (by hand or by cron) to keep the links relevant to the necessary files (so that the links correspond to img-links.txt). Then you will not need to load php for each request, but you can simply send files using httpd (apache or nginx - it doesn’t matter).
2. If the IDs do not have extensions, then you will have to specify the file type in the correspondence table (which is also not difficult).
3. I support the advice on configuring expire for statics for at least a few days (weeks are better).
As a result, requests will be processed faster and, possibly, will not fall into the disk swap and the problem will be solved. (it’s impossible to say for sure without knowing the hosting parameters and your traffic)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question