是保活的行为可以被控制的.htaccess文件。首先由印刷检查服务器设置$_SERVER
,如果
[HTTP_CONNECTION] => keep-alive
在那里,那么你只需要在您设置的.htaccess文件。添加下面一行在年底的.htaccess在项目的根目录下的文件。
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
来自 http://wenda.mojijs.com/Home/question/detail/id/2200566/p/1/index.html
I hope you know that slow web pages scare away visitors, while fast pages lead to higher visitor engagement, retention, and conversions. Here are some suggestions to optimize your web site to get large performance wins with the least development effort. You can use Google PageSpeed to verify that your settings are working.
“The task is to extract the maximum amount of milk with the minimum of moo. And I am afraid to say that these days all I get is moo.”
— (Terry Pratchett, Jingo)
Enable “Keep-Alive”
HTTP keep-alive, also kwown as HTTP persistent connection, or HTTP connection reuse, means using a single connection to send multiple HTTP requests from the browser to the server.
If Keep-Alive is not enabled, the browser needs to open a new connection for every single component of a web page, like images, style sheets and javascripts. That means the number of connections opened on the server can be ten fold compared to having Keep-Alive enabled.
Enable Compression
Compressing resources with gzip or deflate can reduce the number of bytes sent over the network. Text based resources like HTML, CSS and javascsript can easily be reduced in size by 30-50% by utilizing the automatic compression of web servers.
Enable Browser Caching
Your web site most likely contains many images and CSS/ javascript resources that doesn’t change very often. If your server isn’t correctly configured, every time a user loads a page the browser checks whether these resources have changed. Setting an expiry date or a maximum age in the HTTP headers for such resources tells the browser to user a previously downloaded local copy without even checking if the file has changed on the server.
Sometimes this can cause problems: you make changes to a CSS or javascript file, but the user browsing the site still sees the old version.
Complete optimized .htaccess file for your web site
Below is a .htaccess script that does all the optimazions mentioned above. First, it enables Keep-Alive. Secondly, it enables browser caching of images (for two weeks) and CSS/javascript/flash scripts (for two days). You can easily modify these time limits according to your needs. Finally, it enables compression of all requested files, exept images (gif, jpg, jpeg, png) as those are already compressed. It contains a workaround for Netscape 4.0 as it has some problems handling compressed files.
#Headers
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
<IfModule mod_expires.c>
ExpiresActive On
<FilesMatch "\.(ico|jpg|jpeg|png|gif)$">
ExpiresDefault "access plus 2 weeks"
</FilesMatch>
<FilesMatch "\.(js|css|swf)$">
ExpiresDefault "access plus 2 days"
</FilesMatch>
</IfModule>
<IfModule mod_deflate.c>
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \\.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
Apache HTTP Optimization
来自
https://wikgren.fi/optimize-your-website-htaccess/What is keep alive?
- Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request.
- More simply put, it is a communication between the web server and the web browser that says "you can grab more than just one file at a time".
- Keep alive is also known as a persistant connection
How to enable keep-alive
- Keep-alive is enabled using the "Connection: Keep-Alive" HTTP header
- If keep-alive is not enabled it is likely your HTTP headers are stating "connection: close"
- Change that to "connection: keep-alive" to enable keep-alive.
- Enabling keep-alive depends on what server you are using and what you have access to. We cover the most common methods below.
Enable keep-alive using .htaccess
If you do not have access to your webserver config file you can enable keep-alive yourself using an .htaccess file.
<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>
Adding this to your .htaccess file will add keep alive headers to your requests, which will override most webserver or host limitations.
Enable keep-alive in Apache
If you are able to access your Apache config file, you can turn on keep-alive there. The applicable sections are shown below
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 100
Enable keep-alive in NGINX
Keep alive issues can be tackled using the HttpCoreModule. there is a specific directive you should look out for... "keepalive_disable". If you see this make sure you know why it is disabling keep-alive before removing.
Enable keep-alive in Litespeed
Keep-alive is default but your server may be using what is called "smart keep-alive". This is a setting within Litespeed that is specifically for high volume websites. When this setting is on it will appear to pagespeed tools that keep-alive is disabled.
"Smart keep alive" will request the initial file (the HTML file) with a connection close in the HTTP header. It will then request all the other files (css, js, images, etc.) with keep alive enabled. This allows more users to be able to connect at once when there are many concurrent requests.
Tip (litespeed servers only): Unless you are indeed a very high traffic site you can (and probably should) disable smart keep alive in the config and once you do so all of your connections will use keep alive.
How to determine if keep-alive is enabled on my pages?
The pagespeed tool reports on keep-alive status as well as several other factors.
Why is keep-alive used?
In order to display webpages a browser must request files from a web server somewhere. There is a brief communication where the browser asks for a file and the web server says yes or no.
The browser gets the HTML file and reads it. The browser will then request the other things that the HTML references like CSS, javascript or images.
Webpages are often a collection of many files and if a new connection (the brief communication) has to be opened for each and everyone of those files it could take significantly longer to display that webpage.
When keep alive is not enabled this process can increase the time it takes to download the page and waste server resources.
Isn't keep alive on by default?
Some people mistakenly believe that they do not have to worry about this because HTTP connections nowadays are by default persistent (keep-alive enabled).
While this is true, many people use shared hosting environments or web servers that may close connections unbeknownst to the user. This is done for performance reasons and since millions of pages are hosted in shared environments, there is a definite need to determine if your connections are keep-alive. You can do so by using the page speed tool.
来自 https://varvy.com/pagespeed/keep-alive.html