跳转到主内容
极星编程网:以代码为星,赴技术山海!

nginx使用ssl模块配置支持HTTPS访问的方法

默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数.

需求:

做一个网站域名为 www.localhost.cn 要求通过https://www.localhost.cn进行访问.

10.10.100.8 www.localhost.cn实验步骤:1.首先确保机器上安装了openssl和openssl-devel#yum install openssl

#yum install openssl-devel

2.创建服务器私钥,命令会让你输入一个口令:

openssl genrsa -des3 -out server.key 1024 //生成私钥

#因为以后要给nginx使用.每次reload nginx配置时候都要你验证这个PAM密码的.由于生成时候必须输入密码,你可以输入后 再删掉。

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 //除去密码以便reload询问时不需要密码

5.配置nginx

最后标记证书使用上述私钥和CSR:openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

6.修改Nginx配置文件,让其包含新标记的证书和私钥:

#vim /usr/local/nginx/conf/nginx.conf http {include server/*.cn;}

7.修改Nginx配置文件,让其包含新标记的证书和私钥:

#vim /usr/local/nginx/server/www.localhost.cn

server {

listen 443; //监听端口为443

server_name www.localhost.cn;

ssl on;           //开启ssl

ssl_certificate /etc/pki/tls/certs/server.crt; //证书位置

ssl_certificate_key /etc/pki/tls/certs/server.key; //私钥位置

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;      //指定密码为openssl支持的格式

ssl_ciphers HIGH:!aNULL:!MD5; //密码加密方式

ssl_prefer_server_ciphers on; //依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

location / {

root html; //根目录的相对位置

index index.html index.htm;

}

}

8.启动nginx服务器.

#/usr/local/nginx/sbin/nginx -s reload //如果环境允许的话直接杀掉进程在启动nginx 如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”即可如:[root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

9.测试网站是否能够通过https访问

https://www.localhost.cn另外还可以加入如下代码实现80端口重定向到443server {listen 80;server_name www.localhost.cn;#rewrite ^(.*) https://$server_name$1 permanent;rewrite ^(.*)$ https://$host$1 permanent;}

过以下配置,可以设置一个虚拟主机同时支持HTTP和HTTPSlisten 80;listen 443 default ssl;同时支持80和443同时访问配置:server {listen 80 default backlog=2048;listen 443 ssl;server_name www.localhost.com;#ssl on; //注释掉ssl_certificate /usr/local/https/www.localhost.com.crt;ssl_certificate_key /usr/local/https/www.localhost.com.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;ssl_prefer_server_ciphers on;Nginx 设置忽略favicon.ico文件的404错误日志(关闭favicon.ico不存在时记录日志)在 server { … }内添加如下信息.

location = /favicon.ico {log_not_found off;access_log off;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:详解nginx使用ssl模块配置支持HTTPS访问nginx添加ssl模块的方法教程详解nginx使用ssl模块配置HTTPS支持Nginx新增http_ssl_module模块的解决方案

相关文章