K
K
kocherman2012-11-04 19:16:48
PHP
kocherman, 2012-11-04 19:16:48

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);

The script is somewhat simplified for clarity.
He gave the name of the hosting company not by chance. The tariff we use has support for perl and c/c++ scripts.
I want to ask the community if it makes sense to remake proxies in perl or c++? Will the load on the processor and memory drop?
How to implement easier and faster?

The option of changing hosting is also not considered. On cheap VDS, the site works slower than on shared hosting, because. shared hosting has a lot of resources available. Switching to a dedicated server is also not considered due to the high cost of rent.

UPDATEIt doesn't make much sense to cache images, since almost all images are loaded approximately the same number of times. We use google-picasa, dropbox and some other free services for image hosting. There are many accounts - there is no way to pay for 100-gigabyte hosting. So that sites do not lose seo, you have to hide the real placement of images for PS. In addition, the project is designed for users coming from the PS.

The main load is given not by the work of the script itself, but by the creation of a PHP instance.

I would also like to note the direct minimization of time and money spent on the project.
If we imagine that we do not have a single project, but, for example, 10. Pay for 10 dedicated servers, monitor their performance. In this case, I will at least have to retrain from a programmer to a system administrator. Even now there is not enough time to implement certain things. If you still deal with the settings and support of the infrastructure, then you can generally dig in ...

Answer the question

In order to leave comments, you need to log in

12 answer(s)
D
Denis Pushkarev, 2012-11-04
@rock

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.

So put nginx on VDS instead of Apache, there will not be so many processes to produce - once, to give its statics using means without PHP - two. Your rate per eye is enough. Apache + php - a couple of processes for each connection, count what you get in your pictures - no physical servers are enough. Nginx will hang several processes and process requests sequentially - and, thanks to Google, eat 2.5 MB of memory for 10k inactive connections. Well, if you can’t get by with one nginx, which is unlikely, php-fpm is behind it, so that it doesn’t produce extra processes.

A
Anastasia_K, 2012-11-04
@Anastasia_K

it's easier and faster to raise nginx and set up image proxying on one upstream, and everything else on another.

V
vsespb, 2012-11-04
@vsespb

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)

@
@ngreduce, 2012-11-04
_

Maybe I don’t know something, but isn’t distributing static from a subdomain SEO friendly?

O
Oleg Karnaukhov, 2012-11-05
@BupycNet

" 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)

E
Evgeny Kuznetsov, 2012-11-04
@evgentus

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.

E
Evgeny Kuznetsov, 2012-11-04
@evgentus

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) .

V
v0s, 2012-11-04
@v0s

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']]);

(I would transfer the list of links into a puffy array, which will be cached by the opcode cache)
and secondly, I would give the client 301 moved permanently with a real link - let him download it himself.
This is provided that such an approach does not reduce the “seoshnosti” and it is not worth the task of hiding the real location of the pictures from everyone.

L
la0, 2012-11-04
@la0

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.

M
max_rip, 2012-11-04
@max_rip

files on subdomain
in subdomain in robots.txt
User-agent: *
Host: main domain.

Q
qxfusion, 2012-11-05
@qxfusion

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.

A
AHTOH, 2012-11-05
@AHTHOH

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 question

Ask a Question

731 491 924 answers to any question