trowa159
By:
trowa159

Serving two websites on one Nginx

April 14, 2016 1k views
LAMP Stack Nginx

hi there,

I have searched through the Digital Ocean community for this problem that I am having and I was not able to resolve it.

I'm trying to point

app A -> siteA.com
dev app A -> dev.siteA.com

what is happening
dev app A -> dev.siteA.com
app A -> dev.siteA.com

here is my *zone file *

$ORIGIN siteA.com.
$TTL 1800
siteA.com. IN SOA ns1.digitalocean.com. hostmaster.siteA.com. 1460656377 10800 3600 604800 00000
siteA.com. 1800 IN NS ns1.digitalocean.com.
siteA.com. 1800 IN NS ns2.digitalocean.com.
siteA.com. 1800 IN NS ns3.digitalocean.com.
siteA.com. 1800 IN A 000.000.00.00
www.siteA.com. 1800 IN CNAME siteA.com.
siteA.com. 1800 IN MX 10 store1.test.net.
siteA.com. 1800 IN MX 0 smtp.test.net.
dev.siteA.com. 1800 IN CNAME siteA.com.

here is my Nginx setup for siteA

**server_tokens off; # for security-by-obscurity: stop displaying nginx version

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

# HTTP
server {
        listen 80; # if this is not a default server, remove "default_server"
                location ~* "^/[a-z0-9]{40}\.(css|js)$" {
                        root /home/siteA/bundle/programs/web.browser;
                        access_log off;
                        expires max;
                }

        server_name siteA.com; .

                location / {
                        proxy_pass http://localhost:8080;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection "upgrade";
                        proxy_set_header Host $host;
                }
}

here is my Nginx setup for dev.siteA

# this section is needed to proxy web-socket connections
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

# HTTP
server {
        listen 80; # if this is not a default server, remove "default_server"
        server_name dev.siteA.com; 


location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # allow websockets
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
        # the root path (/) MUST NOT be cached
        if ($uri != '/') {
            expires 30d;
        }
    }
}

thank you for reading ;)

2 Answers

It appears that this is configured correctly but both sites are passing all requests through to the service running on port 8080 on your droplet. For dev.siteA it shows 127.0.0.1:8080 while for siteA you have localhost:8080 - these two addresses point to the same location.

  • @ryanpq thank you for replying!

    I managed to get in working by adding

    in siteA

    listen 80 default_server;
    listen [::]:80 default_server;
    

    in dev.siteA

    listen 80;
    listen [::]:80;
    

    I still don't quite get what the defult_server name space is for though @@

It's well known that the LEMP stack (Linux, nginx, MySQL, PHP) provides unmatched speed and reliability for running PHP sites. Other benefits of this popular stack such as security and isolation are less popular, though. In this article we'll show you the security and isolation benefits of running sites on LEMP with different Linux users. This will be done by creating different php-fpm pools for each nginx server block (site or virtual host).
Have another answer? Share your knowledge.