Skip to content

Instantly share code, notes, and snippets.

@matewilk
Last active August 29, 2015 14:23
Show Gist options
  • Save matewilk/97caac5cb2ece0592cfc to your computer and use it in GitHub Desktop.
Save matewilk/97caac5cb2ece0592cfc to your computer and use it in GitHub Desktop.
Apache Reverse Proxy Example - alternative to CORS requests
<VirtualHost *:80>
#
# some apache2 config here - not necessary to display in this gist
#
# Simple reverse proxy config to serve content from another domain
ProxyRequests Off
ProxyPass /subdomain/ http://subdomain.localhost/
ProxyPassReverse /subdomain/ http://subdomain.localhost/
</VirtualHost>
# Subdomain config mocking another server where all requests from *:80 goes
<VirtualHost *:80>
ServerName subdomain.localhost
DocumentRoot /var/www/html/subdomain
</VirtualHost>
<!--
Proxy example file
When the button is clicked, a request is sent to http://localhost/
Redirection is made by apache config (in previous file) and request is redirected to http://subdomain.localhost/
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Proxy example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#request").click(function(e){
e.preventDefault();
$.ajax({type: "POST",
//url is set to /subdomain/* which is than redirected by apache to http://subdomain.localhost/
url: "/subdomain/server.php",
success:function(result){
$("#result").html(result);
},
error: function(xhr, status, string){
console.log(status);
}
});
});
});
</script>
</head>
<body>
<h4>Proxy example</h4>
<button id="request">Send</button>
<div id="result"></div>
</body>
</html>
<?php
//this file lies on http://subdomain.localhost/ and responds to /localhost/subdomain/ request thanks to ProxyPass apache config
//server responds with 'OK' which is displayed on html page (in div with id=result)
echo "OK";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment