【问题标题】:Nginx user subdomains, should I proxy_pass?Nginx用户子域,我应该proxy_pass吗?
【发布时间】:2011-07-12 02:46:51
【问题描述】:

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

我查看了 Nginx 重写,但我不希望浏览器重定向——我希望域是 username.example.com。 无论如何,this question 上的评论说我不能重写主机,只能代理它。 我尝试设置proxy_pass,但所有文档和示例都显示它用于(显然)代理到另一个主机或端口上的服务,但在我的情况下,我只想代理到同一主机上的另一个位置和端口。

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

【问题讨论】:

    标签: nginx subdomain proxypass


    【解决方案1】:

    这样的事情应该几乎可以工作:

    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 做这个,但我快速搜索后没有找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 2023-03-08
      • 2015-10-25
      • 2011-03-02
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多