Score:0

My website is available if I type the hostname ip in the broswer, How to fix

cy flag

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;
  }
Jaromanda X avatar
ru flag
what does `the hostname ip` mean? hostname OR ip? do you have DNS setup to point the hostname to that ip?
in flag
Does this answer your question? [Using Nginx to Block Connections that aren't addressed to my domain](https://serverfault.com/questions/1108830/using-nginx-to-block-connections-that-arent-addressed-to-my-domain/1108834#1108834)
Crypto Coupons avatar
cy flag
Jaromanda X the ip
Crypto Coupons avatar
cy flag
@Gerald Schneider nop
Score:0
vn flag

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/

Crypto Coupons avatar
cy flag
do I need to make one for the 443 port to ?
Lex Li avatar
vn flag
@CryptoCoupons I suggest you first verify the logic using pure HTTP. Once that meets your requirements, adding SSL/443 and further tuning is rather simple.
Crypto Coupons avatar
cy flag
can you please check if I changed correctly in my nginx config ? I updated the code in the question.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.