禁止用 IP 直接访问

Nginx 1.19.4 及之后的版本

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 default_server;
    listen [::]:443 default_server;
    ssl_reject_handshake on;

    server_name _;
    return 444;
}

Nginx 1.19.4 之前的版本

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 ssl default_server;
    listen [::]:443 default_server;
    ssl_certificate /path/to/ssl.cert;
    ssl_certificate_key /path/to/ssl.key;

    server_name _;
    return 444;
}
强制 www 和 https

http://example.comhttp://www.example.com 301 重定向到 https://www.example.com

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

https://example.com 重定向到 https://www.example.com

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;
    ssl_certificate /path/to/ssl.cert;
    ssl_certificate_key /path/to/ssl.key;
    return 301 https://www.example.com$request_uri;
}

https://www.example.com 的配置

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    root /var/www/html/www.example.com;
    ssl_certificate /path/to/ssl.cert;
    ssl_certificate_key /path/to/ssl.key;
}
分类: Nginx

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注

CAPTCHA ImageChange Image