首页 > 系统相关 >nginx扩展之支持多个ssl加密虚拟主机

nginx扩展之支持多个ssl加密虚拟主机

时间:2024-08-29 09:15:37浏览次数:9  
标签:www http 虚拟主机 nginx magedu ssl net root

nginx支持一台服务器唯一IP:PORT,根据server_name创建区分多个经过ssl加密的https虚拟主机,apache不支持

 

生成www.magedu.net域名证书:

[[email protected] logs]# cd /etc/pki/tls/certs/
[[email protected] certs]# vim Makefile
%.key:
        umask 77 ; \
        #/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
        /usr/bin/openssl genrsa $(KEYLEN) > $@                    #去掉-aes128,创建私钥key时,不会再有输入密码加密key的过程,生成无加密的key
[[email protected] certs]# mv magedu.net* /apps/nginx4/ssl/
[[email protected] certs]# ll /apps/nginx4/ssl/                      #
total 16
-rw------- 1 root root 1330 Mar  8 15:23 magedu.net.crt
-rw-r--r-- 1 root root 1679 Mar  8 15:22 magedu.net.key
-rw------- 1 root root 1330 Mar  7 14:11 magedu.org.crt
-rw------- 1 root root 1675 Mar  7 14:12 magedu.org.key

nginx配置:

[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf
server {
        listen 443 ssl;
        listen 80;
        server_name www.magedu.org;
        root /data/site14/;
        #ssl on;
        ssl_certificate /apps/nginx4/ssl/magedu.org.crt;
        ssl_certificate_key /apps/nginx4/ssl/magedu.org.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        access_log /apps/nginx4/logs/magedu.org.ssl.access.log  access_json ;
        location / {
                if ($scheme = http) {
                        rewrite ^/(.*)$ https://www.magedu.org/$1 permanent;
                }
        }
}
server {
        listen 443 ssl;
        listen 80;
        server_name www.magedu.net;
        root /data/site1/;
        ssl_certificate /apps/nginx4/ssl/magedu.net.crt;
        ssl_certificate_key /apps/nginx4/ssl/magedu.net.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        access_log /apps/nginx4/logs/magedu.net.ssl.access.log  access_json;
        location / {
                if ($scheme = http) {
                        rewrite ^/(.*)$ https://www.magedu.net/$1 permanent;
                }
        }
}

验证:

[[email protected] ~]# curl -Lk http://www.magedu.net/
/data/site1/index.html
{"@timestamp":"2021-03-08T15:41:13+08:00","host":"10.0.0.126","clientip":"10.0.0.126","size":185,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","scheme":"http","http_host":"www.magedu.net","uri":"/","domain":"www.magedu.net","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"301"}
{"@timestamp":"2021-03-08T15:41:13+08:00","host":"10.0.0.126","clientip":"10.0.0.126","size":23,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","scheme":"https","http_host":"www.magedu.net","uri":"/index.html","domain":"www.magedu.net","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200"}


[[email protected] ~]# curl -Lk http://www.magedu.org
/data/site14/index.html
{"@timestamp":"2021-03-08T15:50:11+08:00","host":"10.0.0.126","clientip":"10.0.0.126","size":185,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","scheme":"http","http_host":"www.magedu.org","uri":"/","domain":"www.magedu.org","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"301"}
{"@timestamp":"2021-03-08T15:50:11+08:00","host":"10.0.0.126","clientip":"10.0.0.126","size":24,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","scheme":"https","http_host":"www.magedu.org","uri":"/index.html","domain":"www.magedu.org","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200"}

浏览器F12: 301重定向缓存:Status Code: 301 Moved Permanently (from disk cache)

nginx实现此功能的原因:

[[email protected] ~]# nginx  -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled

 

标签:www,http,虚拟主机,nginx,magedu,ssl,net,root
From: https://www.cnblogs.com/cnblogsfc/p/14592021.html

相关文章

  • nginx共享资源站点
    配置nginx文件下载服务模块:ngx_http_autoindex_module常用指令:autoindexon|off;自动文件索引功能,默为offDefault: autoindexoff;Context: http,server,locationautoindex_exact_sizeon|off;计算文件确切大小(单位bytes),off显示大概大小(单位K、M),默......
  • nginx基于用户的访问控制
    ngx_http_auth_basic_module模块 实现基于用户的访问控制,使用basic机制进行用户认证auth_basicstring|off;auth_basic_user_filefile;Default: —Context: http,server,location,limit_exceptlocation/admin/{ auth_basic"AdminArea"......
  • nginx日志格式
    ngx_http_log_modulengx_http_log_module模块指定日志格式记录请求log_formatnamestring...;string可以使用nginx核心模块及其它模块内嵌的变量Default:log_formatcombined"...";Context: httpaccess_logpath[format[buffer=size][gzip[=level]......
  • nginx第三方模块echo和变量
    第三方模块:echo第三模块是对nginx的功能扩展,第三方模块需要在编译安装nginx的时候使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块,需要重新编译源码......
  • nginx输出nginx的基本状态信息
    输出nginx的基本状态信息模块:ngx_http_stub_status_module输出信息示例:Activeconnections:291server   accepts  handled  requests#下面三个数分别对应accepts,handled,requests      166309481663094831070465Reading:6Writing:179Wait......
  • nginx 压缩gzip
    ngx_http_gzip_modulengx_http_gzip_module用gzip方法压缩响应数据,节约带宽gzipon|off;启用或禁用gzip压缩Default: gzipoff;Context: http,server,location,ifinlocationgzip_comp_levellevel;消耗CPU 压缩比由低到高:1到9,默认:1 gzip_di......
  • nginx favicon.ico
    favicon.ico文件是浏览器收藏网址时显示的图标,当使用浏览器访问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错(F12查看浏览器请求情况) 解决方案:1、不加favicon.ico......
  • nginx之ssl认证(https访问)
    ngx_http_ssl_modulengx_http_ssl_module模块:sslon|off;为指定虚拟机启用HTTPSprotocol,建议用listen指令代替ssl_certificatefile; 当前虚拟主机使用PEM格式的证书文件ssl_certificate_keyfile;......
  • nginx
    一、I/O模型二、nginx概述官网:http://nginx.org 2.1、nginx介绍nginx:engineX,是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.0nginx......
  • Nginx 的编译并打包成二.txt
    Nginx的编译并打包成二进制文件是一个涉及源代码编译和静态链接的过程。‌下面是一个简单的步骤说明,‌帮助你从源代码编译Nginx并生成一个可独立运行的二进制文件。‌安装依赖首先,‌确保你的系统上安装了编译Nginx所需的依赖项。‌对于大多数Linux发行版,‌你可能需要安......