Nginx 配置https以及wss

一、申请https证书

可以在阿里云申请免费ssl证书,一年更换一次

二、Nginx配置ssl

shell 复制代码
upstream tomcat_web{
     server 127.0.0.1:8080;
}

server {
    listen  443 ssl;
    server_name  www.xxx.com;
	## 配置日志文件
    access_log  /var/log/nginx/web/xxx-ssl-access.log  main;
    error_log  /var/log/nginx/web/xxx-ssl-err.log;
    ## 配置证书所在目录
    ssl_certificate   sslkey/www.xxx.com.pem;
    ssl_certificate_key sslkey/www.xxx.com.key;
    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;
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    ## 重要,否则应用redirect的时候,会跳转到http,这里强制替换成https
    proxy_redirect http:// $scheme://;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        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    X-Forwarded-Proto  $scheme;
        proxy_pass      http://tomcat_web;
        proxy_http_version 1.1;
        ## 以下两个配置如果没配置,websocket会报错,链接时使用wss://
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

		## 配置上传文件大小 300Mb
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        ## 配置链接超时时间 10分钟
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers   4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
    }

}

注意:

1、配置完成后nginx需要重启,reload证书是不会生效的

2、如需要支持wss协议,需要增加配置:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

3、配置https之后,最后就禁用掉http,通过如下配置,强制跳转https
proxy_redirect http:// $scheme://;

相关推荐
Android系统攻城狮6 分钟前
Linux Gstreamer深度解析之gst_audio_format_build_integer调用流程与实战(十二)
linux·运维·服务器·音视频·车载音视频·gstreamer进阶
天远API9 分钟前
零信任架构实战:基于天远行驶OCR证识别构建自动化智能停车网关
运维·人工智能·架构·自动化
吴声子夜歌35 分钟前
Shell脚本——流程控制:for循环
linux·运维·shell
可乐鸡翅yeah_1 小时前
MPEG‑TS 分片 PTS/DTS 时间戳异常排错,HLS 音画不同步定位实战
运维·测试用例·音视频·媒体·m3u8
SkyWalking中文站2 小时前
Horizon UI 1.0 正式发布:SkyWalking 新一代控制台接棒 Booster
运维·监控·自动化运维
随便做点啥2 小时前
32卡64G-910B4-16后端集群部署报告
java·大数据·运维
我的小卷呀2 小时前
Linux ip 命令底层逻辑与实战指南
linux·运维·c语言·网络协议·tcp/ip·网络安全
CingSyuan3 小时前
4U GPU服务器整机无法上电故障排查实录:通过交叉替换、逐项回装与跨槽位测试,定位RTX 4080显卡故障及硬盘槽位异常并完成临时恢复
运维·服务器
handler013 小时前
【Linux】ELF、链接与动态库,程序装载全解析
linux·运维·服务器·chrome·c·elf·文件
考虑考虑3 小时前
elasticSearch中的element_type
运维·后端·elasticsearch