nginx subdomain configuration example.com/blog

2019-07-04 14:38发布

问题:

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.

回答1:

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;
    }
  }
}


回答2:

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.