Hello,
i dont know if it is a bug:
I got an 502 Bad Gateway-Error after an Ajax-Request.
In the target controller/method i have setted 3 flashdata-session-items:
$this->session->set_flashdata("success","true");
$this->session->set_flashdata("success_to_user_id","1");
$this->session->set_flashdata("success_something_else","xyz");
The request successfully finished. But then, when i refresh the site
(window.location.reload or F5) i got an '502 Bad Gateway' Error.
After playing around I have fixed that error:
I reduced the 3 set_flashdata() calls to 1. Then it works, no more 502.
So:
Less or equal 2x $this->session->set_flashdata(); = OK, NO ERROR
Greater or equal 3x $this->session->set_flashdata(); = ERROR 502 BAD GATEWAY
Is this a bug ?
Greetings,
Julian Fuerderer
You are correct, i've same issue...
Things like this require a lot more than just "this didn't work".
I ran into this issue while using HaProxy load balancing between apache servers running a web app running CI 2.
This issue is caused by how codeigniter session class cleans up the flash cookie information.
In the CI_Session class function _flashdata_mark()
function _flashdata_mark()
{
$userdata = $this->all_userdata();
foreach ($userdata as $name => $value)
{
$parts = explode(':new:', $name);
if (is_array($parts) && count($parts) === 2)
{
$new_name = $this->flashdata_key.':old:'.$parts[1];
$this->set_userdata($new_name, $value);
$this->unset_userdata($name);
}
}
}
The problem is that for each flash key you have that needs to be destroyed set_userdata(...) and unset_userdata(...) are called, each of which behind the scenes results in a $this->sess_write() being called which will call $this->_set_cookie(...).
So for each piece of flashdata you end up with 2 set cookie calls. That's rather ugly.
Anyway, my fix was to have set_userdata(...) and unset_userdata(...) are called only a single time.
function _flashdata_mark()
{
$userdata = $this->all_userdata();
$newUserData = array();
$userDataToUnset = array();
foreach ($userdata as $name => $value)
{
$parts = explode(':new:', $name);
if (is_array($parts) && count($parts) === 2)
{
$new_name = $this->flashdata_key.':old:'.$parts[1];
$newUserData[$new_name] = $value;
$userDataToUnset[$name] = '';
//if we call these for each flash key we end up with a TON of set-cookie headers
//this is dumb, and causes problems with loadbalancers etc.
//$this->set_userdata($new_name, $value);
//$this->unset_userdata($name);
}
}
if ( count($newUserData) > 0 ) {
$this->set_userdata($newUserData);
$this->unset_userdata($userDataToUnset);
}
}
Note: The above code is regarding CI 2, having briefly looked at the newest CI code in this git repo, I think this issue still exists.
Just ran into this issue in the wild.
The above workaround (bundling all the set calls into a single call) worked as a temporary patch for me.
Is there any 'global' solution ? (except the method above)
@julianfuerderer when you say 'global' what do you mean?
fix it now in the rep
Aah ok, I'll put together a pull request. I was meaning to, but just haven't had time.
Is this issue still open?
@narfbg
Does it look like it's closed?
Sorry @narfbg, I thought it was closed in #2850
Thanks for your response. I'm using codeigniter(2.3.1)/nginx/apache, when
i post a simple login form with user and password, after login validation
with database. I've used the DB session the issue has fixed.
After successful login, we are got error message("502 - Bad Gateway") while
storing the login credentials into session variables.
//query the database
$result = $this->user->login($username, $password); # return single array
result.
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
$this->session->set_flashdata('msg_success', 'Logined in successfully');
}
redirect('myaccount');
}
else
{
$this->session->set_flashdata('msg_error', 'Invalid username or
password');
redirect('login');
}
If login failed message show's correctly, otherwise server responded as 502
bad gateway error.
On Sat, Aug 3, 2013 at 8:31 AM, Eric Roberts [email protected]:
- Have you tested it using CI 3.0 to see if it's been fixed yet?
- Are you using DB, cookie, or native sessions?
- Can you provide the actual code you're using?
Things like this require a lot more than just "this didn't work".
Reply to this email directly or view it on GitHubhttps://github.com/EllisLab/CodeIgniter/issues/2476#issuecomment-22047339
.
Best Regards,
Pilathraj A
+91-8197825376
+91-9578083325
[email protected]
[email protected]
Make sure to check your server error logs (Apache or nginx) to see what the actual problem is behind the 502 error.
I have encounted the same issue in the past on a site I manage and resolved it recently by increasing the proxy buffer sizes in my nginx.conf file. For instance:
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
I believe in an Apache setup mod_proxy variables would need to be increased but don't quote me on that.
Regardless, this isn't a Codeigniter issue but instead a server configuration issue which causes the 502 Gateway error to occur, if you check your Apache or Nginx logs as mentioned above, you'll soon be able to resolve the issue.
Superceded by #3073? Confused.
It will be closed when #3073 is closed.
Fixed by #3073.
@AdenFraser thank you.
in Nginx, you must set conf with:
在 Nginx å¿…é ˆåœ¨ conf 宣告以下變數
location ~ \.php$ {
....
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
Most helpful comment
I have encounted the same issue in the past on a site I manage and resolved it recently by increasing the proxy buffer sizes in my nginx.conf file. For instance:
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
I believe in an Apache setup mod_proxy variables would need to be increased but don't quote me on that.
Regardless, this isn't a Codeigniter issue but instead a server configuration issue which causes the 502 Gateway error to occur, if you check your Apache or Nginx logs as mentioned above, you'll soon be able to resolve the issue.