阿里云申请 SSL

控制台,找到 SSL 证书菜单,购买“免费版个人 DV”,按照提示把 SSL 证书设置到对应的域名。

把 SSL 证书上传到 nginx 目录

先把 SSL 证书从阿里云控制台下载下来,服务器上新建目录 /etc/nginx/cert, 把证书文件上传到 /etc/nginx/cert。
设置 nginx.conf 如下:

  1. user root;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log;
  4. pid /run/nginx.pid;
  5. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. tcp_nopush on;
  17. tcp_nodelay on;
  18. keepalive_timeout 65;
  19. types_hash_max_size 2048;
  20. include /etc/nginx/mime.types;
  21. default_type application/octet-stream;
  22. # Load modular configuration files from the /etc/nginx/conf.d directory.
  23. # See http://nginx.org/en/docs/ngx_core_module.html#include
  24. # for more information.
  25. include /etc/nginx/conf.d/*.conf;
  26. upstream my_nodejs_upstream {
  27. server 127.0.0.1:3001;
  28. keepalive 64;
  29. }
  30. server {
  31. listen 80;
  32. server_name wx.huizhanyt.com;
  33. location / {
  34. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  35. proxy_set_header X-Real-IP $remote_addr;
  36. proxy_set_header Host $http_host;
  37. proxy_http_version 1.1;
  38. proxy_set_header Upgrade $http_upgrade;
  39. proxy_set_header Connection "upgrade";
  40. proxy_pass http://my_nodejs_upstream/;
  41. proxy_redirect off;
  42. proxy_read_timeout 240s;
  43. }
  44. }
  45. server {
  46. listen 443 ssl;
  47. server_name wx.huizhanyt.com;
  48. ssl_certificate cert/wx.huizhanyt.com.pem; #将domain name.pem替换成您证书的文件名。
  49. ssl_certificate_key cert/wx.huizhanyt.com.key; #将domain name.key替换成您证书的密钥文件名。
  50. ssl_session_timeout 5m;
  51. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  52. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  53. ssl_prefer_server_ciphers on;
  54. location / {
  55. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  56. proxy_set_header X-Real-IP $remote_addr;
  57. proxy_set_header Host $http_host;
  58. proxy_http_version 1.1;
  59. proxy_set_header Upgrade $http_upgrade;
  60. proxy_set_header Connection "upgrade";
  61. proxy_pass http://my_nodejs_upstream/;
  62. proxy_redirect off;
  63. proxy_read_timeout 240s;
  64. }
  65. }
  66. }

用 sudo nginx -t 检查配置是否有错误。
重启 nginx

firewallD 添加 https 服务

firewall-cmd —permanent —add-service=https