【问题标题】:nginx - two subdomain configurationnginx - 两个子域配置
【发布时间】:2013-07-08 06:50:11
【问题描述】:

我是 Nginx 的新手,我正在尝试让子域正常工作。

我想做的是获取我的域(我们称之为example.com)并添加:

  • sub1.example.com,
  • sub2.example.com,还有
  • www.example.com 可用。

我知道如何使用 Apache 来做到这一点,但 Nginx 实在是让人头疼。

我正在运行 Debian 6。

我当前的 /etc/nginx/sites-enabled/example.com:

server {
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;

    location / {
        index  index.html index.htm;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}

它正在努力为 example.com 和 www.example.com 提供服务。

我尝试在同一个文件中添加第二个服务器块,例如:

server {
        server_name www.example.com example.com;
        access_log /srv/www/www.example.com/logs/access.log;
        error_log /srv/www/www.example.com/logs/error.log;
        root /srv/www/www.example.com/public_html;

        server {
            server_name sub1.example.com;
            access_log /srv/www/example.com/logs/sub1-access.log;
            error_log /srv/www/example.com/logs/sub1-error.log;
            root /srv/www/example.com/sub1;
    }
        location / {
            index  index.html index.htm;
        }

        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
        }
}

运气不好。有任何想法吗?我非常感谢任何反馈。

【问题讨论】:

  • 我应该提到:最终目标是 sub1.example.com 到 example.com/sub1 和 sub2.example.com 到 example.com/sub2。我希望这是有道理的。

标签: nginx subdomain


【解决方案1】:

也许这可以帮助遇到挑战的人,这让我整天都在努力。 首先,如果您安装了 SSL,请将其删除(最好还是删除),这将有助于重置破坏子域配置的先前配置。

在 etc/nginx/.../default 文件中 创建一个新的服务器块

    server {
       listen 80; 
       server_name subdomain.domain.com;

       location /{
         proxy_pass http://localhost:$port;
       }
    }

【讨论】:

    【解决方案2】:
    1. 使用 sub1.example.comsub2.example.com

      为 DNS 提供商中的每个添加 A 字段
    2. 设置服务器。最后保留 example.com

    如下

    server {
        server_name sub1.example.com;
        # sub1 config
    }
    server {
        server_name sub2.example.com;
        # sub2 config
    }
    server {
        server_name example.com;
        # the rest of the config
    }
    
    1. 重启 Nginx sudo systemdctrl restart nginx

    【讨论】:

      【解决方案3】:

      有一个非常可定制的解决方案,具体取决于您的服务器实现:

      单个 nginx“站点”文件中有两个(或更多)子域?如果您拥有通配符 TLS 证书,那就太好了,因此想要维护一个 nginx 配置文件。都使用相同的服务但​​不同的端口? (想想不同的应用版本同时运行,每个版本都在本地监听不同的端口)

      server {
          listen 443 ssl http2;
          listen [::]:443 ssl http2;
          server_name ~^(?<sub>.+).example.com;
      
          # default app (the "default" ports, good for the "old" app)
          set $app 19069;
          set $app-chat 19072;
      
          # new app
          # new.example.com
          if ( $sub = "new" ) {
              set $app 18069;
              set $app-chat 18072;
          }
          # upstreaming
          location / {
              proxy_redirect off;
              proxy_pass http://127.0.0.1:$app;
          }
      
          location /longpolling {
              proxy_pass http://127.0.0.1:$app-chat;
          }
      

      我知道性能会“很糟糕”,但话又说回来,既然决定只使用一台服务器,这就像抱怨经济箱不能像公共汽车那样载人,因为这辆小汽车有一个“重”车顶行李架。

      正则表达式专家可能会提高这种自定义解决方案的性能,特别是因为它可以省略 CPU 昂贵的“if”条件语句。

      【讨论】:

        【解决方案4】:

        您必须为您的子域创建另一个带有服务器块的 nginx 配置文件。像这样:

        /etc/nginx/sites-enabled/subdomain.example.com
        

        【讨论】:

        • 不得不进行大量搜索以确定是否将其称为“子域”或“subdomain.domain.com”。令人惊讶的是有多少文档未能指定。
        【解决方案5】:

        您只需要添加以下行来代替您的 server_name

        server_name xyz.com  *.xyz.com;
        

        然后重启 Nginx。就是这样。

        【讨论】:

        • 如果您希望子域指向服务器上的不同资源,而不是 www.example.com 位置的资源,该怎么办?
        【解决方案6】:

        错误是将服务器块放入服务器块中,您应该关闭主服务器块然后为子域打开一个新的

        server {
            server_name example.com;
            # the rest of the config
        }
        server {
            server_name sub1.example.com;
            # sub1 config
        }
        server {
            server_name sub2.example.com;
            # sub2 config
        }
        

        【讨论】:

        • 当我尝试这个时,它找不到站点,我让它在不同的域中工作,我只是在测试,这不起作用:blog.example.com
        • @Arturo 你能分享完整的服务器块吗?
        • 也许这在 7 年前有效。目前,“server_name example.com;”捕获以“example.com”结束的所有内容,因此您无法区分普通域或子域。因此“subX.example.com”和“example.com”都被第一个服务器块捕获和处理,因此后续块永远不会出现。
        • @alejandrob 你找到解决这个问题的方法了吗?
        猜你喜欢
        • 1970-01-01
        • 2012-04-11
        • 2014-01-24
        • 1970-01-01
        • 2018-12-19
        • 2013-12-04
        • 2018-12-22
        • 1970-01-01
        • 2016-08-23
        相关资源
        最近更新 更多