nginx之https配置

来源:互联网 发布:淘宝卖家打快递单 编辑:程序博客网 时间:2024/06/15 10:29

一、生成证书

# 1、首先,进入你想创建证书和私钥的目录,例如:cd /etc/nginx/

# 2、创建服务器私钥,命令会让你输入一个口令:openssl genrsa -des3 -out server.key1024

# 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 -days365 -in server.csr -signkey server.key -out server.crt

我是直接购买的第三方服务证书,所以跳过这步

二、配置nginx

cd /etc/nginx

vim nginx.conf

server {

        listen       443 ssl;
        server_name  localhost;
#ssl on; 
#root html; index index.html index.htm; 

        ssl_certificate      cert/214004025980350.pem;
        ssl_certificate_key  cert/214004025980350.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /data/html;
            index  index.html index.htm index.php;
        }

location ~ \.php {    
      root          /data/html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_split_path_info ^(.+\.php)(.*)$;     
      fastcgi_param PATH_INFO $fastcgi_path_info;   
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
}
    }

三、测试Nginx配置文件并重新reload配置文件

/usr/sbin/nginx -t

service nginx reload

四、检查443端口是否在监听

netstat -lan | grep 443 (查看443端口)

tcp 0 0 0.0.0.0:4430.0.0.0:* LISTEN (有看到这一行 就表示HTTPS已经在工作了)


注意:第一次配置https没有经验,导致一开始出现访问服务器静态文件可以,但是访问php动态文件不支持,需要增加对php相关文件的支持

0 0