apache 如何将不同的子域请求重定向到不同的端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:19:47  来源:igfitidea点击:

How to redirect different sub domain requests to different port

apache

提问by Graviton

I have two applications, one is the www.myexample.com, another is the blog.myexample.com. I am using PHP and Apache.

我有两个应用程序,一个是www.myexample.com,另一个是blog.myexample.com. 我正在使用 PHP 和 Apache。

Now, I want to let www.myexample.comruns on port 82 of my machine, and blog.myexample.comon port 83, on the same machine. How to configure the apache and/ or the PHP scripts so that when the requests for the requests are served properly?

现在,我想让www.myexample.com运行在我机器的端口 82 和blog.myexample.com端口 83 上,在同一台机器上。如何配置 apache 和/或 PHP 脚本,以便何时正确处理请求?

Edit: Thanks for everyone who responds, but I afraid I don't get the question clear-- my bad!

编辑:感谢所有回答的人,但恐怕我没有把问题说清楚——我的错!

What I really want is to simulate a condition whereby the www.myexample.com and blog.myexample.com are located on different machines. So when a request comes in, the gateway server ( the one which is also hosting www.myexample.com) will check whether this is a request for www.myexample.com or for blog.myexample.com and does the necessary reroutes.

我真正想要的是模拟 www.myexample.com 和 blog.myexample.com 位于不同机器上的情况。因此,当请求进来时,网关服务器(也托管 www.myexample.com 的服务器)将检查这是对 www.myexample.com 还是 blog.myexample.com 的请求,并进行必要的重新路由。

How to do this? Thanks.

这该怎么做?谢谢。

回答by vladr

I will assume that you have your own reason for wanting the two sites (wwwand blog) to run on different ports - and in different processes. If this is not what you intended, e.g. you did not want to have two distinct processes, then having different ports may not be what you intended either: use VirtualHostinstead, to co-host the two domains within the same apache+php instance on port 80. Otherwise, read on.

我假设您有自己的理由希望这两个站点 (wwwblog) 在不同的端口上运行 - 并且在不同的进程中。如果这不是您想要的,例如您不想拥有两个不同的进程,那么拥有不同的端口可能也不是您想要的:VirtualHost改为使用,在端口上的同一个 apache+php 实例中共同托管两个域80. 否则,请继续阅读。

Assuming that you have your two apache+php processes listening on localhost:82 and localhost:83 respectively, bring up a third, apache-only process to act as a reverse proxy. Have the reverse proxy apache instance listen for requests coming on port 80 from the internet, with two virtual host definitions. The first virtual host definition, www, would forward requests to localhost:82, whereas the second virtual host definition, blog, would forward requests to locahost:83, e.g.:

假设您的两个 apache+php 进程分别在 localhost:82 和 localhost:83 上侦听,则启动第三个仅 apache 进程充当反向代理。使用两个虚拟主机定义,让反向代理 apache 实例侦听来自 Internet 端口 80 上的请求。第一个虚拟主机定义,www,将请求转发到 localhost:82,而第二个虚拟主机定义,blog,将请求转发到 locahost:83,例如:

NameVirtualHost *:80

# www
<VirtualHost *:80>
  ServerName www.myexample.com
  ProxyPass               /       http://localhost:82/
  ProxyPassReverse        /       http://localhost:82/
</VirtualHost>

# blog
<VirtualHost *:80>
  ServerName blog.myexample.com
  ProxyPass               /       http://localhost:83/
  ProxyPassReverse        /       http://localhost:83/
</VirtualHost>

回答by Glavi?

I use proxy for this type of things.

我对这类事情使用代理。

In my example, I have apache 1.3 running on port 80, but I needed svn repository to run on apache 2.2, and I didn't want to type :82 on the end of the domain every time. So I made proxy redirection on apache 1.3 (port 80):

在我的示例中,我在端口 80 上运行 apache 1.3,但我需要 svn 存储库才能在 apache 2.2 上运行,而且我不想每次都在域的末尾键入 :82。所以我在 apache 1.3(端口 80)上进行了代理重定向:

<VirtualHost *:80>
  ServerName svn.mydomain.com
  ServerAlias svn
  ServerAdmin [email protected]

  <IfModule mod_proxy.c>
    ProxyPass / http://svn:82/
  </IfModule>
</VirtualHost>

回答by M.A.K. Ripon

Run the following line on terminal (specify your domain and sub domain name correctly)

在终端上运行以下行(正确指定您的域和子域名)

sudo nano /etc/apache2/sites-available/subdomain.domain.com.conf 

Paste the following code and change as your requirement

粘贴以下代码并根据您的要求进行更改

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName subdomain.domain.com
        ServerAlias subdomain.domain.com
        ProxyRequests Off

        #ProxyPass / http://localhost:8080/
        <Location />
                ProxyPreserveHost On
                ProxyPass http://domain.com:8080/
                ProxyPassReverse http://domain.com:8080/
        </Location>
     # Uncomment the line below if your site uses SSL.
     #SSLProxyEngine On
</VirtualHost>

Run the following lines on terminal (specify your domain and sub domain name correctly)

在终端上运行以下几行(正确指定您的域和子域名)

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod subdomain.domain.com.conf
sudo service apache2 restart

回答by Lionel Morrison

A more complete answer to this would be to do something like this which allow you to setup a proxy gateway which is what is loosly described above.

对此的一个更完整的答案是做这样的事情,它允许您设置一个代理网关,这是上面松散描述的。

ServerName localhost

服务器名称本地主机

<Proxy *>
    Order deny,allow
    Allow from localhost
</Proxy>

ProxyRequests           Off
ProxyPreserveHost       On      

ProxyPass               /       http://localhost:10081/
ProxyPassReverse        /       http://localhost:10081/
ProxyPassReverseCookiePath /    http://localhost:10081/

回答by Rytmis

Off the top of my hat:

在我的帽子上:

Listen 82
Listen 83
NameVirtualHost 1.2.3.4 # Use your server's IP here

<VirtualHost www.myexample.com:82>
# Configure www.myexample.com here
</VirtualHost>

<VirtualHost blog.myexample.com:83>
# Configure blog.myexample.com here
</VirtualHost>