1

I struggling with a reverse proxy configuration and specific rewriting/proxypass depending on URL-path.

Szenario:

  • Reverse Proxy in DMZ
  • SSL ends on Reverse Proxy
  • Requests should be passed to internal server via http
  • HTTP needs to be rewritten to HTTPS
  • In html hrefs are build like "/system/js/abc.js"

My current config:

<VirtualHost *:80>
    ServerName media.customer.com
    RewriteEngine On
    RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
    RewriteRule ^(.*)$ https://media.customer.com$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName media.customer.com
    DocumentRoot /var/www/customer.com/

    SSLEngine On
    SSLCertificateKeyFile /etc/apache2/certs/server.key
    SSLCertificateFile /etc/apache2/certs/server.crt

    ProxyRequests Off
    ProxyPass / http://serverName/
    ProxyPassReverse / http://serverName/

    ProxyHTMLEnable On
    ProxyHTMLURLMap http://serverName/ /
</VirtualHost>

That would not be a huge problem, but we need to get following rules working:

  1. Only base-url "Http(s)://serverName" is routed to http://serverName/system/ (add /system/) to base-URL
  2. Every deeper URL only protocol changes from http to https. No change to URL.
  3. Absolute http-URLs need to changes to https-URLs

The above config works for:

  • [http]/serverName/system/
  • [http]/serverName/system
  • [https]/serverName/system/
  • [https]/serverName/system
  • [http]/serverName/system/img/test.gif
  • [https]/serverName/system/img/test.gif

Do I put "/fotoweb/" in RewriteRule or ProxyPass, we get doubled strings like: http://serverName/system/system/img/test.gif

For my opinion, I need to identify if its a base-url and then add "/system/", otherwise just map to http. But no clue how to do this.

Thanks in advance!

0

This rewrites all that is not HTTPS to HTTPS:

RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Not sure what you consider 'base-url', but I'm assuming it's HTTPS + not starting with '/system'. This rewrites all that doesn't start with '/system' to '/system/...':

RewriteCond %{REQUEST_URI} ! ^/system
RewriteRule .* /system%{REQUEST_URI} [R=301,L]

If it's just '/', then:

RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /system/ [R=301,L]

All in one:

<VirtualHost *:80>
    ServerName media.customer.com
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName media.customer.com

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ! ^/system
    RewriteRule .* /system%{REQUEST_URI} [R=301,L]

    DocumentRoot /var/www/customer.com/

    SSLEngine On
    SSLCertificateKeyFile /etc/apache2/certs/server.key
    SSLCertificateFile /etc/apache2/certs/server.crt

    ProxyRequests Off
    ProxyPass / http://serverName/
    ProxyPassReverse / http://serverName/

    ProxyHTMLEnable On
    ProxyHTMLURLMap http://serverName/ /
</VirtualHost>
| improve this answer | |
  • Thanks fuero! I Updated the Description regarding base url. So the logic we want needs to be done by rewrite rule, not proxypass. There we cannot define regex for Url matching – Oliver Feb 11 '15 at 19:19
  • Look close: RewriteCond %{REQUEST_URI} ^/$ - doesn't that do what you ask? – fuero Feb 11 '15 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.