Nginx 用户子域,我应该使用 proxy_pass 吗?

发布于 2024-10-20 20:16:43 字数 429 浏览 1 评论 0原文

我正在尝试设置用户子域,从特定文件夹提供内容:从 username.example.com 提供的 www.example.com/username (就像 github 页面一样)。

我研究过 Nginx 重写,但我不希望浏览器重定向——我希望域名是 username.example.com。 无论如何,对这个问题的评论说我无法重写主机,只能代理它。 我尝试设置 proxy_pass,但所有文档和示例都显示它用于(显然)代理到另一个主机或端口上的服务,但就我而言,我只想代理到另一个主机或端口位于同一主机和端口上。

这是解决这个问题的适当方法吗?如果是,正确的 Nginx 配置语法是什么?

I am trying to setup user subdomains, serving content from specific folders: www.example.com/username served from username.example.com (just like github pages).

I've looked at Nginx rewrites, but I don't want the browser to redirect--I want the domain to be username.example.com.
Anyway, a comment on this question says that I cannot rewrite host, only proxy to it.
I tried to setup a proxy_pass, but all of the documentation and examples show it being used to (obviously) proxy to a service on another host or port, but in my case I want to just proxy to another location on the same host and port.

Is this the appropriate way to tackle this problem, and if so, what is the right Nginx config syntax?

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

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

发布评论

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

评论(1

夜血缘 2024-10-27 20:16:43

像这样的东西应该几乎可以工作:

server {
  location / {
    # simple version
    if ( $host ~ "user1.example.com" ) {
      proxy_pass http://example.com/user1;
    }

    # generic version
    if ( $host ~ ^(.+)\.example\.com$ ) {
      proxy_pass http://example.com/$1;
    }


  }
}

但我怀疑它会按预期工作,因为代理网址中的“/user1”部分,但我不确定后果。

另一种肯定有效的方法是使用同一个 nginx 服务器为所有应用程序提供服务,或者为每个其他 nginx 分配给定端口。
我会这样做:

server {
  location / {

    if ( $host ~ "user1.example.com" ) {
      proxy_pass http://127.0.0.1:8000;
    }

    if ( $host ~ "user2.example.com" ) {
      proxy_pass http://127.0.0.1:8001;
    }
  }
}

上次我这样做是为了一个大学项目,我使用了一个特定的插件来处理它,可能还有一些插件可以为 nginx 执行此操作,但在快速搜索后我没有找到任何插件。

Something like this should almost work:

server {
  location / {
    # simple version
    if ( $host ~ "user1.example.com" ) {
      proxy_pass http://example.com/user1;
    }

    # generic version
    if ( $host ~ ^(.+)\.example\.com$ ) {
      proxy_pass http://example.com/$1;
    }


  }
}

But I doubt it will work as expected because of the "/user1" part in the proxied url, I am not sure of the consequences though.

Another way of doing this which will surely work is to either serve all the applications with the same nginx server or assign a given port to each of the other nginx.
Here is how I would do this:

server {
  location / {

    if ( $host ~ "user1.example.com" ) {
      proxy_pass http://127.0.0.1:8000;
    }

    if ( $host ~ "user2.example.com" ) {
      proxy_pass http://127.0.0.1:8001;
    }
  }
}

Last time I did this was for a college project and I used a specific plugin to handle it, there may also be some plugin which can do this for nginx but I did not found any after a quick search.

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