在Nginx下配置SSL证书

来源:互联网 发布:网络流量监视软件 编辑:程序博客网 时间:2024/06/06 01:02

  首先通过SFTP链接你的网站服务器,然后找到 /usr/local/nginx/conf/vhost/域名.conf 文件并下载到本地(以 hello.cn.conf 为例)。

然后我们本地打开 hello.cn.conf 文件并修改,下面是修改后的完整配置文件示例:

(注意:只需要看18行以前就行了,后面的不要跟着示例走,以前是什么就是什么,不需要和示例一模一样。)

server    {        listen 80;        listen 443 ssl http2;        ssl on;        ssl_certificate /root/hello.cn.crt;        ssl_certificate_key /root/hello.cn.key;        ssl_session_cache shared:SSL:10m;        ssl_session_timeout  10m;        add_header Strict-Transport-Security "max-age=31536000";        server_name hello.cn;        index index.html index.htm index.php default.html default.htm default.php;        root  /home/wwwroot/hello.cn;        if ( $scheme = http ){            return 301 https://$server_name$request_uri;        }# 只需要看前面的代码就行了,后面的不需要和示例一致!!!        include none.conf;        #error_page   404   /404.html;        location ~ [^/]\.php(/|$)        {            # comment try_files $uri =404; to enable pathinfo            try_files $uri =404;            fastcgi_pass  unix:/tmp/php-cgi.sock;            fastcgi_index index.php;            include fastcgi.conf;            #include pathinfo.conf;        }        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$        {            expires      30d;        }        location ~ .*\.(js|css)?$        {            expires      12h;        }        access_log off;    }

修改并保存后,上传 hello.cn.conf/usr/local/nginx/conf/vhost 文件夹并覆盖原文件(注意备份)。

然后再把 hello.cn.crt、hello.cn.key 上传到 hello.cn.conf 文件中指定的地方,比如示例中的 /root 文件夹下。

然后SSH链接网址服务器并重启Nginx

如果你不需要访客强制访问 HTTPS ,那么不需要添加这段代码。

if ( $scheme = http ){            return 301 https://$server_name$request_uri;        }
0 0
原创粉丝点击