nginx配置https

发布时间:2020/05/03 作者:天马行空 阅读(860)

1、申请ssl证书
首先需要申请ssl证书,ssl证书是收费的,当然对于个人而言用免费版的就好了,免费版可在:https://freessl.cn/申请。具体申请步骤此处略过,自行研究。

2、下载证书

下载并上传你申请的证书到服务器,将证书放到nginx.conf同目录下

3、配置nginx

在配置文件中修改server,将http跳转到https,并设置https的证书

server {
        listen  80;
        server_name xxx.com www.xxx.com;
        rewrite ^(.*)$  https://$host$1 permanent;
}
server {
        #listen  80;
        listen 443;
        server_name  xxx.com  www.xxx.com;
        #开启https,并设置证书位置
        ssl on;
        ssl_certificate /home/wwwroot/nginx/conf/full_chain.pem;
        ssl_certificate_key /home/wwwroot/nginx/conf/private.key;
        access_log  /home/wwwroot/nginx/logs/xxx.log;
        location / {
             root   /home/wwwroot/www/xxx/;
             index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           /home/wwwroot/www/xxx/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

注意一旦使用https就要求全站都使用https,不然你就会看到浏览器地址栏有黄色的警告图标。

图片.png

关键字centos nginx https