How do I use URL redirection with reverse proxy?

8,639

Under Apache, using the ProxyPassReverse directive will do what you want. It will take the redirect from the backend server and replace the backend host name with the front end name. From the documentation:

This directive lets Apache httpd adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses. This is essential when Apache httpd is used as a reverse proxy (or gateway) to avoid bypassing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

They also have an example of how it is typically used in conjunction with adjusting cookie paths:

ProxyPass         "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverse  "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverseCookieDomain  "backend.example.com"  "public.example.com"
ProxyPassReverseCookiePath  "/"  "/mirror/foo/"

You may find that there are also absolute links in the HTML documents from the backend server. If that is the case, you can use the mod_proxy_html module to also replace those.


It sounds like that in this case you didn't develop the backend webapp yourself and it may not have settings for running behind a proxy server. However, if you can modify the code on the backend webapp, you can modify it to write its links and redirects using the front end host name.

The front end proxy server passes the host name to the backend using the X-Forwarded-For header. You can change your server to use the value from this header rather than ServerName so that it works seamlessly behind a proxy.

Share:
8,639

Related videos on Youtube

Kyle Olson
Author by

Kyle Olson

Over Ten years of solid experience as a Linux Administrator and into various streams like Hadoop, Bigdata Analytics with Tools implementation like Cloudera DataScience Workbench, Neo4j graph database, Elastic search, and Lavastorm implementation. Path which I come across have manipulated different kind of scenarios with respect to the realms mentioned above. Automation knowledge using configuration management tools like Ansible. Knowledge on cloud platforms like Amazon EC2, have had worked extensively on Cloud platform for server provisioning, implementing security groups all that. Dealt with various Linux & Unix flavored servers. Good scripting skills to automate things. Have dealt with Hadoop cluster issues using Cloudera Manager, Spark issues with respect to YARN queues, and Upgrade of Hadoop clusters using Cloudera Manager. Kerebros configuration with the cluster, and Hadoop user creation, maintenance. Knowledge on web servers like, Jetty, Apache, Tomcat. Performance tuning with PostgreSQL database. Hot & Warm standby with PG-Pool mechanism for PostgreSQL database. Dealt with PostgreSQL backups using scripts. Housekeeping servers using scripts. Had worked on VMWare VC on node creation, configuration change and did troubleshoot the Virtual Machine issues. Shell scripting for automated DB/application/repository(SVN)backups and alert mechanisms. Good work knowledge on Amazon cloud servers & Load balancing between servers (SaaS environment), HA cluster setup between servers.

Updated on September 18, 2022

Comments

  • Kyle Olson
    Kyle Olson over 1 year

    My requirement is to redirect the URL, which is running on port 80 with reverse proxy. I guess I would have to use URL redirection, reverse proxy together.

    There is already a web-domain active like : https://abcd.cdsw.intranet.example.com However, we would want to use reverse proxy to hid the diversions from the URL, but this URL runs with port 80 I guess.

    As a result, I couldn't able to start httpd server in Redhat enterprise Linux 7 to introduce reverse proxy. Can someone help me with this?

    Expected:

    Actual URL : https://abcd.cdsw.intranet.example.com/login

    Expected URL : http or https://example.com/cdsw

    • Stephen Ostermiller
      Stephen Ostermiller over 6 years
      Do you have control over the web-app? Can you change the code to make it redirect somewhere else, or are you going to need to configure the proxy to intercept and change the redirects?
    • Kyle Olson
      Kyle Olson over 6 years
      But, I am not sure whether Cloudera Datascience Workbench uses any sort of web-app like that.
    • Stephen Ostermiller
      Stephen Ostermiller over 6 years
      What are you using for your reverse proxy? Apache? Nginx? Something else? We can't help you configure it unless we know what it is.
  • Kyle Olson
    Kyle Olson over 6 years
    So, if I go by your suggestion, my configuration would be like, ProxyPass "/login" "https://abcd.cdsw.intranet.example.com" ProxyPassReverse "/login" "https://abcd.cdsw.intranet.example.com" ProxyPassReverseCookieDomain "abcd.cdsw.intranet.example.com" "example.com/cdsw" ProxyPassReverseCookiePath "/" "/login" We can assume the above configuration works, but port 443, and 80 is already being used by CDSW application. How can I use this redirection without stopping the application? @Stephen Ostermiller
  • Kyle Olson
    Kyle Olson over 6 years
    ..cont even if I stop it, httpd can be started, but the application may barf saying the port 443, 80 is already being used by httpd isn't it?
  • Stephen Ostermiller
    Stephen Ostermiller over 6 years
    To use reverse proxy you either need two different servers, or use different ports. One solution is to run the CDSW application on a different machine. Another solution is to run the CDSW application on ports 8080 and 8443. That allows you to reverse proxy them from a virtual host on the main ports along with other virtual hosts.
  • Kyle Olson
    Kyle Olson over 6 years
    That's more likely even I was telling to my colleague to the same, but still trying if there are any alternatives. We are approaching the vendor to see if they can help. From the Webapp side they can help on that I believe. Thanks for the suggestions.
  • Kyle Olson
    Kyle Olson over 6 years
    I have tried something of this sort based on the suggestion provided on a different host where CDSW application is not installed cont... @Stephen
  • Kyle Olson
    Kyle Olson over 6 years
    Please find the code that I have tried under /etc/httpd/conf.d/cdsw.conf on the below link, Check here for the code Please assist me what am I missing there to make it work.
  • Kyle Olson
    Kyle Olson over 6 years
    I am still hitting the bush, not sure, what am I missing here, tried to set the loglevel to debug, but still didn't quite catch any error messages in the log. This is the configuration what I am with now configuration
  • Stephen Ostermiller
    Stephen Ostermiller over 6 years
    Your new proxy directives look OK to me from visual inspection.
  • Kyle Olson
    Kyle Olson over 6 years
    /var/log/httpd/error_log - file name [Tue Dec 19 12:08:03 2017] [error] [client <ip address>] Directory index forbidden by Options directive: /var/www/html/ [Tue Dec 19 12:08:03 2017] [error] [client <ip address>] File does not exist: /var/www/html/favicon.ico, referer: https://frafahadooappd5.de.cd.com/ do you see anything from this error?
  • Stephen Ostermiller
    Stephen Ostermiller over 6 years
    I don't think that has anything to do with it.
  • Kyle Olson
    Kyle Olson over 6 years
    To add to this issue, frafahadooappd5.de.cd.com is a standalone server, where I run apache with port 443 that's all. This dev.cdsw.abc.intranet.cd.com is mapped to some other IP address, and this alias is mapped to dev.cdsw.global.intranet.cd.com different IP address. Since it is clearly mentioned that the application host doesn't support httpd because it is already utilizing ports 80 and 443, I have planned to use it in a different server altogether. Hope this won't bring any issue in my case.
  • Kyle Olson
    Kyle Olson over 6 years
    Hope the above explanation give a little insight what I am trying to do and see if that's possible.
  • Kyle Olson
    Kyle Olson over 6 years
    I am able to do the reverse proxy with redirection, but the reverse proxy URL jumps from the reverse proxy URL to the origin (application URL) itself. Please have a look at this and let know if anything can be done about it. here
  • Stephen Ostermiller
    Stephen Ostermiller over 6 years
    To change the links in the page, you need to also enable mod_proxy_html as I already explained in my answer.
  • Kyle Olson
    Kyle Olson over 6 years
    But it has so many directives, not quite getting which one to use and how? Because examples are very few on the internet :(