Question

Unable to access server by IP, but can access by domain name.

Posted June 6, 2020 276 views
Nginx

I am on Ubuntu with Nginx.

When I try to access the server by IP, I get a 404 error.

My server configuration looks like this:

server {
        listen 443 ssl;

        root /var/www/wordpress;

        server_name domain.com;


        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log_not_found off;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

}


server {
#    listen 443 ssl;
     server_name domain.com www.domain.com;

# location /xmlrpc.php {
#      deny all;
#    }


#    rewrite ^(.*[^/])$ $1/ permanent;
    return 301 https://domain.com$request_uri;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @ozwiz,

What you could do in this case is add your Droplet’s IP address to your server_name list. So it would look like this:

        server_name domain.com your_server_ip_address;

However what I could suggest is rather than doing this, it would be better to create a redirect so that if anyone visits the IP address of your Droplet they would get redirected to your domain name instead. I believe that this would be better in terms of SEO.

Hope that this helps!
Regards,
Bobby

Submit an Answer