• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Nginx内网环境开启https双协议的实现

    发布者: 皮3591 | 发布时间: 2025-8-16 14:51| 查看数: 37| 评论数: 0|帖子模式

    前言

    nginx开启https前提:

    • 服务器支持open-ssl
    • nginx 包含
      1. --with-http_ssl_module --with-stream --with-stream_ssl_preread_module
      复制代码
      模块

    一、open-ssl


    1. 验证
    1. openssl version
    复制代码
    2. 安装

    1. mkdir /usr/local/ssl
    2. cd /usr/local/ssl
    3. # 解压
    4. tar -xf openssl-3.0.1.tar.gz
    5. # 设置SSL库文件路径
    6. ./config --prefix=/usr/local/ssl/
    7. make
    8. make install
    复制代码
    1. vi /etc/ld.so.conf
    2. # 最后一行添加/usr/local/ssl/ 路径
    3. sudo ldconfig
    复制代码
    常见报错:
    1. openssl: error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
    复制代码
    系统版本和openssl版本不一致,具体哪里的日志记录需要的版本忘记了

    3.生成ssl证书
    1. # 第一步:生成私钥
    2. mkdir /etc/ssl/certs/www.abc.com
    3. cd /etc/ssl/certs/www.abc.com
    4. openssl genrsa -des3 -out server.key 2048
    5. # 输入一个4位以上的密码
    6. # 确认密码
    7. #第二步:生成CSR(证书签名请求)
    8. openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=JiLin/L=ChangChun/O=commany/OU=commany/CN=www.abc.com"
    9. #第三步:去除私钥中的密码
    10. #在第1步创建私钥的过程中,由于必须要指定一个密码。而这个密码会带来一个副作用,那就是在每次启动Web服务器时,都会要求输入密码
    11. #这显然非常不方便。要删除私钥中的密码,操作如下:
    12. openssl rsa -in server.key -out server.key
    13. #第四步:生成自签名SSL证书
    14. # -days 证书有效期-天
    15. openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
    复制代码
    一、nginx


    1. 验证支持模块
    1. nginx -V
    复制代码
    2. 安装必要模块

    可以参考我之前的博客 Nginx 平滑升级
    2.1 重新编译nginx
    1. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-http_gzip_static_module --with-stream --with-stream_ssl_preread_module
    复制代码
    生成nginx二进制执行文件到当前目录 /objs
    1. make
    复制代码
    2.2 替换原文件
    替换
    1. mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    2. cp /usr/local/nginx-1.13.3/objs/nginx /usr/local/nginx/sbin/
    复制代码
    验证
    1. [root@web nginx-1.21.5]# make upgrade
    2. /usr/local/nginx/sbin/nginx -t
    3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    5. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
    6. sleep 1
    7. test -f /usr/local/nginx/logs/nginx.pid.oldbin
    8. kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
    复制代码
    升级
    1. #验证模块是否加载成功
    2. nginx -V
    复制代码
    3. 配置https

    下面是一段双协议支持的配置代码
    请允许我抄袭一下小左同学的代码
    1. stream {
    2.     upstream http_protocol {
    3.         # 8991端口是一个开启http的端口
    4.         server 127.0.0.1:8991;
    5.     }
    6.     upstream https_protocol {
    7.         # 10002端口是一个开启https的端口
    8.         server 127.0.0.1:10002;
    9.     }
    10.     # 根据不同的协议走不同的upstream
    11.     map $ssl_preread_protocol $upstream {
    12.         default http_protocol;
    13.         "TLSv1.0" https_protocol;
    14.         "TLSv1.1" https_protocol;
    15.         "TLSv1.2" https_protocol;
    16.         "TLSv1.3" https_protocol;
    17.     }
    18.     server {
    19.         listen 8990;
    20.         ssl_preread on;
    21.         proxy_pass $upstream;
    22.     }
    23. }
    复制代码
    1.   server {
    2.         listen 10002 ssl;
    3.         server_name www.xxx.com;
    4.         ssl_certificate /etc/ssl/certs/www.abc.com/server.crt;
    5.         ssl_certificate_key /etc/ssl/certs/www.abc.com/server.key;
    6.         #减少点击劫持
    7.         #add_header X-Frame-Options DENY;
    8.         add_header X-Frame-Options AllowAll;
    9.         #禁止服务器自动解析资源类型
    10.         add_header X-Content-Type-Options nosniff;
    11.         #防XSS攻击
    12.         add_header X-Xss-Protection 1;
    13.         #优先采取服务器算法
    14.         ssl_prefer_server_ciphers on;
    15.         #协议
    16.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    17.         ssl_session_cache shared:SSL:10m;
    18.         ssl_session_timeout 10m;

    19.         location / {
    20.             proxy_pass http://127.0.0.1:8991/;
    21.         }
    22.     }
    复制代码


    总结


      1. openssl: error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
      复制代码
      这个问题是很大的难点,排查好久才找到一个对应版本安装成功(我的是麒麟银河V10,版本OpenSSL 1.1.1f),关键是怎么找到对应版本的过程当时没有记录,现在也想不起来了,😫
    • open-ssl验证时本地发现有open-ssl,所以就跳过了第一步 结果nginx make报错
    1. make -f objs/Makefile
    2. make[1]: Entering directory '/opt/nginx-1.21.5'
    3. cd /usr/local/ssl/ \
    4. && if [ -f Makefile ]; then make clean; fi \
    5. && ./config --prefix=/usr/local/ssl//.openssl no-shared no-threads  \
    6. && make \
    7. && make install_sw LIBDIR=lib
    8. /bin/sh: line 2: ./config: No such file or directory
    9. make[1]: *** [objs/Makefile:1447: /usr/local/ssl//.openssl/include/openssl/ssl.h] Error 127
    复制代码
    OpenSSL源代码未正确指定:在Nginx的配置过程中,你可能没有正确指定OpenSSL的源代码目录。你需要确保–with-openssl选项指向的是OpenSSL的源代码目录,而不是安装目录。
    上传openssl-1.1.1f.tar.gz包(和验证时的版本一致即可)解压后指定–with-openssl到解压目录
    1. --with-openssl=/opt/openssl-1.1.1f
    复制代码
    1. ./configure \
    2.   --prefix=/usr/local/nginx \
    3.   --user=nginx \
    4.   --group=nginx \
    5.   --with-pcre \
    6.   --with-openssl=/opt/openssl-1.1.1f \
    7.   --with-http_ssl_module \
    8.   --with-http_v2_module \
    9.   --with-http_realip_module \
    10.   --with-http_addition_module \
    11.   --with-http_sub_module \
    12.   --with-http_dav_module \
    13.   --with-http_flv_module \
    14.   --with-http_mp4_module \
    15.   --with-http_gunzip_module \
    16.   --with-http_gzip_static_module \
    17.   --with-http_random_index_module \
    18.   --with-http_secure_link_module \
    19.   --with-http_stub_status_module \
    20.   --with-http_auth_request_module \
    21.   --with-http_image_filter_module \
    22.   --with-mail \
    23.   --with-threads \
    24.   --with-mail_ssl_module \
    25.   --with-stream_ssl_module \
    26.   --with-stream --with-stream_ssl_preread_module \
    27. && make
    复制代码

    • 双协议不支持获取访问ip穿透
      更改为https或者http单协议可获取到客户端访问ip,如果代理中包含websocket需要把响应代理放到和ssl配置的配置文件中
      关键配置:
    1. proxy_set_header Host $host;
    2. proxy_set_header X-Real-IP $remote_addr;
    3. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    复制代码
    nginx.conf 示例
    1. user root;
    2. worker_processes auto;
    3. error_log /usr/local/nginx/logs/error.log;

    4. events {
    5.     worker_connections  1024;
    6. }

    7. http {
    8.     # log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    9.     #                   '$status $body_bytes_sent "$http_referer" '
    10.     #                   '"$http_user_agent" "$http_x_forwarded_for"';
    11.     log_format  main  '$year$month$day $hour:$minutes:$seconds ' '[$status] ' '【$http_x_forwarded_for $remote_addr $http_host】' '[$request_uri] ' ;

    12.     access_log  /usr/local/nginx/logs/access.log  main;
    13.        
    14.     underscores_in_headers on;
    15.     sendfile            on;
    16.     tcp_nopush          on;
    17.     tcp_nodelay         on;
    18.     keepalive_timeout   65;
    19.     types_hash_max_size 4096;

    20.     include /etc/nginx/mime.types;
    21.     client_max_body_size 10m;

    22.     default_type        application/octet-stream;
    23.     #default_type         text/html;


    24.     #gzip
    25.     gzip on;
    26.     gzip_min_length 1024;
    27.     gzip_comp_level 6;
    28.     gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml  font/woff;
    29.     gzip_vary on;
    30.     gzip_disable "MSIE [1-6]\.";
    31.     gzip_buffers 32 16k;
    32.     gzip_http_version 1.0;

    33.     include /usr/local/nginx/conf/conf.d/*.conf;

    34.      server {
    35.             listen 8990 ssl;
    36.                 server_name www.bbcc.com;
    37.         ssl_certificate /etc/ssl/certs/www.bbcc.com/server.crt;
    38.         ssl_certificate_key /etc/ssl/certs/www.bbcc.com/server.key;
    39.         #减少点击劫持
    40.         #add_header X-Frame-Options DENY;
    41.         add_header X-Frame-Options AllowAll;
    42.         #禁止服务器自动解析资源类型
    43.         add_header X-Content-Type-Options nosniff;
    44.         #防XSS攻击
    45.         add_header X-Xss-Protection 1;
    46.         #优先采取服务器算法
    47.         ssl_prefer_server_ciphers on;
    48.         #协议
    49.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    50.         ssl_session_cache shared:SSL:10m;
    51.         ssl_session_timeout 10m;

    52.         # 自定义时间变量
    53.                 if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
    54.                         set $year $1;
    55.                         set $month $2;
    56.                         set $day $3;
    57.                         set $hour $4;
    58.                         set $minutes $5;
    59.                         set $seconds $6;
    60.                 }

    61.         location / {
    62.             autoindex off;
    63.                 proxy_set_header Host $host;
    64.             proxy_set_header X-Real-IP $remote_addr;
    65.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    66.             proxy_pass http://172.168.18.31:8990/;
    67.         }
    68.         
    69.         location /gws {
    70.             proxy_http_version 1.1;
    71.             proxy_set_header Upgrade $http_upgrade;
    72.             proxy_set_header Connection "upgrade";
    73.             proxy_pass http://172.168.18.31:8990;
    74.         }
    75.     }
    76.    
    77. }
    复制代码
    到此这篇关于Nginx内网环境开启https双协议的文章就介绍到这了,更多相关Nginx开启https双协议内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表