nginx配置HTTPS

来源:互联网 发布:内网端口转发 编辑:程序博客网 时间:2024/05/21 10:31

转自:http://blog.csdn.net/weixin_35884835/article/details/52588157

使用ssl模块配置同时支持http和https并存

一,生成证书

# 1、首先,进入你想创建证书和私钥的目录,例如:cd /etc/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.orgopenssl rsa -in server.key.org -out server.key# 5、最后标记证书使用上述私钥和CSR:openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

二,配置nginx

cd /etc/nginxvim 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 http://IP地址/ssl/;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

重启nginx,在浏览器输入:https://IP地址 会跳转到 http://IP地址/ssl/ 这个地址(后期调整为webservice接口地址即可)

三、受浏览器信任的StartSSL免费SSL证书:

跟VeriSign一样,StartSSL(网址:http://www.startssl.com,公司名:StartCom)也是一家CA机构,

它的根证书很 久之前就被一些具有开源背景的浏览器支持(Firefox浏览器、谷歌Chrome浏览器、苹果Safari浏览器等)。

四、项目需要,将访问目录 \services\ 由http访问 重定向到 https (解决方法:nginx rewrite 加上 location 方式实现)

location ~ /services/.*$ {        if ($server_port ~ "^80$"){            set $rule_0 1$rule_0;        }        if ($rule_0 = "1"){            rewrite /(.*) https://IP地址/$1 permanent;                       break;        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

五,配置结束上传以后用nginx -t 测试下配置无误 就reload一下nginx服务 检查443端口是否在监听

/usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful (显示表示配置文件没有错误)service nginx reload (重新加载nginx服务)  netstat -lan | grep 443 (查看443端口)  tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN (有看到这一行 就表示HTTPS已经在工作了) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
3
原创粉丝点击