-- 是否强制https function is_force_ssl(stype) local value = get_env_var(stype, 'is_force_ssl') # 通过get_env_var函数读取json文件,判断is_force_ssl的值对应什么(通过json文件灵活配置是否跳转https) if value == nil then value = 'false' end return value end
--检查当前访问的域名是否有对应的ssl证书,用于是否强制跳转https的判断 function is_have_ssl() local server_name = ngx.var.host local is_have_ssl = 'true' if server_name ~= nil then server_name = string.gsub(server_name, "^www%.", "") # www和顶级域名公用一份证书文件 local file = io.open("/usr/local/openresty/nginx/conf/ssl/" .. server_name .. ".pem") if file == nil then file = io.open("/usr/local/openresty/nginx/conf/letsencrypt/ssl/" .. server_name .. "/fullchain1.pem") if file == nil then is_have_ssl = 'false' else file.close() end else file:close() end end return is_have_ssl end
function rewrite_https(stype) -- 有证书也不跳https, 直接返回 -- 可以写不跳转https的逻辑代码,直接return返回
-- a)配置跳转 b)有证书 满足a并b 才会进行跳转 local is_force_ssl = is_force_ssl(stype) local is_have_ssl = is_have_ssl() if is_force_ssl == "true" and is_have_ssl == 'true'then local httpsPort = "" if ngx.var.server_port == "8787"then# 自定义的8787端口,然后跳转https的8989端口 local _uri = ngx.var.uri if string.match(_uri, "/rcenter/") or string.start(_uri, "/fserver/") or string.start(_uri, "/ftl/") or string.start(_uri, "/__purge/") then#url符合这些的也不跳https return#直接返回,不往下继续走了 end httpsPort = ":8989"# 8787访问跳转https8989 elseif ngx.var.server_port == "8383"then httpsPort = ":8585"# 8383跳转8585 end local _host = ngx.var.host local _request_uri = ngx.var.request_uri return ngx.redirect('https://'.._host..httpsPort.._request_uri, '301') else return end end
local ssl = require "ngx.ssl" ssl.clear_certs() local server_name = ssl.server_name() if server_name ~= nil then server_name = string.gsub(server_name,"^www%.","") local file = io.open("/usr/local/openresty/nginx/conf/ssl/" .. server_name ..".pem") if file == nil then file = io.open("/usr/local/openresty/nginx/conf/letsencrypt/ssl/" .. server_name .."/fullchain1.pem") if file == nil then file = io.open("/usr/local/openresty/nginx/conf/ssl/nginx.pem") end end local f = assert(file) local pem_cert_chain = f:read("*a") local der_cert_chain, err = ssl.cert_pem_to_der(pem_cert_chain) ssl.set_der_cert(der_cert_chain) f:close() local kfile = io.open("/usr/local/openresty/nginx/conf/ssl/" .. server_name ..".key.pem") if kfile == nil then kfile = io.open("/usr/local/openresty/nginx/conf/letsencrypt/ssl/" .. server_name .."/privkey1.pem") if kfile == nil then kfile = io.open("/usr/local/openresty/nginx/conf/ssl/nginx.key.pem") end end local k = assert(kfile) local pem_priv_key = k:read("*a") local der_priv_key, err = ssl.priv_key_pem_to_der(pem_priv_key) ssl.set_der_priv_key(der_priv_key) k:close() end