Access website only through ip with nginx

June 25, 2015 6.4k views
Nginx LEMP Ubuntu

I'm new to DigitalOcean. I just configured my first droplet installing Ubuntu and a LEMP stack. At the moment I don't have a domain to use and wanted just to do some tests. I followed this guide to create a Server Block:

I changed the root line to this:

root /var/www/;

and the server_name to my ip.

Nginx is working, but if I try to access the website using ip_address/ it doesn't work.

What can it be? How can I debut the issue?


4 Answers


I just came accross an issue. Doesn't matter if its a local setup or one with a domain name.

When you create a symbolic frpom sites-available to sites-enabled you have to use the whole path to each location.

e.g. you can't

cd /etc/nginx/sites-available/
ln -s monitor ../sites-enabled/

It has to be:

ln -s  /etc/nginx/sites-available/monitor  /etc/nginx/sites-enabled/
  • Thanks for your answer. It's been a while now and I don't remember how I eventually fixed the problem but it's very likely that this was the issue.

    I remember having a similar problem and it was caused by the wrong syntax in the ln command. Thanks again!

Inside /etc/nginx/sites-available you should have just edited the default file to change the root web folder you specified and left the server name part alone. Restart nginx, should work fine. You don't need to specify the IP of your droplet. That's the whole purpose of the default file.

You only need to copy the default file and change server names when you want to set up virtual hosts.

Also "access the website using ip_address/ it doesn't work"

The root folder specified in the default server block defines the starting folder pages will be served. So if your IP is visited pages inside html/ will be served such as index.html.

If you want it to work like you said ip_addr/ you need to make a folder inside html/ called and place your index.html inside that folder.

Good luck

  • Well, I didn't specify this, but I removed "default" from sites-enabled and I added Inside this file I specified root as /var/www/ and the server name to the ip address. Isn't this also correct?

The Server Blocks are for setting up virtual hosts. This will allow nginx to serve different web pages for different domain names that all point to the same server allowing multiple domain names and web pages to be served from the same single droplet.

You don't have multiple domain names or even one domain name, so you had no need to go and change the server_name inside your server block you should have left it alone.

Because you don't have a domain name your only way of accessing the web pages on your server through a browser on the internet is by entering your servers IP into the address bar. The default server block is what handles these requests.

The default server block means the default web site that will be served when a HTTP request is made to the server by a user that doesn't specify any other pre-defined vhosts/domain names that exists within your server blocks.

So the default server block will handle and process requests made to the server that are made by entering a raw IP address just like you have to because you don't have any domain names.

You can't use as your server name because it isn't a registered domain name and has no public DNS records pointing to your server. You're not going to be able to enter into your address bar in a browser and expect your server to serve it because there's no DNS for it and it doesn't exist on the public internet.

You COULD access your website through (which is how you have configured it currently) ONLY if you was browsing locally from within your servers network, you won't be able to access that from outside your servers network.

You should have just left your default server block as it was and changed the document root to /var/www/ and then you could access your web site through your servers IP.

You said you wanted to access it like this http://ip_address/ in which case you then need to enter /var/www/ from your shell and mkdir and place your web pages inside that folder.

I think you should go back to the basics and have a read up on basic networking as this is very basic stuff. I would suggest going to google and have a read up on the difference between the "intranet and the Internet" this should give you an overview and basic understanding of what I am talking about.

Best of luck

Well, a simple fix would be to use the following code in the configuration:

server {
server_name localhost;
location / {
root /var/www/;

The code above will serve all requests who don't have a specific block.

Have another answer? Share your knowledge.