如果我在浏览器中输入主机名 ip,我的网站就可以访问,如何修复

如果我在浏览器中输入主机名 ip,我的网站就可以访问,如何修复

如果我在浏览器中输入主机名 ip,我的网站就可以访问,如何修复此问题并将其重定向到我的网站或该怎么办?

我设法在以前类似的答案中找到一些信息

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.

但我希望它重定向到主域名 www 版本而不是给出 404

我不能得到类似的东西或者类似的东西吗?

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

如果是的话,怎么办?

更新 1

就像下面的答案所暗示的那样,我在 nginx 配置中找到了 server_name_;(当前配置

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;
  }

我必须用这个来替换它,正确吗?

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

如果您只希望允许您的网站通过域名访问domain1.com,而不是通过xx.xx.xx.xxIP 地址访问,请添加一个server带有 的块server_name

然后,您可以将 catch-allserver块更改为重定向。

为简单起见,以下示例仅显示 HTTP。

  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;
  }

参考

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

相关内容