下载安装NGINX
# 下载NGINX:yum install nginx# 启动nginx:systemctl start nginx# 加入开机启动:systemctl enable nginx# 查看nginx的状态:systemctl status nginx
配置NGINX
下载后需要对NGINX进行配置,这里的配置文件是nginx.conf,可以使用命令 find / -name “nginx.conf” 进行查找,默认是在 /etc/nginx/nginx.conf,在 /etc/nginx目录下创建cert,用于存放下载好的ssl证书(pem文件和key文件)
#创建存放证书的目录,并用xftp工具讲ssl证书文件上传到该目录mkdir cert
之后对配置文件进行配置
# 打开nginx.conf配置文件vim /etc/nginx/nginx.conf
找到项目的配置文件之后点击编辑。在最后一行加入以下配置:
server {listen 443 ssl;server_name localhost; # localhost修改为您证书绑定的域名。ssl_certificate cert/xxxx.pem; #将xxxx.pem替换成您证书的文件名。ssl_certificate_key cert/xxxx.key; #将xxxx.key替换成您证书的密钥文件名。ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。ssl_prefer_server_ciphers on;location / {proxy_pass http://127.0.0.1:8080; #选填你的端口号.即你的springboot项目运行所使用的端口号proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $host;proxy_set_header Upgrade-Insecure-Requests 1;proxy_set_header X-Forwarded-Proto https;}}
wq保存配置,重启Nginx,访问https://申请证书的域名。即可
nginx -s reload
访问成功
若使用http://域名访问则会访问到nginx默认页面,若想访问到我们的springboot项目则需进行http相关配置
将默认规则替换为我们的新规则
将
server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
替换为
server{listen 80;#重定向跳转httpsreturn 301 https://chenstar.top$request_uri; #localhost修改为你的域名}
注意:注意server后面的括号是成对出现的
之后访问http://chenstar.top/域名也会跳转到https://chenstar.top/访问成功