Saturday, June 25, 2022

[FIXED] How to fix urls of wordpress site behind reverse proxy server

Issue

I have a WordPress site on IIS 8, let's call it www.mysite.com, that I need to isolate behind a proxy server (with IIS 10).

What I have done so far is:

  • changed siteurl and home to site.mysite.com (to avoid continuos redirect)
  • added site www.mysite.com in new server with reverse proxy to site.mysite.com (defined in hosts file as old server ip alias) configured as follow
    <configuration>
        <system.webServer>
            <rewrite>
                <outboundRules rewriteBeforeCache="true">
                    <clear />
                    <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
                        <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://site.mysite.com/(.*)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
                        <action type="Rewrite" value="http{R:1}://www.mysite.com/{R:2}" />
                    </rule>
                    <rule name="RestoreAcceptEncoding" preCondition="NeedRestoringAcceptEncoding" enabled="true">
                        <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
                        <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
                    </rule>
                    <preConditions>
                        <preCondition name="ResponseIsHtml1">
                            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                        </preCondition>
                        <preCondition name="NeedRestoringAcceptEncoding">
                            <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
                        </preCondition>
                    </preConditions>
                </outboundRules>
                <rules>
                    <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                        <match url="(.*)" />
                        <action type="Rewrite" url="https://site.mysite.com/{R:1}" />
                        <serverVariables>
                            <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                            <set name="HTTP_ACCEPT_ENCODING" value="" />
                        </serverVariables>
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

Unfortunately this isn't enough.

Despite the reverse proxy outbound rule, many scripts, css links and images point to site.mysite.com (which is not exposed to the internet) instead of www.mysite.com.

Is there a way to fix those links making them refer to www?

Are there better solutions?


I have never worked with WordPress/PHP and have no knowledge how it builds those urls, so if you need any clarification please be as specific as possible thus I can improve my question.

UPDATE

If I configure

  • in isolated server
    • siteurl and home to www.mysite.com
    • set 127.0.0.1 www.mysite.com in hosts file
  • in exposed server
    • IIS 10 site binding www.mysite.com with forward rule to www.mysite.com
    • set WORDPRESS_SERVER_IP www.mysite.com in hosts file

I get following IIS error:

Errore HTTP 400.605 - Bad Request The request cannot be routed because it has reached the Max-Forwards limit. The server may be self-referencing itself in request routing topology.


Solution

You should set the siteurl and home variables to the value of the proxy server.

If you're exposing your server to the public at www.mysite.com then you should set the same values in those variables.



Answered By - Pranav G.
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.