Only allow access to nginx server when using the full domain name and not the IP

10,922

You can create two virtual hosts in Nginx. One for default access and another for domain specific.

Then you can restrict access for default access or redirect to your domain as per your choice.

server {
    listen      80 default_server;
    listen      [::]:80 default_server;
    server_name "";
    return      444;  # or comment this and uncomment below to redirect to domain.
    #return 301 http://www.example.com$request_uri;
}
server {
    listen      80 default_server;
    listen      [::]:80 default_server;
    server_name "www.example.com";
    ...
    ...
}

Nginx Virtual Hosts - https://tecadmin.net/setup-nginx-virtual-hosts-on-ubuntu/

Share:
10,922

Related videos on Youtube

Govind Wadhwa
Author by

Govind Wadhwa

exploring JS World. and Currently working Here from Rajasthan wfh.

Updated on September 18, 2022

Comments

  • Govind Wadhwa
    Govind Wadhwa almost 2 years

    I have an nginx server serving http://example.com which resolves to the IP 1.2.3.4. At the moment, users can connect either by going to http://example.com or by visiting http://1.2.3.4.

    I only want to allow access when using the full domain name, http://exmple.com. How can I configure my nginx to block access via the IP and only allow access when using the full domain name?

  • Govind Wadhwa
    Govind Wadhwa about 5 years
    Hey Rahul i have followed many of your tutorial from techadmin.net . i am new to ubuntu, where should i change means in /etc/nginx here in which file i have to make this changes