nginx - two subdomain configuration

后端 未结 5 971
耶瑟儿~
耶瑟儿~ 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 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 ~^(?.+).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.

提交回复
热议问题