nginx配置ssl证书

1.前言

我们在访问一个网站的时候,或者一个产品的时候都会通过域名的方式代理映射到相应的ip:port。就比如www.baidu.com,如果要访问此域名,就必须要告诉浏览器通过什么协议去访问,比如可以通过http、https、ws、wss等,前面两种是http协议,加s的具有加密安全性,后面两种是websocket协议,同样加s具有加密安全性。

2.配置ssl

接下来开始配置ssl证书了

首先需要给二级域名 (这里的二级域名是test) 申请一个免费的ssl证书(如果你是土豪,也可以选择烧钱的)

在哪里申请的域名就在哪里申请ssl证书,这里用阿里云上申请的域名就在阿里云控制台申请ssl证书了

申请成功后需要下载证书,我下载的是nginx版的证书,下载后上传到服务器。(最好与该域名下的项目同级,方便查找)

阿里云的nginx版证书有两个文件,一个是key为后缀的,另一个是pem为后缀的

1737930_test.itinga.cn.key
1737930_test.itinga.cn.pem

接下来创建一个目录,将这两个文件放到创建的目录中

cd /root/usr/local/
mkdir test
mkdir cert
cp /home/1737930_test.itinga.cn.key test/cert/
cp /home/1737930_test.itinga.cn.pem test/cert/

剩下的就是配置nginx了

vim /etc/nginx/nginx.conf
http {
    upstream test {
	    server 127.0.0.1:8889;
    }
    #ssl
	server{
	    #ssl 默认监听443端口
		listen 443 ssl;
		#配置代理的域名
		server_name test.itinga.cn;
		#客户端上传文件的大小
        client_max_body_size 50M ;

        #ssl证书对应pem为后缀的文件(不同的厂商云服务器,对应的后缀可能不同)
        ssl_certificate /root/usr/local/test/cert/1737930_test.itinga.cn.pem;
        #ssl证书对应key为后缀的文件
        ssl_certificate_key  /root/usr/local/test/cert/1737930_test.itinga.cn.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        #ssl session缓存
        ssl_session_cache    shared:SSL:1m;
        #ssl session失效时间
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
                #反向代理连接的时长
                proxy_connect_timeout   3;
                反向代理发送数据的时长
                proxy_send_timeout            300;
                反向代理读取数据的时长
                proxy_read_timeout            300;

                #设置反向代理的头 
                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://test/;

                proxy_set_header   Cookie $http_cookie;
                cookie存储的路径
                proxy_cookie_path       /test/ /;
                proxy_cookie_path       /test /;
        }
	}
}
2.1.以上配置就能够保证https生效了,但是如果需要继续使用http的话就需要在配置一段,这段就需要默认监听80端口
    server {
		listen 80;
		server_name    test.itinga.cn ;
		client_max_body_size 50M ;

		location / {
				proxy_connect_timeout   3;
				proxy_send_timeout            300;
				proxy_read_timeout            300;

				# limit_conn perip 50; 
				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://test/;

				proxy_set_header   Cookie $http_cookie;
				proxy_cookie_path       /test/ /;
				proxy_cookie_path       /test /;
		}
	}

以上就配置完成,接下来我们检查下配置是否正确

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果返回以上的信息就说明配置没有语法错误,接下来我们可以启动nginx,我们这里将nginx指向配置问价的方式启动

nginx -c /etc/nginx/nginx.conf

启动后可以用 ps -ef|grep nginx 命令查看启动是否成功

接下来既可以以http://test.itinga.cn方式访问,又可以以https://test.itinga.cn方式访问

2.2.如果需要将http自动映射到https就需要改下配置,就需要把以下代码替换掉2.1中的代码
    server {
		listen 80;
		server_name    test.itinga.cn ;
		client_max_body_size 50M ;

        #内部跳转到https
        rewrite     ^   https://$server_name$request_uri? permanent;
	}

配置完后先检查配置文件是否无误,成功后我们可以通过命令重启,以下是命令

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx -s reload

接下来我们通过访问http://test.itinga.cn会自动跳转到https://test.itinga.cn

3.结语

nginx的功能非常强大,实际上是一个拦截器的功能,可以实现反向代理服务器,也可以实现负载均衡的功能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值