前置条件
- 服务器已安装nginx,并且通过http能够访问通,如若不会可访问(linux下 nginx 安装)
- 已经申请了https的相关证书
第一步 配置nginx
在配置ssl证书之前,要确保你的nginx已经安装了ssl模块,一般情况下自己安装的nginx都是不存在ssl模块的。
这里先检查下自己是否存在ssl模块:
进入到你的nginx安装目录下面,我的目录是在( /usr/local/nginx),如果你的nginx安装步骤和上面的文章一致的话,那你的目录和我应该是一致的
进入到目录的sbin目录下,输入
./ nginx -V
如果出现 (configure arguments: --with-http_ssl_module), 则已安装(下面的步骤可以跳过,直接进行第五步)
一般情况下都是不存在ssl模块的,接下来进入到你的解压缩后的nginx目录,注意这里不是nginx安装目录,是解压缩后的目录,我的是在(/root/nginx),进入目录后,输入
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
注意
在执行这一步的时候可能会出现 提示安装openssl,但是系统里面已经安装openssl了,我们就需要离线下载openssl包执行以下命令
openssl 离线包 链接: pan.baidu.com/s/144PHWIY4… 提取码: 4h2p 复制这段内容后打开百度网盘手机App,操作更方便哦
拖入服务器解压(tar -xvf xxx)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/nginx/openssl-1.0.2h
接下来执行
make
#切记不要执行make install,否则会重新安装nginx
上述操作执行完成以后,你的目录下会出现objs文件夹,文件夹内存在nginx文件,如图:
接下来使用新的nginx文件替换掉之前安装目录sbin下的nginx,注意这里的替换的时候可以先将之前的文件备份下,停掉nginx服务
---注意---
-
一
此时我们运行nginx 命令可能会遇到提示 在 /usr/local/nginx/logs/ 路径下找不到 nginx.pid 的一个文件,我们需要手动创建该文件,然后执行命令,没问题跳过说明二
-
二
如果完成一步后还是不行 运行以下命令
./nginx -c /usr/local/nginx/conf/nginx.conf
./nginx -s stop #停止nginx服务
#替换之前的nginx
cp /root/nginx/objs/nginx /usr/local/nginx/sbin
成功之后,进入到nginx安装目录下,查看ssl时候成功
#注意这里是大写的V,小写的只显示版本号
./nginx -V
#可以看到这里出现了configure arguments: --with-http_ssl_module 证明已经安装成功
提示:这里替换后在执行 -V命令如果提示权限不足,先给这个nginx文件提升下权限
chmod 111 nginx
第二步 配置ssl证书
解压缩下载好的证书(证书一般是pem文件和key文件,这里名字可以随便改)
将下载好的证书上上传到服务器,我将证书放在了root目录下的card文件夹
#在root目录下创建card文件夹
cd /root
mkdir card
第三步 进行nginx.conf配置
进入nginx.conf文件下
cd /usr/locla/nginx/conf
#修改nginx.conf文件
vim nginx.conf
文件如下(我在项目中的配置):
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
#你的域名
server_name xxx.xxx;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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 html;
}
# 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 html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
#你的域名
server_name xxx.xxx;
root html;
index index.html index.htm;
#https 证书路径位置
ssl_certificate /xx/xx.pem;
ssl_certificate_key /xx/xx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://公网地址:项目端口号;;
}
}
server {
# rewrite ^/(.*)$ https://域名:443/$1 permanent;
rewrite ^/(.*)$ https://$host$1 permanent;
}
}
注意:这里需要在安全组中开放443端口
第四步 重启nginx
进入sbin目录下,输入
./nginx -s reload
./nginx -s stop
./nginx
提示:如若无反应需要配置 hosts 映射
客户端访问,无提示信息的话,就大功告成了。