小言_互联网的博客

如何给网站通过Nginx配置https证书详细过程

203人阅读  评论(0)

前言:

如果开发微信小程序,那么前端请求的接口就需要https的链接,那么你就需要在服务器里配置https证书。这里我用的阿里云服务器,所以我就针对阿里云服务器讲解下如何通过nginx配置https证书。

正文:

一、需要准备的东西

1.需要准备一个域名

 

2.需要把域名与服务器的ip进行绑定

3.需要买个ssl证书并且与域名绑定,并且下载证书

二、准备好后,进入到服务器,对nginx进行配置即可

1.首先我们需要把证书放到nginx的目录下

2.修改nginx的配置

这份是你安装完nginx的默认配置,找到 HTTPS server 取消注解进行修改即可。


  
  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. }

 这是修改后的配置文件


  
  1. server {
  2. listen 443 ssl;
  3. server_name xxxxx; #域名
  4. ssl_certificate cert/xxxxx.pem; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
  5. ssl_certificate_key cert/xxxxxx.key; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
  6. ssl_session_cache shared:SSL: 1m;
  7. ssl_session_timeout 5m;
  8. ssl_ciphers HIGH:!aNULL:!MD5;
  9. ssl_prefer_server_ciphers on;
  10. ssl_protocols TLSv1. 1 TLSv1. 2;
  11. location / {
  12. root html;
  13. index index.html index.htm;
  14. }
  15. }

安装完后,通过https可以访问你的nginx,就证明你配置成功了

 三、可能遇到的坑

nginx安装时一定要把ssl模块也安装了,当然之后也可以添加ssl模块。如果不添加则会在改完配置文件启动时报错。

总结:

其实这个配置不难,就是整个流程大家可能不清晰,我把流程在重复一下,买域名->域名绑定ip->买ssl证书->证书绑定域名->下载证书->放到你安装的nginx目录下->如果nginx没有安装ssl模块,重新编译添加ssl模块->添加https模块的配置->阿里云服务器开放443端口和你的nginx端口->启动nginx,访问时https:\\xxxx域名 ,访问到nginx就成功了。

我是阿达,一名喜欢分享知识的程序员,时不时的也会荒腔走板的聊一聊电影、电视剧、音乐、漫画,这里已经有20692位小伙伴在等你们啦,感兴趣的就赶紧来点击关注我把,哪里有不明白或有不同观点的地方欢迎留言!


转载:https://blog.csdn.net/jdk_wangtaida/article/details/116458858
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场