多个应用程序,同一域下的应用程序的多个版本。如何配置apache服务器,路由和相对路径以使其工作? [英] Multiple apps, multiple versions of the app under the same domain. How to configure apache server, routes and relative paths to make it work?

查看:88
本文介绍了多个应用程序,同一域下的应用程序的多个版本。如何配置apache服务器,路由和相对路径以使其工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队的服务器设置如下:我们有一个域名,该域名似乎已经是公司域的子域。我们希望在该域下托管多个应用程序。我们甚至希望为服务器上的每个应用提供生产版本和登台版本。

My team's server is set up in such as way: We have one domain name, which seems like already a subdomain of the company's domain. We want to host multiple applications under this one domain. We'd even like to have a production version and staging version for each of the apps on the server.

文档根目录是一个空文件夹。应用程序位于文档根目录之外。
,我们尝试使用URL路径中的第一个令牌找出我们尝试访问的应用程序,然后以某种方式(内部或外部)重定向至该应用程序。

Document root is an empty folder. Applications sit outside of the document root. we are trying to use the first token in the URL path to find out which app we try to access, then somehow redirect to it (internally or externally).

这里的结构等同于目录的组织方式。

Here is a structure equivalent to how the directories are organized.

/usr/local/var/www  <- Document Root
/usr/local/var/app1 <- application 1
------------------/public/index.php
------------------/public/css
/usr/local/var/app2 <- application 2
/usr/local/var/app1.stg <- application 1 staging version, code is exactly the same as application1
/usr/local/var/app2.stg <- application 2 staging version, code is exactly the same as application2

以下是httpd.conf中的相关设置

Here are the relevant settings in httpd.conf

DocumentRoot /usr/local/var/www
<Directory "/usr/local/var/www">
    AllowOverride None
    Require all granted
</Directory>
Alias "/app1" "/usr/local/var/app1"
Alias "/app2" "/usr/local/var/app2"

<VirtualHost *:80>
   # rewriting rules to make the routing work
   # There is only one vhost so it can actually be removed
</VirtualHost>

当我们访问 https://sub.domain.com/app1 ,我们希望转到app1
访问 https://sub.domain.com/app1.stg 时,我们希望转到app1.stg

When we access https://sub.domain.com/app1, we expect to go to app1 When we access https://sub.domain.com/app1.stg, we expect to go to app1.stg

应用程序是用PHP编写的。这种服务器配置意味着我们必须在路由和重写规则中包括应用程序的路径,并在所有资源引用中使用完整的绝对路径。
例如,一条路线将如下所示:

The applications are written in PHP. This server configuration means we have to include the "path to the application" in the routes and rewrite rules, and use the "full absolute path" in all the resource references. For example, a route will look like

$router->map("GET", "/app1/action", SomeController);

css引用将是:(即使给出了相对路径,其行为也与相对路径类似到DocRoot(前面带有 /)。您可以在详细的 post

A css reference will be: (even though relative path is given, it behaves just like a relative path to the DocRoot (with "/" in front). You can see it in this detailed post)

<link href="app1/public/css/style.css" type="text/css" rel="stylesheet"/>

这些足以使两个应用程序都能正常工作,但暂存版本无法正常工作,因为它包含完全相同的代码副本(在暂存环境中进行测试,然后推送到生产环境)。
如果我希望两个版本均能工作,则必须动态编码路径,即使用CONTEXT_DOCUMENT_ROOT或其他服务器变量来确定其所在的应用程序版本,并具有两个路由副本,一个以app1开头,其他app1.stg。对于每个版本,我还必须有单独的重写规则。

These will be sufficient to make both apps work, but the staging version is not going to work, because it contains EXACTLY THE SAME copy of code (which is how it's intended to be, to test out in staging environment, then push to production environment). If I want both versions to work, I have to code the paths dynamically, namely using CONTEXT_DOCUMENT_ROOT or some other server variable to figure out which app version it's in, and have two copies of routes, one starting with app1, the other app1.stg. I also have to have separate rewriting rule for each version.

在应用服务器设置限制的情况下(一个域名,用我描述的方式来区分应用程序,等等。)是否可以仅使用相对路径,仅针对应用程序本身编写路由?像这样的

With the server setup restriction applied (one domain name, distinguish apps with the way I described, etc..), is it possible to use only relative paths, write routes with respective to only the app itself? Some like:

<link href="css/style.css" type="text/css" rel="stylesheet"/>

$router->map("GET", "/action", SomeController);

换句话说,我必须在约束内更改服务器设置,这样应用程序才能

In other words, I have to change the server setup within the constraints, so that the app can be written in a way without caring how the server is set up.

我知道一种方法是为每个应用程序/版本使用不同的端口,但是显然服务器管理员不会这样做

I know one way is to use different ports for each app/version, but apparently the server admin doesn't like the idea.

我已将问题分解为 。时间很长,但是如果您愿意遵循,它应该提供更多详细信息。

I've broken down the problem into steps in this question. It's quite long but if you are willing to follow through, it should provide much more details.

如果问题不是足够清楚,每用户目录看起来很像我想要的实现。但是,我希望使用应用程序目录代替用户目录,而不是用户目录。当然,我从来没有使用过每个用户的目录,所以我不知道它是否确实按照我认为的方式运行。

If the question is not clear enough, the per-user directory looks quite like what I want to achieve. But instead of user directory, I want the app directory in place of it. Of course I never used per-user directory so I dont know if it actually behaves the way I think it does.

所以我知道我们可以在/ etc / hosts中将多个主机名映射到一个IP地址。我可以仅使用该主机名作为apache config中的ServerName,并通过键入该主机名在浏览器中进行访问吗?该网站仅供内部使用,因此只能在公司的网络内访问。

So I understand that we can have multiple hostnames in /etc/hosts mapped to one IP address. Can I just use that hostname as the ServerName in apache config, and access in the browser by typing that hostname? The website is for internal usage so should only be accessed within company's network.

在/ etc / hosts中:

In /etc/hosts:

123.45.67.89 app1.team-server-name app2.team-server-name

在httpd.conf中:

In httpd.conf:

<VirtualHost>
    ServerName app1.team-server-name
    DocumentRoot /usr/local/var/app1/public
</VirtualHost>
<VirtualHost>
    ServerName app2.team-server-name
    DocumentRoot /usr/local/var/app2/public
</VirtualHost>


推荐答案

这是一个很长的问题,感谢您提供这么多细节。

This is quite the lengthy question, thank you for providing so much detail.

我会选择与您当前尝试的方法不同的方法。不要将每个应用程序都放在一个文件夹之外,而是将每个应用程序设置为基于域的虚拟主机。使用诸如app1.local之类的名称作为主机名,并确保将条目添加到127.0.0.1下的/ etc / hosts文件中。确保这些虚拟主机的listen指令位于环回(127.0.0.1:80)上。这些应用程序中的每个应用程序都应像安装在其自己服务器的文档根目录下一样运行。所有CSS应该假定其相对于/位于'css / style.css'。

I would opt for a different approach than you are currently attempting. Instead of trying to serve each of these applications out of a folder, set up each of them as a domain based vhost. Use something like app1.local or whatever for the hostname and be sure to add the entries to your /etc/hosts file under 127.0.0.1. Make sure the listen directive for these vhosts is on the loopback (127.0.0.1:80). Each of these apps should function as if they were installed at the document root of their own server. All the CSS should assume its at 'css/style.css' relative to /.

现在,您已经在环回中设置了所有应用,可以设置一个从请求中删除/ app1前缀后,从虚拟主机侦听公共接口上的反向代理以将所有应用程序位置代理到其相应的环回虚拟主机。

Now that you have all of the apps setup on the loopback, you can setup a reverse proxy from the vhost listening on the public interface to proxy all of the application locations to their appropriate loopback vhost after you remove the /app1 prefix from the request.

location  /foo {
  rewrite /foo/(.*) /$1  break;
  proxy_pass         http://app1.local;
  proxy_redirect     off;
  proxy_set_header   Host $host;
}

此方法的最大问题是被代理的应用程序都需要以便在任何地方使用相对路径,或者它们需要在URL之前添加某种可配置的前缀。大多数框架将支持prefix选项。例如: https://laravel.com/docs/5.6/urls 此前缀可以是也用于资产(css / js / jpg)加载,但仅用于执行PHP的文件中。

The biggest issue with this approach is that the applications that are being proxied either need to use relative paths everywhere, or they need to have some kind of configurable prefix that is prepended to the urls. Most frameworks will support the prefix option. For Example: https://laravel.com/docs/5.6/urls This prefix can be used for asset (css/js/jpg) loading as well, but only from files that execute PHP.

这篇关于多个应用程序,同一域下的应用程序的多个版本。如何配置apache服务器,路由和相对路径以使其工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆