nginx subdomain configuration example.com/blog
Asked Answered
E

2

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.

Epideictic answered 14/7, 2018 at 21:50 Comment(2)
example.com/blog is not a subdomain.. blog.example.com would beInheritrix
what you need to do is create a example.com server block, and then define location /blog and do a rewrite before passing to the app server.Melisenda
M
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;
    }
  }
}
Melisenda answered 14/7, 2018 at 23:5 Comment(1)
I’m going to study better how nginx work because I can’t make it work like that. Thanks thoEpideictic
B
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.

Binnacle answered 14/7, 2018 at 21:55 Comment(8)
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?Epideictic
No, it doesn't matter to nginx where they are, they need to be passed to the port where the apps are running.Binnacle
How do I define two upstreams,I pick random ports? for example: proxy_pass 127.0.0.1:2368; and proxy_pass 127.0.0.1:2223; ?Epideictic
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?Binnacle
If I define the port like: proxy_pass 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 ?Epideictic
Is your node app running on port 2222?Binnacle
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 worksEpideictic
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. docs.nginx.com/nginx/admin-guide/web-server/…Binnacle
M
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;
    }
  }
}
Melisenda answered 14/7, 2018 at 23:5 Comment(1)
I’m going to study better how nginx work because I can’t make it work like that. Thanks thoEpideictic

© 2022 - 2024 — McMap. All rights reserved.