node.jsnginxsubdomainubuntu-16.04ghost

nginx subdomain configuration example.com/blog


I've spent all day yesterday and today learning how nginx works and I got two different domains working, one with Ghost blogging platform and a static page (future NodeJS app), now I'm trying to setup the subdomain, but I'm kind of frustrated because I feel like I'm almost there but it's not working... This is my current setup:

#Main Domain
server {
    listen 80;
    listen [::]:80;

    server_name example.com;
    root /var/www/portfolio;
    index index.html;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
#        proxy_pass http://127.0.0.1:2222;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}


#Sub domain
server {
    listen 80;
    listen [::]:80;

    server_name example.com/blog;
    root /var/www/ghost/system/nginx-root;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

The idea is to create mysite.com/blog where eventually mysite would be a nodejs app, prob linking the route later will be another problem but... one at a time lol, how can I establish that subdomain? If I separate the config file into a separate file, I would get the other domain working :/

Thanks

EDIT: I've found that with bucket in S3 on AWS I could acomplish that, but now I don't need it for what I'm doing jeje, but it's good to know.


Solution

  • Note: This is not a complete answer, you probably will need to tinker a bit

    As @Jonathan mentioned, from nginx's point of view this is the same site, but you need nginx to handle both locations differently.

    Here's how it would look like

    server {
        listen 80;
        listen [::]:80;
    
        server_name example.com;
        root /var/www/portfolio;
        index index.html;
        client_max_body_size 50m;
    
        location / {
          # your normal location settings
        }
    
    
        # your blog is defined here
        location /blog {
          root /var/www/ghost/system/nginx-root;
    
          # You'll probably need to do a rewrite here, because a
          # /blog/article needs to be passed as `/article` to the
          # app server
    
          # rewrite ^/blog/(.*) $1;
    
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $http_host;
          proxy_pass http://127.0.0.1:2368;
        }
      }
    }