nginx把http迁移到https

发布时间 2023-08-16 16:42:59作者: 杜嘟嘟

买的证书是阿里云提供的

server {
     #HTTPS的默认访问端口443。
     #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
     listen 443 ssl;
     
     #填写证书绑定的域名
     server_name <yourdomain>;
 
     #填写证书文件名称
     ssl_certificate cert/<cert-file-name>.pem;
     #填写证书私钥文件名称
     ssl_certificate_key cert/<cert-file-name>.key;
 
     ssl_session_cache shared:SSL:1m;
     ssl_session_timeout 5m;
	 
     #自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
     #TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

     #表示优先使用服务端加密套件。默认开启
     ssl_prefer_server_ciphers on;
 
 
    location / {
            alias  /usr/local/nginx/html/XXXX/
            index  index.html index.htm;
        }


location /api/ {
        proxy_pass http://localhost:0000/xxx/;
        proxy_set_header Host $host:$server_port;
        proxy_cookie_path /api /;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Accept-Encoding "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
}
}

在这里把之前的nginx配置都挪到listen为 443 ssl的;

然后再新加一个server配置

	server {
        listen       80;
		server_name your_domain;
   #将所有HTTP请求通过rewrite指令重定向到HTTPS。
         rewrite ^(.*)$ https://your_domain$1;
       }