当前位置: 首页 > news >正文

【openresty服务器】:源码编译openresty支持ssl,增加service系统服务,开机启动,自己本地签名证书,配置https访问

1,openresty 源码安装,带ssl模块

https://openresty.org/cn/download.html

(1)PCRE库
PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。
(2)zlib库
zlib库用于对HTTP包的内容做gzip格式的压缩,如果我们在nginx.conf中配置了gzip on,并指定对于某些类型(content-type)的HTTP响应使用gzip来进行压缩以减少网络传输量,则在编译时就必须把zlib编译进Nginx。zlib-devel是二次开发所需要的库。
(3)OpenSSL库
如果服务器不只是要支持HTTP,还需要在更安全的SSL协议上传输HTTP,那么需要拥有OpenSSL。另外,如果我们想使用MD5、SHA1等散列函数,那么也需要安装它。

apt-get install -y libssl-dev libpcre3 libpcre3-dev zlib1g-devwget https://openresty.org/download/openresty-1.27.1.1.tar.gztar -zxvf openresty-1.27.1.1.tar.gz 
cd openresty-1.27.1.1/./configure 
gmake && gmake install
Configuration summary+ using system PCRE library+ using system OpenSSL library+ using system zlib librarycp conf/nginx.conf '/usr/local/openresty/nginx/conf/nginx.conf.default'
test -d '/usr/local/openresty/nginx/logs' \|| mkdir -p '/usr/local/openresty/nginx/logs'
test -d '/usr/local/openresty/nginx/logs' \|| mkdir -p '/usr/local/openresty/nginx/logs'
test -d '/usr/local/openresty/nginx/html' \|| cp -R docs/html '/usr/local/openresty/nginx'
test -d '/usr/local/openresty/nginx/logs' \|| mkdir -p '/usr/local/openresty/nginx/logs'
gmake[2]: Leaving directory '/data/openresty-1.27.1.1/build/nginx-1.27.1'
gmake[1]: Leaving directory '/data/openresty-1.27.1.1/build/nginx-1.27.1'
mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

2,增加openresty 没有service服务,开机启动

增加一个服务配置

vi /etc/systemd/system/openresty.service
[Unit]
Description=OpenResty nginx server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/openresty/bin/openresty -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/usr/local/openresty/bin/openresty -c /usr/local/openresty/nginx/conf/nginx.conf -s reload
ExecStop=/usr/local/openresty/bin/openresty -s stop
PrivateTmp=true[Install]
WantedBy=multi-user.target

然后重启服务:

# 配置变了,重新加载下
systemctl daemon-reload# 重启服务
systemctl restart openresty# 增加开机启动服务:
systemctl enable openresty

3,自签名证书,在OpenResty中配置SSL

OpenResty是一个基于Nginx的高性能Web平台,支持多种功能扩展。配置SSL可以使网站通过HTTPS协议进行加密传输,提升安全性。以下是如何在OpenResty中配置SSL的步骤。

生成服务器私钥和证书

首先,确保已经安装了OpenResty和OpenSSL。在OpenResty的配置目录下创建一个cert文件夹,用于存放证书和私钥。


mkdir -p /usr/local/openresty/nginx/conf/cert
cd /usr/local/openresty/nginx/conf/cert# 生成4096字节的服务器私钥:
openssl genrsa -des3 -out server.key 4096# 创建签名请求的证书(CSR):
openssl req -new -key server.key -out server.csr# 根据提示输入相关信息,例如国家、州、省、市、组织名称等。
# 去除私钥的口令保护:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key# 生成证书文件:
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

5,配置Nginx.conf

编辑Nginx的配置文件nginx.conf,添加SSL相关配置。

server {listen 443 ssl;server_name localhost;ssl_certificate /usr/local/openresty/nginx/conf/cert/server.crt;ssl_certificate_key /usr/local/openresty/nginx/conf/cert/server.key;ssl_session_cache shared:SSL:5m;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;location / {alias html/;index index.html index.htm;try_files $uri $uri/ /index.html;client_max_body_size 100M;
}
}

6,重启Nginx

保存配置文件后,重启Nginx以应用新的配置。

sudo systemctl restart openresty

验证HTTPS

在浏览器中访问配置的域名或IP地址,确保能够通过HTTPS协议访问网站。

将HTTP请求重定向到HTTPS

为了确保所有HTTP请求都重定向到HTTPS,可以在Nginx配置文件中添加以下配置:

server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
}

通过以上步骤,您可以在OpenResty中成功配置SSL,使网站支持HTTPS协议

在这里插入图片描述

7,可以访问https地址了

在这里插入图片描述

http://www.lryc.cn/news/535201.html

相关文章:

  • 如何将网站提交百度收录完整SEO教程
  • 【STM32】ADC|多通道ADC采集
  • 蓝桥杯算法日记|贪心、双指针
  • ArcGIS Pro SDK (二十七)自定义许可
  • 通过客户端Chatbox或OpenwebUI访问识别不到本地ollama中的模型等问题的解决
  • 速度超越DeepSeek!Le Chat 1100tok/s闪电回答,ChatGPT 4o和DeepSeek R1被秒杀?
  • JVM速成=。=
  • Packer 手动修复安装腾讯云插件
  • 学习总结三十
  • 开发完的小程序如何分包
  • Flutter PIP 插件 ---- Android
  • 【20250211】字符串:459.重复的子字符串
  • 【DeepSeek学Cuda】矩阵转置:行读取优先还是列读取优先。
  • 如何将3DMAX中的3D文件转换为AutoCAD中的2D图形?
  • Softhsm储存安全数据性能整理
  • 【C++】——精细化哈希表架构:理论与实践的综合分析
  • 【cocos creator】拖拽排序列表
  • b站——《【强化学习】一小时完全入门》学习笔记及代码(1-3 多臂老虎机)
  • 【Mac排错】ls: command not found 终端命令失效的解决办法
  • 探秘Hugging Face与DeepSeek:AI开源世界的闪耀双子星
  • SkyWalking 10.1.0 实战:从零构建全链路监控,解锁微服务性能优化新境界
  • 本地部署DeepSeek-R1(Mac版)
  • 网易易盾接入DeepSeek,数字内容安全“智”理能力全面升级
  • apachePoi中XSSFClientAnchor图片坐标简述;填充多张图片
  • Java、Go、Rust、Node.js 的内存占比及优缺点分析
  • C++智能指针的使用
  • 计算机毕业设计——Springboot的社区维修平台旅游管理
  • MySQL ALTER 命令详解
  • 02、QLExpress从入门到放弃,相关API和文档
  • Mp4视频播放机无法播放视频-批量修改视频分辨率(帧宽、帧高)