nginx配置https请求(使用openSSL生成秘钥和证书)
一、window下载nginx
1、从官网下载window 64位的nginx:
nginx下载地址 http://nginx.org/en/download.html
2、解压到指定目录:
-
cmd窗口运行方式:
cmd进入nginx所在目录;
nginx -t 检查nginx.conf配置文件格式是否正确 start nginx 启动nginx 访问 http://localhost 可见nginx首页
二、安装 OpenSSL
1、下载OpenSSL
http://slproweb.com/products/Win32OpenSSL.html
下载完成后,安装到指定目录,下一步下一步。
2、配置环境变量
key:OPENSSL_HOME
value:C:\wnmp\OpenSSL-Win64\bin
加入path:%OPENSSL_HOME%;
三、生成https证书和秘钥
1、nginx目录下创建文件夹 ssl (用于存放证书)
2、cmd窗口进入ssl目录下(管理员方式打开)
- 创建私钥
# openssl genrsa -des3 -out test.key 1024 //test 自己取的名字
下面有密码的地方都是用这个密码。
-
创建 csr 证书
# openssl req -new -key test.key -out test.csr
执行命令后会让你输入一些信息:其他看着填,Common Name这个写我们要使用https访问的域名
-
复制 test.key,在同目录下粘贴一份,文件名改为 test.copy.key
或者用命令方式:# openssl rsa -in test.copy.key -out test.key
-
生成crt证书
openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
最终:
四、nginx.conf配置https请求
1、将 test.key test.crt 两个文件复制到 nginx的conf目录下
2、nginx.conf配置
worker_processes 1;
#error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
}
http {
include mime.types;
#sendfile on;
charset utf-8;
keepalive_timeout 65;
server {
# ssl标记9001端口https的请求方式
listen 9001 ssl;
# 访问的域名
server_name localhost;
#证书文件位置
ssl_certificate test.crt;
#私钥文件位置
ssl_certificate_key test.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
#location = /50x.html {
# root html;
#}
location /cas {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /dataService {
proxy_pass http://127.0.0.1:8082;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
最终访问nginx首页 https://localhost:9001
或者访问服务(加上服务路径):https://localhost:9001/dataService