0

My website is available if I type the hostname ip in the broswer How to fix this and redirect it to my website or what to do?

I manage to found some info in previous similiar answers

server block with default_server in the listen directive. This block should only have return 404; or return 444;. You might want to turn off access_log in this block too.

server block with server_name example.com *.example.com;. This virtual host should contain your actual application.

but I want it to redirect to main domain the www version not give out a 404

can't I get something like that or similar ?

server {
    if ($host = vgopromo.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

and if yes how ?

UPDATE 1

Like the answer bellow suggests I found the server_name_; in nginx config (current config)

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

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

        server_name _;
        location / {
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php;
        }

error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }

this I have to replace with this correct ?

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

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

        server_name _;
    access_log logs/default.access.log main; ## this necessary?

    server_name_in_redirect off;

    return 301 http://www.domain1.com$request_uri;
        }

error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }

1 Answers1

0

If you just want to allow your site to be accessible via domain name like domain1.com, but not xx.xx.xx.xx IP address, add a server block with server_name.

Then you can change the catch-all server block to redirect.

The following example only shows HTTP for simplicity.

  server {
    server_name www.domain1.com;
    access_log logs/domain1.access.log main;

    root /var/www/domain1.com/htdocs;
  }

  server {
    listen 80 default_server;
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    access_log logs/default.access.log main;

    server_name_in_redirect off;

    return 301 http://www.domain1.com$request_uri;
  }

Reference

https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

Lex Li
  • 1,235
  • 8
  • 10