How to make subdomain for my vps?

Good afternoon.

How do I make a subdomain for your site, if now it is arranged as follows:
In the directory root/site.ru/ node started at 127.0.0.1:3001 and gives fronend using nginx.
sites-enabled/default

map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}

server {
listen 80;
access_log /var/log/nginx/access.log;
return 301 https://$host$request_uri;
server_name site; # setup your domain here
root /root/site;

gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;

location / {
expires $expires;

"proxy_redirect" off;
proxy_set_header Host $Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3001; # set the adress of the Node.js instance here
}
}

server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/access.log;
ssl_certificate /etc/ssl/site.crt;
ssl_certificate_key /etc/ssl/site.key; # the port nginx is listening on
server_name site; # setup your domain here
root /root/site;

gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;

location / {
expires $expires;

"proxy_redirect" off;
proxy_set_header Host $Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3001; # set the adress of the Node.js instance here
}
}


And in the directory root/api/ runs another node at site.EN:3000 is a api
Now, requests to the api occur on the link site.EN:3000/*, and I want the subdomain api.site.ru/*.
As I understand it you need to create a file for example api in sites-enabled and set the port for node.js (api) 443, but in this case, to configure the api in sites-enabled, to avoid contradictions with freedom? And whether Noda to swear on an already open port?

PS: I know that it is desirable to keep the frontend and backend on different servers, but I have so far....
March 23rd 20 at 18:44
1 answer
March 23rd 20 at 18:46
Solution
1. Create sites-enabled/api.site.EN.conf (don't forget to configure DNS for api.site.ru)
2. Write it like this:
spoiler
server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/api.site.ru_access.log; 
ssl_certificate /etc/ssl/api.site.EN.crt; 
ssl_certificate_key /etc/ssl/api.site.EN.key;
server_name api.site.ru; 
root /root/api.site.EN;

gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;

location / {
# there are headers etc.
proxy_pass http://127.0.0.1:3000; # Here the address of the node which gives the API (already running)
}
}

3. Then check that everything is OK nginx -t (don't forget about certificates)

In a nutshell that's it.
Another reload of nginx add, and then there is not that :) - gladyce_Leffler commented on March 23rd 20 at 18:49

Find more questions by tags Node.jsHostingDomain name systemNginx