nginx配置https证书
一、本地生成证书
1、首先,进入你想创建证书和私钥的目录,例如:
cd /usr/local/nginx
2、创建服务器私钥,命令会让你输入一个口令:(随便写,也可以根据实际情况写)
openssl genrsa -des3 -out server.key 1024
3、创建签名请求的证书(CSR):
openssl req -new -key server.key -out server.csr
4、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
5、最后标记证书使用上述私钥和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
这样,当前目录下就生成四个文件:
server.crt
server.csr
server.key
server.key.org
6、配置nginx.conf
#
# HTTPS server configuration
#
server {
listen 443;
server_name 本机的IP地址;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
location / {
#root html;
#index testssl.html index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass 目标地址;
}
}
二、外部获取域名的绑定https证书
1、下载nginx版本的证书
2、将xxx.pem文件和xxx.key文件放入想要存放的目录,比如:
cd /usr/local/nginx/cert/
3、配置nginx.conf
server {
listen 443 ssl;
server_name 域名;
ssl on;
ssl_certificate "/usr/local/nginx/cert/xxxxx.pem";
ssl_certificate_key "/usr/local/nginx/cert/xxxxx.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass 目标地址;
}
}
4、如果想要http访问自动转发到https需要在nginx.conf文件中添加
server {
listen 10003;
server_name http://IP;
rewrite ^(.*)$ https://$host$1 permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass https://IP:PORT;
}
}