Nginx 配置 SSL 初探

2014-11-27  本文已影响55人  天然傲娇

前阵子低价入了一个5年年限的 SSL 证书,域名恰好闲置中,于是自己也折腾了一下,去感受下 https 的魅力。

以下是我的配置:

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /path/to/ssl.crt;
    ssl_certificate_key /path/to/ssl.key;

    location / {
        root /path/to/root;
        index index.html;
    }
}

server {
    listen 80;
    server_name example.com www.example.com;

    if ($host = 'example.com') {
        rewrite (.*) https://example.com$1 permanent;
    }

    if ($host = 'www.example.com') {
        rewrite (.*) https://www.example.com$1 permanent;
    }
}

我使用了 if 语句让一级域名和二级域名能够分别跳转到各自的 https 地址。即访问 http://example.com 会跳转到 https://example.com;访问 http://www.example.com 会跳转到 https://www.example.com。有没有更好的方法呢?

不过还需要注意以下几点:

希望自己能够有时间、精力、恒心去做完整个站点。以上。

上一篇 下一篇

猜你喜欢

热点阅读