0

I have an Ubuntu machine on LightSail which serves static files with nginx.

I would like to a subdomain so I've added an A Recording pointing to the IP of the machine named client.mydomain.com.

My nginx site configuration files is defined as

server {
        listen 5000;
        server_name client.mydomain.com;

        location / {
                root /home/ubuntu/client-data;
        }
}

Although I can access the files in client-data via http://[ip]/file.txt http://client.mydomain.com/file.txt returns an 301 Permanent Moved error.

Why? Is it because I already have an nginx site running at 80 and 8080 ports. Why wouldn't the subdomain using the right site from 5000?

Paul
  • 295
  • 4
  • 10
  • Short said: you tell the Server to listen to cient.mydomain.com on Port 5000, you tell that you access ...com/file.txt - which leads to request it on port 80! so this means, that you request wrongly ...com:5000/file.txt would be the correct way. – djdomi May 17 '21 at 05:07

1 Answers1

0

By default, your client (Internet-Browser/Utility) will connect to your server using the default ports 80/443 when using HTTP or HTTPS.

Else, if you use, like in your case, i.e. http://client.mydomain.com:5000/file.txt, you will request that file as intended, as you configured here this server on port 5000 and the file will be served in case it exists.

If you already have a default server_name example like down below defined on port 80, Example:

server {
        #listen <PORT>
        listen 80;
        #server_name _invalid_name_for_default_server_
        server_name _default_;
        #location defines WHAT can/should be found WHERE
        location / {
                root /home/ubuntu/client-data;
        }
}

While accessing your server using a domain name, it will usually connect to port 80 and then look for the configured server block with server_name to verify if it exists, else, if configured it will serve the default Page.

Your Block explained:

server {
        #listen <PORT>
        listen 5000;
        #server_name domain_to.listen
        server_name client.mydomain.com;
        #location defines what can be found WHERE
        location / {
                root /home/ubuntu/client-data;
        }
}

In case, you want to request it with client1.mydomain.com/myfile.txt:

Set "listen" to port 80. If you then connect to your server, it will resolve the server_name directive. EG:

server {
        listen 80;
        server_name client1.mydomain.com;

        location / {
                 root /home/ubuntu/client-data;
        }
}

Additionally, if for whatever reason you want to like too, to listen for BOTH Ports:

server {
        listen 80;
        listen 5000;
        server_name client1.mydomain.com;

        location / {
                 root /home/ubuntu/client-data;
        }
}

Please also read: https://nginx.org/en/docs/http/server_names.html

We hope we could help you, if you wanted to raise the maximum number of Clients, listen is the wrong one :-)

djdomi
  • 1,377
  • 3
  • 10
  • 19
Maanloper
  • 73
  • 3