I have a website built in Symfony 2.8 running on Apache on Ubuntu Server. I had some broken image links on the website, and whenever somebody went to the offending page, it would peg out both the processor and memory, and the browser would continue to try and fetch the missing assets for 60 seconds plus.
I've fixed the missing images, but is there something I can do on the server end to quit searching for missing assets when they're clearly not there?
The way it stands now it would be very easy for an attacker to simply demand content from my website that isn't there, and bring my server to it's knees.
--Edit--
What I'm wondering if there is a way to stop my server from taking a resource hit if somebody tries to access a path/file that's non-existent directly. i.e. if they went directly to example.com/images/non-existent.jpg. Currently on my server my CPU will spike to 100% on one core and RAM usage will slowly climb until the request times out somewhere around 1 min. Just a few more requests like that would use all of my resources.
--Edit 2--
I have discovered since I posted this, that this isn't just limited to images. Any path that should return a 404 error is behaving the same. ie example.com/this/is/not/a/real/path will just hang, and finally time out.
Check if the file exsists before including it in your HTML
if(file_exists('img.jpg')){
// your code for inserting the image into the HTML
}
http://php.net/manual/en/function.file-exists.php
Related
Not all the time, but sometimes if I visit the site as this http://WEBSITESAMPLE.com/paramA/pathB/, it becomes very slow and nothing loads till a minute or so, but if I add something dummy on the path, it loads immediately, do any of you have an idea why is it?
I am not sure what's going on here because your question lacks a little detail.
But just to make the scenario clear when it comes to Client/ Server side and accessing something on the web with a domain. You have to understand that whatever lags you experience it always points to a certain process that a line of code cannot finish the job in the desired time.
Lags sometimes caused by a script like Javascript(JS) that probably will send request from the server which server can't response in a certain time required by the script.
Also sometimes, browsing to a specified URL without a clear path to a server could cause waiting time.
anyhow, you have to inspect the path you are accessing and the source code and possible process jobs. From there I'm sure you can figure it out.
Otherwise you have to share your setup. Assuming you're the one who setup and build the site.
Problem
When you load up a page on my site, often times one (or several) images (jpg files that I have saved from Lightroom and/or Photoshop) will not appear. It instead looks like a broken link (ALT description appears) but no image. Hard reload of the browser solves problem (e.g. all images load properly after a hard reload).
Error Message
Chrome displays an "ERR_CONTENT_LENGTH_MISMATCH" warning for all images it does not load. (Sometimes the image will flash quickly before going to what looks like a dead image)
Setup
Running latest version of Wordpress (4.2.2) on a Shared Host. Site is SSL (https) if that matters. Images are located in an image upload folder (nothing complex like Imagemagick, etc) on the host.
My Troubleshooting
I have replicated the issue from multiple locations using various ISPs on various machines (both Mac & PC) and with various browsers (Chrome & Safari) some of which are not using any ad-blockers.
What I've tried is the following:
I asked the host if there was an issue on the server side. They claim no.
I've tried resetting the functions.php file. No impact.
I've disabled all plug-ins. No impact.
I've hardkeyed in the meta charset as UTF-8. No impact.
Checked if I'm using Gzip. I am not.
Enabled Wordpress Cache plugin. No impact.
Cleared .htaccess of all non-necessary redirects & commands. No
impact.
Replaced wp-admin and wp-includes folders from fresh install. No
impact.
Deleted Wordpress & Reinstalled from a Backup. No dice.
I've put source code from pages that have this issue into a test.html file and the images seem to load up fine doing that.
My Thoughts & Questions
The images are 100-200kb each and sometimes there are a fair amount of them on the page. Is something timing out and then once I hard reload, everything show up because the timeout isn't tripped? That is the best random guess I can gather without understanding the issue perfectly.
Any ideas of things I can try? Should I delete the whole database and start again? Everything I know about computers is self-taught and server issues are not a strong point for me. Even if you don't know what it might be, could someone explain what a content length mismatch is in general terms?
Thanks much!
When you request data from a web server, it responds first with some information about the data (HTTP headers) and then with the data. One of these pieces of information, an HTTP header, is called Content-Length. It tells the client how much data it should expect to receive from the server. When your browser gets an image, the server's response (very simplified looks like)
Content-Length: 100000
< the image, 100000 bytes of data >
The client knows the request is complete when it has received the amount of data told by Content-Length. Until it receives in this case 100KB (100000 bytes), it considers the image, for example, to not be done loading.
If the server breaks the request before the client receives the data from the server, or if the client receives more data than it received, the client will throw some sort of error and assume the data to be corrupted/unusable and dispose of it. How this is handled can vary between browsers.
How did you upload the images to your website? Myself, I have encountered this problem in a situation where the file's supposed size was stored in the database, and this was used to set the Content-Length header. The file size in the DB wasn't correct for the file. HOWEVER, I know that WordPress does not store file sizes in the database; media uploads are simply represented by a URL.
This could also happen if the web server runs out of resources and can no longer fulfill your requests; you said you had lots of images per page. If you are on a really lousy shared hosting plan, it may be the case that the host imposes limits, or that the server simply can't handle the traffic of all the sites it hosts.
I wanted to circle back on this in case someone else is experiencing this problem. It appears that there is some type of glitch between HTTPS and image retrieval that was causing the problem. While I don't understand WHY that is, I converted my site from SSL/HTTPS to simple HTTP (which I was able to do as it doesn't require encryption) and it appears all images load as they should.
If someone understands the "why", I'd love to understand what the issue actually is. Luckily, I was able to come up with workaround. So, while this doesn't answer my question, it does provide context of what is causing the problem and my common sense workaround.
You might see this problem with a shared hosting service. Free bandwidth is like free speech, not free beer. Resource outage policies are invoked during traffic spikes.
A distributed system architecture solves this by inserting a front-end CDN tier (eg. CloudFlare). CDNs cache your static resources and can vastly reduce the load on your host. In fact, for completely static sites the host can be shut down.
There are other advantages to CDNs, like attack detection, free SSL (not beer) and overall improved performance and security compared to shared hosting alone.
Many CDNs are free (as in speech). You could also upgrade to private hosting, but $ and you still might want a front-end tier.
Just 30 mins ago I ran into a really unexpected problem (which as a matter of fact happened to me for the 1st time in my life, and in the 10 years of coding websites).
On a particular page from my admin module, few images reverted to their old state, the exact way they were few days ago. Which means I changed them yesterday, and now they all look like they were before.
It gets more weird. I logged in with Filezilla, and deleted the images. They still appear, with the way they looked 3 days ago, before I changed them.
If I go to the URL of the image it still displays the old images. If I look locally in my Windows folder they are the new version, upload them again, still the browser displays the old version.
I erased the cache in Chrome, even restarted the computer thinking there is something to do with Chrome or something, i really ran out of options. Firefox displays the same, the old images (even if i uploaded the new version of images).
I tried with CPanel FTP manager and...surprise...if i look at an image, it's the good version of images (the new one!). But all browsers fail to see the image.
Also as a keypoint, if I erase the image with ftp, the browser still sees it (SHIFT + f5, still it sees it, so it's not from cache). ?!?!
ANy pointing to the problem is really appreciate. Cheers
Sounds like your hosting provider has a reverse proxy that caches requests for images. One way to overcome the problem is to add something to the URL, perhaps a request parameter, that makes the proxy think that the request is not for the same image.
For example, http://www.example.com/yourimage.png?date=2012-10-25
Sounds like the hosting provider is screwing you over, so I'd say step one is to dump them and move elsewhere...
But if Joni's suggestion doesn't work, you can build your image dynamically on the server, like:
<img src="imageHandler.php?image=myimage.png" />
Your imageHandler.php code would need to read the bytes of the image, and build the HTTP response manually and pass the image bytes back. Not sure what that would look like in PHP, but it shouldn't be more than a few lines of code.
This is a terrible solution, since it would require a ton of processing and disk access for a trivial task like pulling images, but I'd expect that it would at least work.
it's the first time i answer my own question cause it was related to external factors.
yes, problem was fixed, it was actually the guys at tech. dept. at the ISP i have the files hosted, the virtual hosts config file for my domain xxx.net was ..misconfigured.
cheers all, problem is fixed
This has been frustrating me for a while now. I started developing a site for a friend using ajax to load content, but whenever the image galleries are loaded it takes soooo long. The annoying thing is that it wasn't like that when i tested it on my own server.
The test site is here: http://www.europeanbob.co.uk/phil/index.html
And the actual site is here: http://www.philmarsdenphotography.co.uk
The test is hosted on dreamhost and the actual one on krystal if that makes any difference?
You are doing some weird things, but Stack Overflow is not a debugging service. What I've seen from a cursory look at the network pane, when you click on Gallery > People, there are two simultaneous requests to /inc/people.php — one takes 4-4.5s and the other 8-9s, thus my guess is one gets locked waiting on the other to finish, which might be either due to session or database. These scripts return a bit under 4k, so the long delay cannot be explained by network latency — it is something you're doing server-side. Good luck.
Part of the problem is that you have a lot of javascript and css files which all have to load before the image loads.
Every external file that you link to (ie non-inline file) requires a separate dns lookup and then retrieval. The problem is that the browser will only look up a few at a time (a limitation of the TCP/IP protocol) and make the browser wait for those to return before looking up the next ones.
The solution is to combine some of these javascript and css files from 8 or so to 2-3. This should shave off about 2 seconds
I have a website. It's my first website with Zend Framework but I think it's written good. Generatiom time is about 0.9s now. I'll do it something like 0.2 but leave it now. When you press any link on the website it tooks about 1,5-2s before web browser is loading page. Then it tooks 0.15s to show it. So if execution time is 0.9 where are the other 1.1s? Ping is about 13ms. Website address is http://zgarnijlicke.pl
Edit:
Strange. Second domain, http://lottek.eu, is working good. Look at http://lottek.eu/picostreamer. It isn't lagging like the zgarnijlicke.pl domain.
Edit 2:
There is a problem with Zend-Framework. I setted up action without rendering view (layout disabled too) and it's working as fast as server can do it. I'll make new question for it.
Here's a webpagetest.org report for your site: http://www.webpagetest.org/result/100721_1P0Y/
If you view the waterfall graph for the first view, you'll see that the browser gets your HTML source at around the 1.2 second mark, and is first able to render your page at just after 4 seconds. What happens in between those two is downloading of your three javascript files and two CSS files. So, this is where you want to start. Some suggestions:
Consider using a free CDN for jquery.js insteading of serving it from your server, e.g. Google's: http://code.google.com/apis/ajaxlibs/ . This way, users are more likely to already have it cached, Google will serve it from a location geographically closer to the user, and (I think) in compressed format.
For jquery.corner.js and jquery.media.js, consider merging them into one file and serving them compressed (the Apache module mod_deflate makes this very easy to do)
Same for your CSS files - consider merging them into one file and serving them compressed.
Those will give you some quick wins. However there are other things you can improve:
Add width and height attributes to your image tags. Without these, some browsers will halt rendering while they download the images so that they know how much space they'll occupy. None of your image tags have these attributes.
Make sure you're using the right image format for the job. Your banner.png image is over 300k which is far too large. I converted this to a JPEG image (80% quality) and it was 30k.
As for the execution time, 0.9 seconds seems quite high. Are you using APC or similar? Is the page doing any heavy database work?
Try putting some timer code in your php that measures the length of time it takes to generate the page content. This way you can confirm or rule out server problems.
You might also use network tools like ping and traceroute to see if your problem is caused by network latency.
A quick test with wget here gets an overall execution time of 1.5s to transfer one of the pages, with an actual download time of 0.2 seconds, so 1.3s of overhead. The pause occurs before the transfer starts, so that's a server-side problem.
Is that site on a virtual server? It's possible that if the underlying physical server is heavily loaded, your image could be getting swapped out or otherwise CPU-starved and takes ~1 second to become responsive again.
Perhaps it's an internal resource thing - are you connecting to a DB, especially a remote one? Even if some or most of the pages aren't DB-driven, the overhead of connecting to a DB could be causing this slowdown. And then gets swapped/delayed again as there's little further activity to keep the image active.
It could even be something as silly as Apache being configured with 'IdentityCheck' on, though unlikely, as this would slow down all requests. I'm not seeing any slowdown on the requests for .css/.js files from your server when viewed from HTTPFox. Interestingly, requesting the .css/.js via wget returns a '500 Internal Server Error'.
I found it. It's problem with ZF because when I did hello.php page with code like that:
hello world
Without any < ?php ?> script took 0.4s to complete.