shkoliar / magento-ngrok

Magento 2 module for ngrok.io service support
https://shkoliar.com
MIT License
44 stars 22 forks source link

infinite redirects #3

Open eraq opened 4 years ago

eraq commented 4 years ago

First I want to say thank you for building this and making it public. I very much appreciate it. I've been having some issues getting this to work correctly.

I'm running magento2 in wamp with a virtual host name 'http://magento.local' which works correctly loading magento at that url. I am not forcing secure url on frontend or admin. When I go to the ngrok 'http://xxx.ngrok.io' url, magento loads the homepage but with all css and js resources broken as they point to the local url of 'http://magento.local and are blocked by cross domain origin'. So of course now I install and enable this module.

with the module installed and enabled and cache cleared, setup upgrade and di compile run. index reindexed etc.

At the Ngrok url http://xxx.ngrok.io the I get an infinite 302 redirect loop back to that same ngrok url

hitting a static php file directly 'http://xxx.ngrok.io/ngroktest/test.php' it does return a successful 200

When I log __METHOD__ at the top of Shkoliar\Ngrok\Plugin\Model::afterGetBaseUrl, I see that the method is running, when I comment out the method, the homepage loads but with all resources blocked pointing to the 'magento.local' path.

I have disabled and cleared all caches, I have run cache:clean and cache:flush, setup:update and di:compile along with index:reindex and static-content:deploy -f. I have deploy mode in developer. I have tried this with rewrite on and off. I have tried clearing browser cache and cookies, and using different browsers I never used before. I am running ngrok like so ngrok http -host-header=magento.local 80

Is there something else I am missing?

eraq commented 4 years ago

here is the request raw:

GET /index.php/ HTTP/1.1
Host: magento.local
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Dnt: 1
Pragma: no-cache
Upgrade-Insecure-Requests: 1
X-Forwarded-For: {my i.p}
X-Original-Host: {my ngrok subdomain}.ngrok.io

and here is the response:

HTTP/1.1 302 Found
Date: Wed, 29 Apr 2020 19:30:04 GMT
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.2.25
X-Powered-By: PHP/7.2.25
Set-Cookie: PHPSESSID=9couub5cfut687tam0qv9f1kvl; expires=Wed, 29-Apr-2020 20:30:15 GMT; Max-Age=3600; path=/; domain=magento.local
Expires: Mon, 29 Apr 2019 19:30:15 GMT
Cache-Control: max-age=0, must-revalidate, no-cache, no-store
Pragma: no-cache
Location: http://magento.local/index.php/
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Content-Length: 0
Content-Type: text/html; charset=UTF-8
shkoliar commented 4 years ago

Hello @eraq,

What Magento version is used?

eraq commented 4 years ago

2.3.3

shkoliar commented 4 years ago

Tested with clean Magento 2.3.3 installation, works normally.

Often infinite redirects issue happens because of https redirect configuration.

May you try to debug redirects by creating a helper script? (taken from here)

redirects.sh

#!/bin/bash
echo
for domain in $@; do
echo --------------------
echo $domain
echo --------------------
curl -sILk $domain | egrep 'HTTP|Loc' | sed 's/Loc/ -> Loc/g'
echo
done

Example of an Infinite Redirect from HTTP to HTTPS

./redirects.sh http://www.example.com
--------------------
http://www.example.com
--------------------
HTTP/1.1 301 Moved Permanently
-> Location: https://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: https://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.example.com/
HTTP/1.1 301 Moved Permanently
....
-> Location: https://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: https://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.example.com/
HTTP/1.1 301 Moved Permanently
-> Location: https://www.example.com/

If you are on Windows you may need to use the WSL terminal, or use PowerShell bash command if available.

jamiethorpe commented 4 years ago

I was having the same issue and found that signing up for an ngrok account, adding an auth token (step 3 here https://ngrok.com/download) and running ngrok http -host-header=yoursite.com 443 fixed the issue.

I'm still having other issues, but that solved the redirect problem.

vestman commented 4 years ago

I had the same issue. Setting Stores / Config / General / Web / Url Options / Auto-redirect to Base URL to No fixed the issue for me.

alejandrogih commented 1 year ago

@jamiethorpe this really worked ( ngrok http -host-header=yoursite.com 443 )