nginx linux 上安装 -转发http 为https请求
一、nginx安装
1、上传到指定的服务器地址 、opt/nginx
2、解压
tar -zxvf nginx-1.24.0.tar.gz
3、进行configure
进入Nginx的根目录,执行命令:
./configure 或:./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
假如进行configure时报错 yum -y install pcre-devel yum -y install openssl openssl-devel 再进行configure应该不会报错了。
如果要使用 https,则安装https模块:
执行:
./configure --with-http_ssl_module
如果报错,找不到openssl,则下载 openssl 安装包,到制定目录,这里放到了 /opt/nginx 下面,解压,tar -zxvf openssl-3.3.0.tar.gz
执行以下命令:
./configure --with-http_ssl_module --with-openssl=/opt/nginx/openssl-3.3.0
4. 执行编译命令
make
5.执行安装命令
make install
找到安装目录:
/usr/local/nginx
查看nginx版本信息:
#进入可执行目录sbin cd /usr/local/nginx/sbin/ #查看nginx版本信息 ./nginx -v #查看nginx版本信息、编译版本、配置参数 大写字母V ./nginx -V
进入可执行目录sbin
cd /usr/local/nginx/sbin/
6. 启动nginx
#等同于/usr/local/nginx/sbin/nginx,也可使用这个命令/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx
# 停止命令,优雅的停止(不接受新的连接请求,等待旧的连接请求处理完毕再关闭) 配合./nginx命令也可实现重启 ./nginx -s stop # 也是停止命令,快速关闭 配合./nginx命令也可实现重启 ./nginx -s quit # 重启命令,重新加载配置文件 ./nginx -s reload
7. 开机启动配置:
#给予执行权限 chmod +x /etc/rc.d/rc.local #打开文件/etc/rc.local超链接指向了/etc/rc.d/rc.local vi /etc/rc.d/rc.local #输入i,光标移动到最后一行上,添加下面的命令(nginx启动命令) /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8、报错:
403 没有权限:
配置文件修改,和启动的用户对应起来:
二. mkcert 生成 ssl
下载 mkcert 包:
对应的目录下输入:
mkcert-v1.4.4-windows-amd64.exe -install
弹出选择是
查看命令:
mkcert-v1.4.4-windows-amd64.exe --help
查看证书位置:
mkcert-v1.4.4-windows-amd64.exe -CAROOT
生成局域网自签证书:
mkcert-v1.4.4-windows-amd64.exe localhost 127.0.0.1 ::1 192.168.1.1
# 这个192.168.1.1是我的电脑的地址,这里根据根据自己的电脑ip进行设定
根据自己的需求填写ip可以有多个ip。(如上所示,就使用localhost、127.0.0.1、::1、192.168.1.1)这四个地址,生成对应的证书,也就是这个证书中,是根据这个四个地址生成的。
在IP地址中,::1 是一个特殊的IPv6地址,被称为回环地址(loopback address)。在IPv4中,回环地址是 127.0.0.1。IPv6的回环地址 ::1 与IPv4的 127.0.0.1 在功能上是相似的。
这种地址在本地开发、测试网络服务和应用时非常有用,因为它确保通信不会离开本地机器,从而避免了可能存在的网络延迟或外部干扰。
将 localhost+3.pem 改名为
双击查看信息
三、nginx https的配置:
将 ssl 私钥和公钥 放到 conf 目录下面配置为
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; client_header_buffer_size 4k; open_file_cache max=102400 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 1; server_tokens off; #gzip on; server { listen 9900 ssl; server_name localhost; #强制ssl #ssl on; ssl_certificate wfpt.pem; ssl_certificate_key wfpt-key.pem; #缓存有效期 ssl_session_timeout 5m; #安全链接可选的加密协议 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #加密算法 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用服务器端的首选算法 ssl_prefer_server_ciphers on; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://127.0.0.1:9700/; } } }
四、证书的转换
jks 转换为 keystore
keytool -importkeystore -srcstoretype JKS -srckeystore wfqmjkpt.com.jks -deststoretype PKCS12 -destkeystore wfqmjkpt.com.keystore
五、转换工具
https://tool.cccyun.cc/cert_convert
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)