【lnmp环境搭建】LNMP下Nginx SSL配置的方法详解

更新时间:2020-05-14    来源:php安装    手机版     字体:

【www.bbyears.com--php安装】

在前面的几篇文章里介绍了免费SSL证书Let’s Encrypt安装使用教程以及debian下lnmp一键安装脚本完善版。虽然脚本中提供了搭建HTTPS虚拟主机的命令,tennfy还是在这篇文章里详细介绍下。

配置文件示例

首先找到虚拟主机的nginx配置文件,将该文件修改为:

server {
 listen 80;
 server_name 111cn.net www.111cn.net; #替换为自己的域名
 rewrite ^/(.*) https://$server_name/$1 permanent;
}
server {
 listen 443 ssl;
 root /var/www/111cn.net;              #替换为虚拟主机目录
        index index.php index.html index.htm;
        server_name 111cn.net www.111cn.net; #替换为自己的域名
 
 location / {
  include rewrite.conf;          #替换为伪静态规则
        }
 
 location ~ \.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 }
 
 ssl on;
 ssl_certificate tennfy_certificate;    #替换为SSL证书
 ssl_certificate_key tennfy_privatekey; #替换为SSL证书密钥
 ssl_session_timeout 10m;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
 ssl_session_cache builtin:1000 shared:SSL:10m;
}
其中第一个server{}的作用是监听80端口,将http访问重定向到https访问。

第二个个server{}的作用是监听443端口,响应https访问,并解析php文件。

在使用的时候替换相应的地址及域名即可。

最后记得重启nginx

/etc/init.d/nginx restart

小结

配置很简单,不过却是很实用的技巧。

本文来源:http://www.bbyears.com/jiaocheng/93465.html

热门标签

更多>>

本类排行