nginx https的配置方法

文章目录

安装证书工具

curl 'http://pan.itshine.cn:5080/?explorer/share/fileOut\&shareID=64h6PiQQ\&path={shareItemLink%3A64h6PiQQ}%2F工具%2Fmkcert' > './mkcert'

chomd +x mkcert && cp mkcert /bin

安装根证书

mkcert -install

生成域名证书

mkcert 192.168.10.174

后面的IP地址必须是自己的IP地址或者是你的机器的nginx 一个虚拟主机server一个域名。

会有二个文件生产

  • 192.168.10.174.pem 包含公钥的证书,经过自签发了。
  • 192.168.10.174.key.pem 私钥

配置

复制代码
server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  _;
        root         /usr/share/nginx/html;
# 公钥证书,改成你自己的证书绝对路径
        ssl_certificate "/etc/nginx/ssl/192.168.10.42.pem";
# 私钥 ,改成自己绝对路径
        ssl_certificate_key "/etc/nginx/ssl/192.168.10.42-key.pem";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}

重新启动访问

https://your-ip/

转发 ssl的请求到http请求

复制代码
location /
{
# 改成你要代理到的http服务器,
    proxy_pass http://127.0.0.1:8087/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
   # proxy_set_header Connection $connection_upgrade;
    proxy_set_header   X-Forwarded-Proto https;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    set $static_fileJKcauNzk 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_fileJKcauNzk 1;
  expires 1m;
    }
    if ( $static_fileJKcauNzk = 0 )
    {
        add_header Cache-Control no-cache;
    }
} 

将上述文字复制到 刚才虚拟主机当中。

相关推荐
一只鹿鹿鹿5 小时前
信息安全等级保护安全建设防护解决方案(总体资料)
运维·开发语言·数据库·面试·职场和发展
房产中介行业研习社5 小时前
2026年3月哪些房源管理系统功能全
大数据·运维·人工智能
wenlonglanying6 小时前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
biubiubiu07066 小时前
Linux / Ubuntu systemd 服务使用说明
linux·运维·ubuntu
MaximusCoder6 小时前
等保测评命令——Anolis Linux
linux·运维·服务器·网络·经验分享·安全·php
田里的水稻6 小时前
ubuntu22.04_构建openclaw开发框架
运维·人工智能·python
相思难忘成疾7 小时前
《RHEL9虚拟机部署及SSH远程登录实践手册》
linux·运维·ssh·虚拟机
cg_ssh7 小时前
Vue3中样式变量的使用
linux·运维·服务器
炸炸鱼.7 小时前
Nginx 代理与缓存实战:正向、反向及网络层级详解
网络·nginx·缓存
TDengine (老段)8 小时前
TDengine IDMP 运维指南 —— 部署架构
大数据·运维·数据库·架构·时序数据库·tdengine·涛思数据