How to redirect different sub domain requests to different port

后端 未结 5 440
你的背包
你的背包 2021-01-30 07:19

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

Now, I want to let w

相关标签:
5条回答
  • 2021-01-30 08:04

    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 admin@domain.com
            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
    
    0 讨论(0)
  • 2021-01-30 08:09

    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):

    <VirtualHost *:80>
      ServerName svn.mydomain.com
      ServerAlias svn
      ServerAdmin my@email.com
    
      <IfModule mod_proxy.c>
        ProxyPass / http://svn:82/
      </IfModule>
    </VirtualHost>
    
    0 讨论(0)
  • 2021-01-30 08:13

    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/
    

    0 讨论(0)
  • 2021-01-30 08:15

    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>
    
    0 讨论(0)
  • 2021-01-30 08:22

    I will assume that you have your own reason for wanting the two sites (www and 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 VirtualHost instead, to co-host the two domains within the same apache+php instance on port 80. Otherwise, read on.

    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.:

    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>
    
    0 讨论(0)
提交回复
热议问题