龙空技术网

http——使用Nginx部署HTTPS服务

jiegiser 48

前言:

眼前我们对“nginxgithttps”大概比较关切,同学们都想要学习一些“nginxgithttps”的相关文章。那么小编同时在网上汇集了一些对于“nginxgithttps””的相关知识,希望兄弟们能喜欢,朋友们一起来了解一下吧!

https 服务部署

使用 HTTPS 需要生成私钥与公钥;某个文件夹中打开 git bash 输入命令:openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem;会生成两个文件 localhost-privkey.pem 跟 localhost-cert.pem;

这里我将生成的证书放在了 nginx 安装目录下的 certs 文件夹中。这样就完成了 https 服务的部署。

    proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;    # 表示要启动一个服务    server {        # 开启 ssl 加密算法        listen       443 ssl; # 服务监听的端口-https默认使用端口        server_name  localhost; # 浏览器访问的 host name        # 指定对应的证书        # 相对路径-证书存放的路径        ssl_certificate_key ../certs/localhost-privkey.pem;        ssl_certificate  ../certs/localhost-cert.pem;        # 要代理的路由        location / {          # 匹配到 localhost:9999/ 代理到下面的地址          proxy_pass ;        }    }
#访问 http 跳转到 https 服务

如果我们想在访问 http 的时候直跳转到 https:

    # 增加服务配置 http 跳转到 https    server {      listen       8777 default_server;      listen       [::]:8777 default_server; # 使用 IP 的情况下      server_name localhost;      return 302 ; # server_name 就是前面指定的 localhost,request_uri 就是具体访问的路径    }    # 表示要启动一个服务    server {        # 开启 ssl 加密算法        listen       443 ssl; # 服务监听的端口-https 默认使用端口        server_name  localhost; # 浏览器访问的 host name        # 指定对应的证书        # 相对路径-证书存放的路径        ssl_certificate_key ../certs/localhost-privkey.pem;        ssl_certificate  ../certs/localhost-cert.pem;        # 要代理的路由        location / {          # 匹配到 localhost:9999/ 代理到下面的地址          proxy_pass ;        }    }

标签: #nginxgithttps #nginxhttp自动跳转到https #nginxhttp目录浏览器