V
V
Vladislav2021-11-15 16:17:00
Nginx
Vladislav, 2021-11-15 16:17:00

Setting up nginx hosts and default?

Sysadmins, help me out.
Ubuntu 20 nginx + php-fpm 7.4
All day long trying to figure out why it works this way and not otherwise.
I have a domain attached to the VPS from the hoster: https://vm2888385.33ssd.had.wf/
I also attached mine and redirected it to the server IP via the A records: https://vladislav.in/

Installed nginx and puff and configured the host for the second link.
I need the subdomain still in a separate folder. So you need to create another config file, did it, everything opens the site too: https://files.vladislav.in/

And the situation turns out to be:
1. We go to the site https://vm2888385.33ssd.had.wf/ - nginx opens folder for files.vladislav.in
2. Go tohttps://vladislav.in/ - everything is ok, opens the vladislav.in folder
3. Go to https://files.vladislav.in/ everything is ok opens your folder
4. Go to https://213.166.70.205/ opens the folder for files.vladislav.in

Why does it lead to a subdomain by default? And how to fix it.
Below I attach the configs:
/etc/nginx/conf.d/default.conf

spoiler

server {
    listen       80;
    server_name  _;

    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;
    }
}


/etc/nginx/sites-available/default
spoiler

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;


        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {

                try_files $uri $uri/ =404;
        }
}


/etc/nginx/sites-available/vladislav.in.conf
spoiler

server {
    charset utf-8;
    client_max_body_size 128M;

    listen vladislav.in:443 ssl;
    #listen [::]:80 default_server ipv6only=on; 

    ssl_certificate /etc/letsencrypt/live/vladislav.in/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vladislav.in/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/vladislav.in/chain.pem;


    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 127.0.0.1 8.8.8.8;



    server_name vladislav.in;
    root        /var/www/vladislav.in;
    index       index.html index.php;

    location / {

        try_files $uri $uri/ /index.php?$args;
    }


    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
    error_page 404 /404.html;

    location ~ \.php$ {
       include fastcgi.conf;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}


/etc/nginx/sites-available/files.vladislav.in.conf
spoiler

server {
    charset utf-8;
    client_max_body_size 128M;

    listen files.vladislav.in:443 ssl;
    #listen [::]:80 default_server ipv6only=on; 

    ssl_certificate /etc/letsencrypt/live/files.vladislav.in/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/files.vladislav.in/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/files.vladislav.in/chain.pem;


    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 127.0.0.1 8.8.8.8;



    server_name files.vladislav.in;
    root        /var/www/files.vladislav.in;
    index       index.html index.php;

    location / {

        try_files $uri $uri/ /index.php?$args;
    }


    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
    error_page 404 /404.html;

    location ~ \.php$ {
       include fastcgi.conf;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}



Symlinks to the site-enabled folder are created for the last three configs.
How to make a site from the /var/www/html folder open when opening an IP or domain from the hoster
Yes, and in principle, in any case, if the domain or subdomain does not have its own config, then the default page should open until I create it config. For ease of perception, I added index.html with the text Welcome to nginx
in the /var/www/html folder And in the /var/www/files.vladislav.in folder in the index.html file there folder files.vladislav.in

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheAndrey7, 2021-11-15
@cr1gger

How to make a site from the /var/www/html folder open when opening an IP or domain from a hoster

The default_server option to make it the default host. In /etc/nginx/sites-available/default you only listen on port 80, you don't have a default host for 443 (https).
listen 80 default_server;
listen [::]:80 default_server;

It is necessary to duplicate the ssl certificate for port 443 (it can be self-signed).
To prevent anything from opening on the default domain, you can write this:
location / {
    return 444; # Reset connection
}

listen files.vladislav.in:443 ssl;This is how you can't do it. Or we specify IP (if you have several of them) or we specify only the port. The server_name directive is responsible for the website domain .
Here's how to do it right:
listen 80; # http IPv4
listen [::]:80; # http IPv6
listen 443 ssl; # https IPv4
listen [::]:443 ssl; # https IPv6

You also need to listen to port 80, otherwise how to redirect visitors to https?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question