Web 编程中的子域与子目录

发布于 2024-08-15 20:45:55 字数 328 浏览 5 评论 0原文

处理网络上的多个“应用程序”有两种主要策略:

  • 子域名(例如 wiki.example.org、blog.example.org、admin.example.org、api.example.org/v1)
  • 子目录(例如 example.org) /wiki, example.org/blog, example.org/admin, example.org/api/v1)

这两种解决方案在处理 Web 编程时(例如在代码组织、浏览器方面)有什么区别(优点和缺点)安全模型、javascript 等)。

编辑:CW,因为有一个正确的答案,但它非常广泛。

There are two main strategies for handling multiple "applications" on the web:

  • subdomains (e.g. wiki.example.org, blog.example.org, admin.example.org, api.example.org/v1)
  • subdirs (e.g. example.org/wiki, example.org/blog, example.org/admin, example.org/api/v1)

What are the differences (advantages and disadvantages) of these two solution when dealing with web programming (e.g. in terms of code organization, browsers security models, javascript etc).

Edit: CW as there's a correct answer, but it's very broad.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

天气好吗我好吗 2024-08-22 20:45:55

除了从安全角度来看在子域中隔离应用程序更容易这一事实之外,我将仅评论我认为两者之间最大的区别。

子域的专业版:

  • 您可以隔离每个域的配置(例如 apache)。
  • 将应用程序的某些部分迁移到其他机器会更容易。子目录不会真正给你这种灵活性。
  • 您可以假设应用程序的根目录始终为 /,而不必在每个 html 模板中使用 $baseUri 变量。

缺点:

  • 快速设置临时或临时开发环境会更烦人。对于每个“应用程序”,您现在都需要主机文件条目的 DNS 和网络服务器配置。使用子目录,您可以将应用程序放入目录中,然后开始!
  • 如果您确实需要将应用程序部署在不同的系统上,而由于某些奇怪的策略而无法使用 / ,则可能需要进行一些重写。

我的建议:

确保您始终可以两者兼得,这将为您带来两全其美的效果。应用程序的每个部分都应该有一个始终受到尊重的可配置基本 uri。只要你确保你总是可以双向前进,那么谁在乎你做什么呢?它只是一个 url,并且可以随时更改。

Besides the fact that from a security standpoint it is a bit easier to isolate an app within a subdomain, I will just comment on what I think is the biggest difference between the two.

Pro's for subdomains:

  • You can isolate configuration (for for example apache) per-domain.
  • It will be easier to migrate parts of your application to other machines. Sub-directories won't really give you this flexibility.
  • Instead of having to use a $baseUri variable in every html template, you can just assume the root of the app is always /.

Cons:

  • It will be much more annoying to quickly setup staging or temporary development environments. For every 'app' you will now need DNS of hosts-file entries and webserver configuration. With subdirectories you could drop the app in a directory, and go!
  • If you do ever have the requirement to deploy your application on a different system where using / is because of some odd policy not possible, some rewriting might be in order.

My advice:

Make sure you can always do both, which will give you the best of both worlds. Every part of your app should have a configurable base uri that is always respected. As long as you make sure you can always go both ways, then who cares what you do? it's just a url and it can always be changed.

ま柒月 2024-08-22 20:45:55

就我个人而言,我更喜欢为每个应用程序使用子域,然后是子目录(无论它们是否实际上是子目录 - 最好它们只是通过.htaccess)来表示该应用程序的不同状态。例如:

admin.blah.com/users/1234/bob
admin.blah.com/pages/4321/title
blog.blah.com/archives/2007/5678/title
子域告诉您

您在哪里,子目录告诉您您在做什么。

Personally, I prefer using a subdomain for each application, and then the sub-dirs (whether they are actually sub-directories or not -- preferably they are just re-routed to /index.php by a .htaccess) to denote different states of that application. For example:

admin.blah.com/users/1234/bob,
admin.blah.com/pages/4321/title,
blog.blah.com/archives/2007/5678/title,
etc.

The subdomain tells you where you are, and the sub-directories tell you what you're doing.

暮倦 2024-08-22 20:45:55
  • 在代码组织方面:差异为零,因为您可以将子域映射到任何目录。
  • 在浏览器安全方面:跨子域的 JavaScript 访问是可能的,但存在障碍(请参阅 document.domain和配偶)。我不知道 JavaScript 方面有什么在处理不同子域时完全不可能的事情。

意见

我个人倾向于目录,反对公共地址的子域。公众已经习惯了以“www”开头的网址。打破这种模式会造成不必要的混乱。您会注意到,很多时候,当人们在地址栏中输入子域名时,他们会自动开始输入“www”。他们会惊讶地发现地址可以没有。

对我来说,使用子域的唯一好方法是出于内部目的,以方便或准备使用不同的服务器(例如 static.example.com、images.example.com 等)

  • In terms of code organization: The differences are nil, as you can map subdomains to any directory.
  • In terms of browser security: JavaScript access across subdomains is possible but has obstacles (see document.domain and consorts). I do not know of anything on the JavaScript side that is completely impossible when working with different sub-domains.

Opinion:

I personally tend towards directories and against subdomains for public addresses. The general public have become used to web addresses beginning with "www." and it creates unnecessary confusion to break this pattern. You will notice that very often people, when given a subdomain to type into the address bar, will automatically start typing in "www." and they will be surprised to learn that an address can be without.

To me, the only good way to use subdomains is for internal purposes to facilitate, or prepare for, the use of different servers (e.g. static.example.com, images.example.com etc.)

爱殇璃 2024-08-22 20:45:55

已经有很好的答案,因此您可以随时查看这些链接。

There have been excellent answers already, so you could check out these links anytime.

秋千易 2024-08-22 20:45:55

我认为使用子域(而不是子/目录)的三个最重要的原因是安全、安全和安全。子/目录将服务器会话暴露给黑客。

例如,如果我将两个网络应用程序放入目录中,那么它们通常会共享同一个会话(除非采取特定步骤来防止/维护它)。因此,如果我有:

domain.com/application1

domain.com/application2

如果 application2 被黑客攻击,那么黑客可以看到 application1 中设置的所有会话变量。此时,您的会话安全性已降低到最弱子应用程序的级别,即链中最薄弱的环节。

I think the three most important reasons for using sub.domains, as opposed to sub/directories, are security, security and security. Sub/directories expose the server session to hackers.

For example, if I have two web apps that I put into directories then they will typically share the same session (unless specific steps are taken to prevent/maintain it). So if I have:

domain.com/application1

domain.com/application2

If application2 gets hacked then the hacker can see all the session variables that were set in application1. At this point your session security is reduced to the level of the weakest subapp, i.e., the weakest link in the chain.

寻找我们的幸福 2024-08-22 20:45:55
  • 您可以轻松地在子域上“创建”虚拟服务器。
  • 您可以将子域分离到不同的 cookie。

您最好认识到子域是网络空间中的“主要”分隔,而子目录是“次要”分隔。子域名适用于不同的域名;您可以让不同的人在不同的子域上运行不同的应用程序。子目录是单个(子)域的分区,可能由同一用户分隔不同的应用程序。

Web 标准故意非常开放,但你越滥用它们来创建奇怪的层次结构,最终就会越痛苦。

  • You can easily 'do' virtual servers on subdomains.
  • You can separate out subdomains to different cookies.

You'd be best off recognizing that subdomains are a "major" separation in Web space, and subdirectories a "minor" one. Subdomains are for, well, different domains; you could have different people running different applications on different subdomains. Subdirectories are partitions of a single (sub)domain, separating perhaps different applications by the same user.

Web standards are intentionally very open, but the more you abuse them to create strange hierarchies, the more that will bite you in the end.

扬花落满肩 2024-08-22 20:45:55

子域的主要优点之一是它们指向的文件可以包含在任何地方,甚至可以包含在另一台服务器上。我对子域最常见的用法是正在进行的实时项目的开发。例如,您可以创建子域:

dev.example.com

并复制您的实时站点,包括整个目录结构。放入 .htaccess 文件以拒绝除您和您客户端的 IP 之外的任何人的连接,并使用该文件进行更改,直到准备好实时推送更新为止。

One of the major advantages of sub-domains is the files they point to can be contained anywhere -- even on another server. My most common usage of a sub-domain is on going development of a live project. For example, you could create the sub-domain:

dev.example.com

and make a copy of your live site, including the entire directory structure. Drop in an .htaccess file to refuse connections from anyone but you and your client's IP, and use this to make changes until the updates are ready to be pushed live.

骑趴 2024-08-22 20:45:55

这是一个相当大的主题,但这里有一些关于子域的想法...

优点

  • 可以使用“干净”的路由设置,如果
    您正在使用 MVC。

  • 各个部分之间的划分非常清晰
    系统和命名空间没有危险
    重叠。

缺点

  • 缺点是,可能
    上的重复代码更多
    后端,除非你很聪明
    库/包含目录和使用
    一个共享区域。

从SEO的角度来看,没有
这些天有很大的不同
确实还有像 Google 这样的工具
可以指示 Analytics 使用特定的日志记录域。

This is a pretty big topic, but here’s some thoughts on sub-domains...

Pros

  • Can use a ‘clean’ routing setup if
    you’re using MVC.

  • Very clear division between parts of
    the system and no danger of namespace
    overlap.

Cons

  • On the downside, there's likely to
    be more duplication of code on the
    backend, unless you're clever with
    library/includes directories and use
    a shared area.

From an SEO perspective, there's not
a great deal of difference these days
and indeed tools such as Google
Analytics can be instructed to use a specific logging domain.

阿楠 2024-08-22 20:45:55

我个人使用flask进行开发
所以设置一个(两者)并不麻烦,

通常我会使用子目录
最好使用子域。

但在某些情况下,如果您有像 host.tld/user/repository/commit/... 这样的子目录,
我宁愿像 user.host.tld/repo/commit 那样

,如果他们以另一种方式输入域,我通常使用会话进行重定向,

例如:如果他们像 host 一样输入域,我将重定向.tld/<用户>/<存储库>/<提交>
user.host.tld/repo/commit

让用户更容易,同时您可以以更好的方式安排您的应用程序

,简而言之,它确实是主观的

I personally use flask for development
so it isnt much of a hassle to set one up(both)

Usually i go with sub directories
but in some situations it is better to use subdomains

If you have subdirectories like host.tld/user/repository/commit/...
Id rather go like user.host.tld/repo/commit

And i usually use sessions to redirect if they type the domain in another way

eg:I will redirect the if they typed it like host.tld/<user>/<repo>/<commit>
to user.host.tld/repo/commit

Making it easier for the user meanwhile you can arrange your application in a better way

in short its really subjective

贱贱哒 2024-08-22 20:45:55

这取决于您的喜好。出于SEO目的,子目录可以更好地提高域名权重。

就个人而言,我对相关的不同应用程序使用子目录,同时对托管在不同服务器上的静态资产使用子域,以减少对子目录的请求数量。

It depends on your preference. For SEO purpose, subdirectory is better to boost the domain authority.

Personally, I used subdirectories for different apps that are related while I used subdomain for static assets hosted on a different server to reduce the number of requests to the subdirectories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文