nginx配置 SSL证书【已配置成功的案例】

我个人配置成功的案例:

 

# nformation on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 是否设置后台运行默认是on后台运行!:
daemon off;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
		log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

		access_log  /var/log/nginx/access.log  main;

		sendfile            on;
		tcp_nopush          on;
		tcp_nodelay         on;
		keepalive_timeout   65;
		types_hash_max_size 2048;

		include             /etc/nginx/mime.types;
		default_type        application/octet-stream;

		# Load modular configuration files from the /etc/nginx/conf.d directory.
		# See http://nginx.org/en/docs/ngx_core_module.html#include
		# for more information.

		include /etc/nginx/conf.d/*.conf;
	
		server {
		    listen 80;
		    #监听443端口(必须)
		    listen 443 ssl;
		
		    server_name xxxx.org www.xxxx.org;
		    index index.html index.htm;
		    root /root/www/xxxx.org;

		    #引用证书(必须,放在conf/ssl目录下可以用相对路径,其他位置用绝对路径)
		    ssl_certificate     ssl/xxxx.org.crt;
		    ssl_certificate_key ssl/xxxx.org.key;

		    #协议优化(可选,优化https协议,增加安全性)
		    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
		    ssl_prefer_server_ciphers on;
		    ssl_session_cache shared:SSL:10m;
		    ssl_session_timeout 10m;

		    #自动跳转到HTTPS(可选,和下面的部分域名跳转不能同时使用)
		    if ($server_port = 80){
				rewrite ^(/.*)$ https://$host$1 permanent;
		    }

		    #其他的配置信息···
    }
		
}

 

 

 

参考别人的:http://371cloud.net/?id=107

Nginx格式的证书解压后应包含两个文件:

domain.com.crt   (域名证书) 有的证书可能是cer 或 pem 或其他后缀名,都可以
domain.com.key   (私钥文件)

配置Nginx找到站点的配置文件,在server中添加443端口监听和证书文件引用

server {
    listen 80;
    #监听443端口(必须)
    listen 443 ssl;
    
    server_name 371cloud.cn www.371cloud.cn;
    index index.html index.php index.htm;
    root /www/wwwroot/371cloud.cn;

    #引用证书(必须,放在conf/ssl目录下可以用相对路径,其他位置用绝对路径)
    ssl_certificate     ssl/domain.com.crt;
    ssl_certificate_key ssl/domain.com.key;

    #协议优化(可选,优化https协议,增加安全性)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    #其他的配置信息···
}

以上为最基本的配置,其他参数请根据生产环境需要添加

安装后重启nginx使其生效

centos6
service nginx restart
centos7
systemctl restart nginx

如果重启nginx失败使用如下命令检测nginx是否支持SSL

nginx -V

如果有显示 –with-http_ssl_module 表示已编译openssl,支持安装ssl,仔细检查配置文件

如果没有安装请下载nginx源码重新编译(编译请技术人员操作)

./configure --with-http_stub_status_module --with-http_ssl_module
make && make install

强制跳转HTTPS在配置文件中添加跳转代码

server {
    #站点和SSL的配置信息···

    #自动跳转到HTTPS(可选,和下面的部分域名跳转不能同时使用)
    if ($server_port = 80){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

    #绑定域名较多,只让部分域名跳转(根据情况选用,和上面的全部跳转不能同时使用)
    set $redirect_https 1;
    if ($server_port = 80) {
        set $redirect_https "${redirect_https}2";
    }
    if ($http_host = 'abc.com') {
        set $redirect_https "${redirect_https}3";
    }
    if ($http_host = 'cde.com') {
        set $redirect_https "${redirect_https}3";
    }
    if ($redirect_https = "123") {
        #当前域名跳转
        rewrite ^(.*)$ https://$host$1 permanent;
        #可以跳转到指定的域名
        #rewrite ^(.*)$ https://www.abcde.com$1 permanent;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值