1

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?

1

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/

| improve this answer | |
  • 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 – Govind Wadhwa Jun 19 '19 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.