K
K
kr_ilya2019-06-02 09:48:53
Nginx
kr_ilya, 2019-06-02 09:48:53

How to make a subdomain on vps?

Good afternoon.

How can I make a subdomain for the site, if now it is arranged like this:
A node is launched in the directory root/site.ru/at 127.0.0.1:3001 and sends the frontend through 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 root/api/another node is launched in the directory at site.ru:3000 - this is api
Now api is accessed via the site.ru:3000/* link , and I want it via the subdomain api.site.ru/* .
As I understand it, you need to create a file, for example, api in sites-enabled and specify the port for node.js (api) 443, but in this case, how to configure this api in sites-enabled so that there are no conflicts with frontend? And will the node swear at an already open port?

PS: I know that it is desirable to keep frontend and backend on different servers, but so far I have....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-06-02
@kr_ilya

1. Create sites-enabled/api.site.ru.conf (don't forget to configure DNS for api.site.ru)
2. Write something like this in it:

spoiler
server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/api.site.ru_access.log; 
ssl_certificate /etc/ssl/api.site.ru.crt; 
ssl_certificate_key /etc/ssl/api.site.ru.key;
server_name api.site.ru; 
root /root/api.site.ru;

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

location / {
# тут заголовки и др.
proxy_pass http://127.0.0.1:3000; # Тут адрес ноды которая отдает API (уже запущенная)
}
}

3. Then you check that everything is OK nginx -t(do not forget about the certificates)
In short, that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question