简体   繁体   English

nginx - 两个子域配置

[英]nginx - two subdomain configuration

I'm new to Nginx and I'm trying to get subdomains working.我是 Nginx 的新手,我正在尝试让子域正常工作。

What I would like to do is take my domain (let's call it example.com ) and add:我想做的是获取我的域(我们称之为example.com )并添加:

  • sub1.example.com , sub1.example.com ,
  • sub2.example.com , and also have sub2.example.com ,还有
  • www.example.com available. www.example.com可用。

I know how to do this with Apache, but Nginx is being a real head scratcher.我知道如何用 Apache 做到这一点,但 Nginx 是一个真正的头疼的问题。

I'm running Debian 6.我正在运行 Debian 6。

My current /etc/nginx/sites-enabled/example.com:我当前的/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;
    }
}

It is working to serve example.com and www.example.com.它正在为 example.com 和 www.example.com 提供服务。

I have tried to add a second server block in the same file like:我试图在同一个文件中添加第二个服务器块,例如:

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;
        }
}

No luck.没运气。 Any ideas?有什么想法吗? I'd super appreciate any feedback.我非常感谢任何反馈。

The mistake is putting a server block inside a server block, you should close the main server block then open a new one for the sub domains错误是将服务器块放在服务器块中,您应该关闭主服务器块然后为子域打开一个新的

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
}

You just need to add the following line in place of your server_name您只需要添加以下行来代替您的 server_name

server_name xyz.com  *.xyz.com;

And restart Nginx.并重新启动 Nginx。 That's it.而已。

You'll have to create another nginx config file with a serverblock for your subdomain.您必须为您的子域创建另一个带有 serverblock 的 nginx 配置文件。 Like so:像这样:

/etc/nginx/sites-enabled/subdomain.example.com
  1. Add A field for each in DNS provider with sub1.example.com and sub2.example.com使用sub1.example.comsub2.example.com为每个 DNS 提供商添加一个字段

  2. Set the servers.设置服务器。 Keep example.com at last最后保留example.com

As below如下

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. Restart Nginx sudo systemdctrl restart nginx重启 Nginx sudo systemdctrl restart nginx

There is a very customizable solution, depending on your server implementation:有一个非常可定制的解决方案,具体取决于您的服务器实现:

Two (or more) SUBdomains in a single nginx "sites" file?单个 nginx“站点”文件中的两个(或更多)子域? Good if you own a wildcard TLS certificate, thus want to maintain ONE nginx config file.如果您拥有通配符 TLS 证书,因此想要维护一个 nginx 配置文件,那就太好了。 All using same services BUT different ports?都使用相同的服务但​​不同的端口? (Think of different app versions running concurrently, each listening locally to differents ports) (想想同时运行的不同应用程序版本,每个版本都在本地侦听不同的端口)

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;
    }

I know the performance will "suck", but then again, since the decision was to go with one server for all it's like complaining that an econobox cannot haul as much people as a bus because the little car has a "heavy" roof rack on top of it.我知道性能会“糟透了”,但话又说回来,既然决定使用一台服务器就好像在抱怨经济舱不能像公共汽车一样拖运那么多人,因为这辆小车上有一个“重”的车顶架最重要的。

A regex expert could potentially improve the performance for such a custom solution, specially since it could ommit the CPU expensive "if" conditional statements.正则表达式专家可能会提高这种自定义解决方案的性能,特别是因为它可以省略 CPU 昂贵的“if”条件语句。

Maybe this could help someone having a challenge with this, this got me grinding the whole day.也许这可以帮助遇到这个问题的人,这让我磨了一整天。 First, if you have SSL installed, remove it (delete better still), this would help reset the previous configuration that's disrupting the subdomain configuration.首先,如果您安装了 SSL,请将其删除(最好还是删除),这将有助于重置之前破坏子域配置的配置。

in etc/nginx/.../default file create a new server block在 etc/nginx/.../default 文件中创建一个新的服务器块

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM