nginx:所有命名子域到同名子目录,没有信息返回浏览器

标签 nginx

我在之前的许多帖子中都非常接近找到答案,但没有什么是完美的。所以请原谅再次询问。我认为这可能是一个相当常见的问题,因此值得得到一个简洁的答案。

(1) 我(也)想在我的网络服务器上使用子目录,即使浏览器持续认为它正在处理子域。也就是说,我不希望浏览器从服务器接收 301 [因此没有返回指令]。 (2) 我不想指定域列表。我希望它对所有人都有效。

具体来说,我希望 nginx 将所有 $1.example.com 解释为 example.com/$1/,而不指定 $1 可能是什么的列表。因此,http://funny.example.com/在浏览器中应始终检索与 http://example.com/funny/ 相同的页面和 http 代码。 。这也应该适用于所有子文件。 http://funny.example.com/d1/p1.html应该产生与 http://example/funny/d1/p1.html 相同的结果。这不是位置重映射,而是 url 重映射。但是,如果/var/www/example.com/是我的顶级目录,那么可能会提供/var/www/example.com/funny/d1/p1.html。

从浏览器的角度来看,他们只是碰巧看到了完全相同的页面 http://funny.example.com/http://example.com/funny/ .

解决方案表示赞赏。

最佳答案

按照 ngx_http_core module docs 尝试以下操作和 Server names docs

server {
    # sub domains config, match with regular expression *.example.com
    server_name ~^(?<sub_domain>.+)\.example\.com$;
    location / {
        root /var/www/examples.com/funny/$sub_domain;
    }
}

server {
    # catch all config
    server_name _;

    location / {
        root /var/www/examples.com;
    }
}

关于nginx:所有命名子域到同名子目录,没有信息返回浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32786616/

相关文章:

node.js - 将端口 443 用于 Socket.IO 是个坏主意吗?

php - Laravel 未在 Ubuntu NGINX 上运行 Cronjob

linux - 如何在 Pressmatic 上用 PHP 启用 SOAP 客户端

visual-studio - 调试不适用于 docker compose with Visual Studio

nginx - ASP.NET Core + Redis + nginx 的 session id 总是在变化

java - Openfire 因 Java 错误而崩溃

python - nginx:[警告] 0.0.0.0:80 上的冲突服务器名称 "example.com",已忽略

ruby-on-rails - 与 unicorn/thin/puma/etc 相比 passenger 5 的性能

wordpress - nginx 子路径重定向到 wordpress docker 容器

python - 运行的 uwsgi 线程过多