"Connection: Keep-Alive, close" as Response Headers - .htaccess

The question that I have might be pretty basic, but I can't find any solution. I'm trying to make keep-alive works, but it seem as if it was impossible as long as I get "Connection: keep-alive, close" as Response Headers.
I have the following code in my .htaccess, but it doesn't make any change:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
What can I do?

You are getting both keep-alive and close because you need to edit this at the server conf level (ie: your vhost or httpd.conf)
If you simply do this through .htaccess it'll append the keep-alive to the close coming from your .conf. If you have access to the confs, try to update it to the following:
KeepAlive On
KeepAliveTimeout 15
MaxKeepAliveRequests 100

Related

Magento keep alive response: connection close

I am trying to add a KeepAlive statement to my htaccess. I've set it like this:
<IfModule mod_headers.c> Header set Connection keep-alive </IfModule>
I i tested this, I got the response close on the connection!
Does someone know how to resolve this?
I am on magento 1.7.
For those who search for an answer:
KeepAlive have to be activated by your Hoster. Then you can enable it in your htaccess.

Apache , NodeJs, Server Sent Events

First of all, I know (Putting Node under Apache) this is not the approach to go but due to time constraint I cannot experiment.
I am trying to use Server Sent events for a mobile application. After reading all over Net, I figured out that Nodejs is the server to go for. My 80/443 port is occupied by Apache Web server, so I want Node To run behind Apache.
The Problems which I am facing are:-1. I am not able to get the close/end events on refreshing browser or closing browser rather I get after a fixed certain amount of delay, so not able to maintain when the client shuts down connection in Nodejs.
req.on("close", function() {
removeConnection(res);
console.log('Connection closed');
});
2. Apache is sending Keep-Alive:timeout=5, max=100 which I dont want as I want client to be connected forever till anyone closes connection, due to this Browser automatically closes connection and I start getting net::ERR_INCOMPLETE_CHUNKED_ENCODING. How Can I modify this value only for Node Proxy Requests.I have added ProxyPass /events http://localhost:5000/events
ProxyPassReverse /events http://localhost:5000/eventsResponse Headers
Access-Control-Allow-Headers:key,origin, x-requested-with, content- type,Accept,Content-Type
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Type:text/event-stream; charset=utf-8
custom:header
Date:Wed, 13 Jan 2016 18:12:48 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
Transfer-Encoding:chunked
X-Powered-By:Express
Note:- All this is happening when I am using Apache to proxy to Node, else if I directly hit Node (which I cannot in prod due to blocked port) everything works fine.
The KeepAlive settings by default allowed only in server or virtual host configuration.
But during the request processing, apache2 use environment variables (based on apache configuration) to determine the current settings.
Fortunately with mod_rewrite, you can alter apache environment variables on request basis, so you can disable keepalive on specific request.
For example:
#Load rewrite module
LoadModule rewrite_module modules/mod_rewrite.so
#Enable mod_rewrite functionality
RewriteEngine On
#rewrite rule inside locationmatch
<LocationMatch ".*\/sse\/.*">
#This is not required, used only for debug purposes
Header set X-Intelligence "KEEPALIVEOFF"
#Here goes the mod rewrite environment variable trick
RewriteRule .* - [E=nokeepalive:1]
</LocationMatch>

I have enabled keep-alive but it isn't working

I have enabled the keep-alive option in my WHM
but I still get "close" when I have checked by many tools like this
http://www.giftofspeed.com/check-keep-alive/
http://gtmetrix.com/
And I have added the code in .htaccess file but the option is still not working
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
And when I create php file to print "HTTP_CONNECTION"
I get "close"
Thanks a lot
You have to reconfigure apache to have it handle keepalive.
The most relevant settings are:
KeepAlive On
Keepalivetimeout 5
MaxKeepaliveRequests 100
See the documentation here:
http://httpd.apache.org/docs/current/en/mod/core.html#keepalive
And a discussion which points to some caveats here:
https://abdussamad.com/archives/169-Apache-optimization:-KeepAlive-On-or-Off.html

Security issue with .htaccess

Firstly I tried adding multiple ifmodule but it does not work.
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: http://domainurl1.com
</ifModule>
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: http://domainurl2.com
</ifModule>
When try to add multiple ifmodule only last one(http://domainurl2.com) works others not.
then I try following code it works but i think it is not secure to allow everyone
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: “*”
</ifModule>
I have 5 domain that i have to allow.
Are there any solutions for adding multiple domains that i want to allow?
Try this if you want a quick fix
<ifModule mod_headers.c>
Header add Access-Control-Allow-Origin "http://domainurl1.com"
Header add Access-Control-Allow-Origin "http://domainurl2.com"
</ifModule>
However, this is not the recommended solution by W3C, instead you should make the server read the Origin header from the client, then compare it to a list of allowed domains and finally send the value of the Origin header back to the client as the Access-Control-Allow-Origin header. Check http://www.w3.org/TR/cors/#access-control-allow-origin-response-hea for more details.

Header set Access-Control-Allow-Origin in .htaccess doesn't work

I can't figure out why my .htaccess header settings doesn't work.
My .htaccess file content:
Header set Access-Control-Allow-Origin *
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Allow-Headers "*"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
But when I remove Header's and add them in index.php then everything works fine.
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: *");
What am i missing?
This should work:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Just for the record, I was running into the exact same problem and none of the answers worked.
I used a headers checker tool: http://www.webconfs.com/http-header-check.php
I was testing with my IP (http://192.0.2.1/upload) and what came back was the following:
HTTP/1.1 301 Moved Permanently =>
Date => Sat, 10 Jan 2015 04:03:35 GMT
Server => Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Location => http://192.0.2.1/upload/
Content-Length => 380
Connection => close
Content-Type => text/html; charset=iso-8859-1
There was a redirection happening and the AJAX request does not honor/follow redirects.
It turned out to be the missing slash at the end of the domain (http://192.0.2.1/upload/)
I tested again with slash at the end and I got this below. Added a slash in the script too, and it was now working.
HTTP/1.1 200 OK =>
Date => Sat, 10 Jan 2015 04:03:53 GMT
Server => Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By => PHP/5.3.8
Access-Control-Allow-Origin => *
Access-Control-Allow-Methods => PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers => *
Content-Length => 1435
Connection => close
Content-Type => text/html
Use this tool to test if your headers are good and to troubleshoot what is happening.
I have a shared hosting on GoDaddy. I needed an answer to this question, too, and after searching around I found that it is possible.
I wrote an .htaccess file, put it in the same folder as my action page. Here are the contents of the .htaccess file:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Here is my ajax call:
$.ajax({
url: 'http://www.mydomain.com/myactionpagefolder/gbactionpage.php', //server script to process data
type: 'POST',
xhr: function() { // custom xhr
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: beforeSendHandler,
success: completeHandler,
error: errorHandler,
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
See this article for reference:
Header set Access-Control-Allow-Origin in .htaccess doesn't work
Be careful on:
Header add Access-Control-Allow-Origin "*"
This is not judicious at all to grant access to everybody. It's preferable to allow a list of know trusted host only...
Header add Access-Control-Allow-Origin "http://aaa.example"
Header add Access-Control-Allow-Origin "http://bbb.example"
Header add Access-Control-Allow-Origin "http://ccc.example"
Regards,
I activated the Apache module headers a2enmod headers, and the issue has been solved.
Try this in the .htaccess of the external root folder
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Be careful with doing Header add Access-Control-Allow-Origin "*" This is not judicious at all to grant access to everybody. I think you should user:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://example.com"
</IfModule>
I +1'd Miro's answer for the link to the header-checker site http://www.webconfs.com/http-header-check.php. It pops up an obnoxious ad every time you use it, but it is, nevertheless, very useful for verifying the presence of the Access-Control-Allow-Origin header.
I'm reading a .json file from the javascript on my web page. I found that adding the following to my .htaccess file fixed the problem when viewing my web page in IE 11 (version 11.447.14393.0):
<FilesMatch "\.(json)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
I also added the following to /etc/httpd.conf (Apache's configuration file):
AllowOverride All
The header-checker site verified that the Access-Control-Allow-Origin header is now being sent (thanks, Miro!).
However, Firefox 50.0.2, Opera 41.0.2353.69, and Edge 38.14393.0.0 all fetch the file anyhow, even without the Access-Control-Allow-Origin header. (Note: they might be checking IP addresses, since the two domains I was using are both hosted on the same server, at the same IPv4 address.)
However, Chrome 54.0.2840.99 m (64-bit) ignores the Access-Control-Allow-Origin header and fails anyhow, erroneously reporting:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin '{mydomain}' is therefore not allowed access.
I think this has got to be some sort of "first." IE is working correctly; Chrome, Firefox, Opera and Edge are all buggy; and Chrome is the worst. Isn't that the exact opposite of the usual case?
After spending half a day with nothing working.
Using a header check service though everything was working.
The firewall at work was stripping them
try this:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Credentials true
Header set Access-Control-Allow-Origin "your domain"
Header set Access-Control-Allow-Headers "X-Requested-With"
</IfModule>
It's preferable to allow a list of know trusted host.
If anyone else is trying this, the most upvoted answer should work. However, if you are having issues it could be possible the browser has cached the REQUEST. To confirm append a query string.
To complete the most upvoted answer, I want to add the case whenever the options to the header is not add in a response request, you could add the always keyword from Apache.
In my case i needed to add the access control allow origin in the response of the redirection and not in the result of the redirection.
And a redirection is giving the 302 code status so the header wasn't filled with the correct information.
In this case I needed to add it :
Header always set Access-Control-Allow-Origin "*"
For more information you can check this thread :
Apache: difference between "Header always set" and "Header set"?

Resources