问题
I have a bunch of subdomains in one single server:
- a.example.com
- b.example.com
- news.example.com
All of them are in the same Apache virtualhost.
I need to use a feed provided by the news subdomain inside the a and b subdomain. The feeds typically look like this:
- news.example.com/news/a
- news.example.com/news/b
On the a and b subdomains, I'm using jquery's ajax function to load the data from the news feeds, and present it on a and b. This initially didn't work, because of the same-origin policy.
I was able to override this by adding the Access-Control-Allow-Origin "*"
to my Apache config file.
... but this works only in Firefox, Chrome and Safari. Internet explorer seems to ignore that directive.
Thus, I need to create a proxy.
What I need is a new directory in all my subcomains (for example /proxy
) that Apache detects, and redirects to news.example.com, no matter what the subdomain. So:
- a.example.com/proxy/news/a -> return the contents of news.example.com/news/a
- b.example.com/proxy/news/b -> return the contents of news.example.com/news/b
Can I do this directly in Apache + submodules (for example, mod_rewrite), or do I need to use a scripting language like PHP for doing this?
回答1:
You want the ProxyPass Directive.
ProxyPass /proxy/news/a http://news.example.com/news/a
回答2:
At the end we were able to make the proxy using a combination of two modules: mod_rewrite
and mod_proxy
.
The syntax was the following:
rewriteEngine on
rewriteRule proxy/(.+)$ http://news.example.com/$1 [P]
The [P] at the end is telling the rule "act as a proxy" and doesn't work without mod_proxy. Without it, apache makes a "redirect" (the url at the top of the page changes) instead of "just serving the page".
回答3:
Apache can be configured to use apache:
consider this working sample code (proxy part):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName ci.testserver.com
ServerAlias ci
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
来源:https://stackoverflow.com/questions/9412843/apache-can-i-make-a-proxy-server-with-just-apache