###一、配置服务器正常开启HTTP及HTTPS
firewall http 服务开启
firewall-cmd --query-service http ##查看http服务是否支持,返回yes或者no
firewall-cmd --add-service=http ##临时开放http服务
firewall-cmd --add-service=http --permanent ##永久开放http服务
firewall-cmd --reload ##重启防火墙生效
firewall https 服务开启
与上面的 http 类似,话不多说,直接上代码
firewall-cmd --add-service=https --permanent
firewall-cmd --add-service=https --reload
###二、配置SSL
####1:证书申请
域名申请完后,在对应服务商的SSL板块申请证书,申请完下载证书,下载的文件夹下有Apache、Nginx、Tomcat等文件夹,选择自己选用的web服务器软件名称的文件,如Nginx。里面有.pem形式的公钥和.key形式的私钥。
####2、上传证书
在目录/etc/nginx/下新建文件夹cert,将.pem形式的公钥和.key形式的私钥文件放入cert文件夹下。
####3、安装OpenSSL
yum install -y openssl openssl-devel
####4、配置Nginx
进入nginx配置目录/etc/nginx/conf.d,打开default.conf文件,添加HTTP强制转HTTPS功能和在443端口下配置SSL,注意:server与server是平级关系。配置文件如下:
server {
listen 80;
server_name xifajiaoyu.com; # 域名
rewrite ^(.*) https://$host$1 permanent; #HTTP强转HTTPS
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; # 网站主目录路径
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl;
server_name www.xifajiaoyu.com ;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate ssl/1_xifajiaoyu.com_bundle.crt; # 公钥目录
ssl_certificate_key ssl/2_xifajiaoyu.com.key; # 私钥目录
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
或者下面自己亲测
server {
listen 80;
listen 443 ssl;
server_name ***.mglucky.com;
ssl on;
#rewrite ^(.*)$ https://$server_name$1 permanent; # HTTP强转HTTPS
#return 301 https://$server_name$request_uri; #这是nginx最新支持的写法
if ($http_x_forwarded_proto = "http") {
return 301 https://$server_name$request_uri; #http强制转https
}
#charset koi8-r;
ssl_prefer_server_ciphers on;
#access_log logs/host.access.log main;
ssl_session_timeout 5m;
location / {
root /usr/share/nginx/html/niushop;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?s=$uri&$args;
}
ssl_certificate /etc/nginx/conf.d/cert/4354962_shop.xianyunshop.com.pem; # 公钥目录
ssl_certificate_key /etc/nginx/conf.d/cert/4354962_shop.xianyunshop.com.key; # 私钥目录
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/niushop;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html/niushop;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
####5、打开防火墙443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
sudo firewall-cmd --reload
####6:重新加载Nginx
重新加载,因为一般重新配置之后,不希望重启服务,这时可以使用重新加载。
sudo systemctl reload nginx
至此完成SSL配置。