nginx - two subdomain configuration

后端 未结 5 952
耶瑟儿~
耶瑟儿~ 2020-12-23 13:17

I\'m new to Nginx and I\'m trying to get subdomains working.

What I would like to do is take my domain (let\'s call it example.com) and add:

    <
相关标签:
5条回答
  • 2020-12-23 13:52
    1. Add A field for each in DNS provider with sub1.example.com and sub2.example.com

    2. Set the servers. Keep example.com at last

    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
    0 讨论(0)
  • 2020-12-23 14:03

    You just need to add the following line in place of your server_name

    server_name xyz.com  *.xyz.com;
    

    And restart Nginx. That's it.

    0 讨论(0)
  • 2020-12-23 14:04

    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
    }
    
    0 讨论(0)
  • 2020-12-23 14:11

    You'll have to create another nginx config file with a serverblock for your subdomain. Like so:

    /etc/nginx/sites-enabled/subdomain.example.com
    
    0 讨论(0)
  • 2020-12-23 14:14

    There is a very customizable solution, depending on your server implementation:

    Two (or more) SUBdomains in a single nginx "sites" file? Good if you own a wildcard TLS certificate, thus want to maintain ONE nginx config file. 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.

    0 讨论(0)
提交回复
热议问题