3

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.

Arturo
  • 3,254
  • 2
  • 22
  • 61

2 Answers2

1

First: it's not a subdomain, but a subfolder called blog.

If you want to run two apps where one appears in a subfolder, you could do the following

Define two upstreams / proxy pass them to different ports the

Have them in the same config file then

Have two location blocks (location / and location /blog)

Does that make sense? Otherwise one will probably shadow the other.

  • But it is not in a subfolder, right now they have diff folders: both in www, so in /var/www I have site1Folder and site2Folder. Should I put one inside the other? – Arturo Jul 14 '18 at 21:57
  • No, it doesn't matter to nginx where they are, they need to be passed to the port where the apps are running. – Jonathan M. Hethey Jul 14 '18 at 21:59
  • How do I define two upstreams,I pick random ports? for example: proxy_pass http://127.0.0.1:2368; and proxy_pass http://127.0.0.1:2223; ? – Arturo Jul 14 '18 at 22:01
  • You need to have your node app running on 2222 (according to current config) and node/ghost on the other. Unlike with for example php where the files can just sit there and wait for the server to execute them. Does that make sense? – Jonathan M. Hethey Jul 14 '18 at 22:03
  • If I define the port like: proxy_pass http://127.0.0.1:2222; it says: 502 Bad Gateway . Do I have to change something in the command line? Open that port or somethning? if so, how? sudo ufw allow 2222 ? – Arturo Jul 14 '18 at 22:27
  • Is your node app running on port 2222? – Jonathan M. Hethey Jul 14 '18 at 22:29
  • Remember that for now, I only have a statict page at site1.com and a ghost blog at site2, there is no nodejs app running in the static page. I want to make it so site1/site2. The site1 is not running at any specific port, If you see at the code, I commented out that line and it works – Arturo Jul 14 '18 at 22:31
  • Ah, than you'll want to disable that location block for now and just serve your static file, still both location blocks in one file I recommend. https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/ – Jonathan M. Hethey Jul 14 '18 at 22:41
0

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;
    }
  }
}
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • I’m going to study better how nginx work because I can’t make it work like that. Thanks tho – Arturo Jul 16 '18 at 01:59