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

1
2
#yum install openssl
#yum install openssl-devel

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

1
2
openssl genrsa -des3 - out  server.key 1024   //生成私钥
#因为以后要给nginx使用.每次reload nginx配置时候都要你验证这个PAM密码的.由于生成时候必须输入密码,你可以输入后 再删掉。

3.创建签名请求的证书(CSR):

1
openssl req - new  -key server.key - out  server.csr                                 //生成证书颁发机构,用于颁发公钥

4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

1
2
cp server.key server.key.org
openssl rsa - in  server.key.org - out  server.key                                   //除去密码以便reload询问时不需要密码

5.配置nginx
最后标记证书使用上述私钥和CSR:

1
openssl x509 -req -days 365 - in  server.csr -signkey server.key - out  server.crt

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

1
2
3
4
5
#vim /usr/local/nginx/conf/nginx.conf
http {
 
         include server/*.cn;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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服务器.

1
#/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”即可

1
如:[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访问

1
https: //www.localhost.cn

另外还可以加入如下代码实现80端口重定向到443

1
2
3
4
5
6
server {
listen 80;
server_name www.localhost.cn;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$  https: //$host$1 permanent;
}

过以下配置,可以设置一个虚拟主机同时支持HTTP和HTTPS

1
2
listen 80;
listen 443  default  ssl;

同时支持80和443同时访问配置:

1
2
3
4
5
6
7
8
9
10
11
12
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 { … }内添加如下信息.

1
2
3
4
location = /favicon.ico {
log_not_found off;
access_log off;
}

 

参考文档:http://dreamfire.blog.51cto.com/418026/1141302/

    http://www.nginx.cn/265.html

猜你喜欢

转载自blog.csdn.net/martingpf/article/details/80770482