Home  >  Q&A  >  body text

ssl - How to properly configure Nginx to enable HTTPS-enabled websites to prohibit direct access using the server IP address

The following is the official configuration of nginx:

server {
    listen      80;
    server_name "";
    return      444;
}

https://nginx.org/en/docs/htt...
But it seems that it only supports http. If https is used, the domain name will be used regardless of where it is configured and whether it is configured default_server Access and IP direct access 444.

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    if ( $host = $server_addr ) {
        return 444;
    }
    # ...
}

https://paste.ubuntu.com/2340...

This is how I configure it now. http request 301 jumps to https, and then use if to detect if the request is made using the server IP address, it will be 444. But this is not a good configuration (https://www.nginx.com/resourc..., is there a better configuration practice?

淡淡烟草味淡淡烟草味2714 days ago438

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 17:12:23

    server {

    listen 80 default;  
    rewrite ^(.*) https://域名;  

    }

    reply
    0
  • Cancelreply