当前位置: 动力学知识库 > 问答 > 编程问答 >

.htaccess - Apache redirect to a port and mask the URL

问题描述:

I have one web server running two sites on different ports.

IE: server:8081 and server:8083

I setup two DNS records and pointed it to “my server”

Dev.server.com and Pre.server.com

I would like Dev.server.com to redirect to server:8083 but mask the URL to always stay Dev.server.com and Pre.server.com to redirect to server:8081 but mask the URL to always stay pre.server.com

If I set them up like this

<VirtualHost *:80>

ServerName http:// Dev.server.com

ProxyRequests off

ProxyPass / http://server:8083

ProxyPassReverse / http://server:8083

</VirtualHost>

<VirtualHost *:80>

ServerName http:// Pre.server.com

ProxyRequests off

ProxyPass / http://server:8081

ProxyPassReverse / http://server:8081

</VirtualHost>

Everything routes to the Dev instance and nothing makes it to the Pre instance

I have it set like this;

<VirtualHost *:80>

ServerName http:// Dev.server.com

RewriteEngine On

RewriteCond %{HTTP_HOST} ^dev\.server\.com$ [NC]

RewriteRule ^(.*)$ http:// Dev.server.com:8083$1 [R]

RewriteCond %{HTTP_HOST} ^pre\.server\.com$ [NC]

RewriteRule ^(.*)$ http://pre. server.com:8081$1 [R]

</VirtualHost>

Listen 0.0.0.0:8083

Listen 0.0.0.0:8081

<VirtualHost *:8083>

ServerName dev. server.com

ProxyRequests off

ProxyPass / http:// server.com:8083/jde/owhtml/

ProxyPassReverse / http:// server.com:8083/jde/owhtml/

Oc4jMount /jde HTML_DV_8083

Oc4jMount /jde/* HTML_DV_8083

</VirtualHost>

<VirtualHost *:8081>

ServerName pre.server.com

ProxyRequests off

ProxyPass / http:// server.com:8081/jde/owhtml/

ProxyPassReverse / http:// server.com:8081/jde/owhtml/

Oc4jMount /jde HTML_PY_8081

Oc4jMount /jde/* HTML_PY_8081

</VirtualHost>

This works perfectly for the routing but does not mask the URL. It adds the port to the URL witch we do not want to happen.

Anyone have any ideas as to what I am doing wrong?

网友答案:

You want your reverse proxy to happen in your port 80 vhost. Because you're using mod_rewrite to redirect the browser to URLs like http://Dev.server.com:8083/, that's what the browser will see. You just need 2 vhosts on port 80:

<VirtualHost *:80>
  ServerName dev.server.com

  ProxyRequests off
  ProxyPass / http://server.com:8083/jde/owhtml/
  ProxyPassReverse / http://server.com:8083/jde/owhtml/
  Oc4jMount /jde HTML_DV_8083
  Oc4jMount /jde/* HTML_DV_8083
</VirtualHost>

<VirtualHost *:80>
  ServerName pre.server.com

  ProxyRequests off
  ProxyPass / http://server.com:8081/jde/owhtml/
  ProxyPassReverse / http://server.com:8081/jde/owhtml/
  Oc4jMount /jde HTML_PY_8081
  Oc4jMount /jde/* HTML_PY_8081
</VirtualHost>

Note that the "ServerName" is dev.server.com and pre.server.com, and not http:// Dev.server.com with a space following the scheme and ://. Because http:// Dev.server.com isn't going to be the hostname you're going to visit, apache defaults everything to the first vhost. This is probably why your second attempt works, because both dev and pre default to the first vhost since nothing matches on port 80.

分享给朋友:
您可能感兴趣的文章:
随机阅读: