yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Problems with Response redirect function in proxy environment #19760

Open philosim opened 1 year ago

philosim commented 1 year ago

What steps will reproduce the problem?

In Controller use redirect() function to redirect to relative location:

public function actionView2($id)
{
    $model = $this->findModel($id);
    return $this->redirect(["view", "id" => $id]);
}

In View use Gridview with Pjax enabled and actioncolumn view button pointing to ["view2","id"=>$model->id].

What is the expected result?

Response should sent Header Location /controller/view?id=$id and Status Code 302

What do you get instead?

Response sends Location https://myserver.intern:8002/view?id=$id

// UPDATE

It only happens in when using PJAX (in a gridview), Response does not send header-location, the Response is 302 with:

X-Pjax-Url https://myserver.intern:8002/view?id=$id

Additional info

To be fair, it is in the docs that redirect will act like this:

 * Any relative URL that starts with a single forward slash "/" will be converted
 * into an absolute one by prepending it with the host info of the current request.

Question is: why? Header-Location definitely accepts relative URLs and there is no reason to add the internal getHostInfo() to every redirect. In k8s environments (or any other situation using a proxy redirect, loadbalancer, ...) one will have to use own redirect functions, since the internal hostinfo URL is not always available from outside clients.

Q A
Yii version 2.0.43
PHP version 7.X, 8.X
Operating system SLES
DeryabinSergey commented 1 year ago

Maybe I`m wrong. But why your application layer knows about k8s, LB and etc? Why you don`t want to configure the right PHP environment?

philosim commented 1 year ago

My application layer knows nothing about k8s, it's just a simple webapp running on nginx. what do you mean by "configure PHP environment"? php also knows nothing about internal hostnames ...

DeryabinSergey commented 1 year ago

what do you mean by "configure PHP environment"

Set correct nginx headers for redirects and set server host info https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L754 look at comment to Request::getHostInfo

philosim commented 1 year ago

Request Headers are set, working as expected under normal circumstances. It is a very complex environment, I am still trying to figure out why they won't in some cases.

But nonetheless, the question is: why would someone expect a redirect function to always add the hostinfo? Its clearly not necessary and I can't really image any case one would need this - if a want to redirect to am absolute URI I would do so. Could live with a scheme parameter as in Url::to() which defaults to false though ...