[Solution] Nginx + Varnish + Apache force SSL .htaccess Redirect Loops

Started by bullten, August 15, 2018, 01:34:51 PM

Previous topic - Next topic

bullten

Whenever you are using [Nginx + Varnish + Apache] or [Nginx + Apache] you need to tell Apache that a SSL request is being served by a proxy server. So use the code below to force SSL .htaccess

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IFModule>

tutods

Hi.
Where i put this code?
With this code all domains redirect to https?

GTMAN


monkeyking

Quote from: bullten on August 15, 2018, 01:34:51 PM
Whenever you are using [Nginx + Varnish + Apache] or [Nginx + Apache] you need to tell Apache that a SSL request is being served by a proxy server. So use the code below to force SSL .htaccess

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IFModule>


This work great! Is possible to do this through vhost on the server side so we don't have to add this every time?

studio4host

VPS & Dedicated server provider with included FREE Managed support for CWP.
http://www.studio4host.com/

*** Don't allow that your server or website is down, choose hosting provider with included expert managed support for your CWP.

monkeyking

Thank for the idea. I've contacted them just now. Can you also do so too and anyone else? Hopefully they will see it's important. This cause redirect issues and app breaks because of the mixed content.

zirvesunucum

Hello,

I know this issue, and i cant fix not yet but you can use temporary fix :

If you using only Apache, you can use this code for HTTPS redirecting in .htaccess file :

# Before evereything, redirect HTTPS
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{HTTPS} !=on
   RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

   RewriteCond %{HTTP_HOST} !^www\.
   RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,E=HTTPS:1]
</IfModule>

If you using NGINX + APACHE with reverse proxy etc. you can use this code for HTTPS redirecting in .htaccess file :

# NGINX + APACHE Before evereything, redirect HTTPS
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{SERVER_PORT} 80
   RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

   # If you want www redirect..
   RewriteCond %{HTTP_HOST} !^www\.
   RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,E=HTTPS:1]
</IfModule>
---------------------------------------------------------------
T: +90 262 373 65 20
M: info@zirvesunucum.com
W: www.zirvesunucum.com
ZirveSunucum Information
---------------------------------------------------------------

Painkiller88

I don't know why you are using .htaccess for forcing https if you are using nginx.

Nginx is not working with .htaccess and so it is not taking all the code like if you are using apache alone.


To really force https instead of make a redirect, you better configure this in the webserver Main or Domain Config.

Select the user you wanna force, create a template and select "force-https" as nginx default vhost template.

This will do the real force of https instead of first use http and redirect a user to https and it is persistant for updates.


This is the way i use it and i never faced any problems with all my sites.

If you wanna force all users to only use HTTPS, configure this in the webservers main config.

365creativepixel

Quote from: dcarrero on April 23, 2021, 07:44:08 AM
I tried this solution:

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IFModule>

But I have the same problem in some moments. it's extrange.

REgards.
https://alcazardesanjuan.com

-----------

Aún le persiste el problema? me pasa lo mismo al utilizar nginx-varnish-apache, le siguió sucediendo o ya solucionó de otra manera?

Yo acabo de poner el codigo en el archivo htacess espero funcione.