nginx配置ssl证书

迈不过友情╰ 2022-01-05 05:15 496阅读 0赞

配置背景:阿里云域名的免费ssl证书

  • 下载地址:https://www.aliyun.com/product/cas?spm=a2c4g.750001.security.3.4cc17b13QeAGMN 最后一个是免费的

一、参考以下官方配置文档及视频(官方教程很详细)

  • 配置文档参考:https://help.aliyun.com/document_detail/98728.html?spm=a2c4g.11186623.6.572.4d8d20c6kopxzV

  • 相应视频教程:https://help.aliyun.com/video_detail/54216.html?spm=a2c4g.11174359.2.3.43e42d03XCOCdp

二、根据教程安装完成,但是在重启nginx时遇到以下的问题:

  nginx: [emerg] unknown directive “ssl” in /usr/local/nginx/conf/nginx.conf:112

  问题原因:nginx安装时没有生成ssl模块,需要在重新解压安装包,然后重新生成nginx,最后替换掉现有的nginx运行目录中的对应的nginx文件就可以了。

  **问题解决:参考如下两篇文章,很详细**

       https://yq.aliyun.com/articles/659755(官方解决办法)

       https://blog.csdn.net/weiyangdong/article/details/80008543

    

  解决问题,nginx启动成功,但还是无法访问:端口设置问题,https的必须是443(因为https的默认端口是443)

      注 :   当网址为http://时,默认端口为80

           当网址为https://时,默认端口为443

三、用户打开http访问时,自动跳转至https访问,官方配置文档中也有详细代码介绍

  以下是,多个安全证书的配置文件: 配置多个安全证书,打开相应的http时,跳转至对应的https 

  1.   server {
  2. listen 80;
  3. server_name www.ceshi.com;
  4. root /var/html/www;
  5. location / {
  6. rewrite ^(.*)$ https://www.ceshi.com permanent;
  7. }
  8. }
  9. server {
  10. listen 80;
  11. server_name www.ceshi2.com;
  12.      #强制跳转至https设置
  13. return 301 https://www.ceshi2.com;
  14. }
  15. #个人网站
  16. server {
  17. listen 443;
  18. server_name www.ceshi.com;
  19. ssl on;
  20. #将domain name.pem替换成您证书的文件名。
  21. ssl_certificate cert/2339342_ceshi_com.pem;
  22. #将domain name.key替换成您证书的密钥文件名。
  23. ssl_certificate_key cert/2339342_ceshi_com.key;
  24. ssl_session_timeout 5m;
  25. #使用此加密套件。
  26. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  27. #使用该协议进行配置。
  28. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  29. ssl_prefer_server_ciphers on;
  30. location / {
  31. proxy_set_header Host $host;
  32. proxy_set_header X-Real-IP $remote_addr;
  33. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34. proxy_pass http://127.0.0.1:8083;
  35. }
  36. error_page 500 502 503 504 /50x.html;
  37. location = /50x.html {
  38. root html;
  39. }
  40. }
  41. # HTTPS server 2  第二个安全证书的配置
  42. server {
  43. listen 443 ssl;
  44. server_name www.ceshi2.com;
  45. ssl on;
  46. #将domain name.pem替换成您证书的文件名。
  47. ssl_certificate cert/2339347_ceshi2_com.pem;
  48. #将domain name.key替换成您证书的密钥文件名。
  49. ssl_certificate_key cert/2339347_ceshi2_com.key;
  50. ssl_session_timeout 5m;
  51. #使用此加密套件。
  52. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  53. #使用该协议进行配置。
  54. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  55. ssl_prefer_server_ciphers on;
  56. location / {
  57. proxy_set_header Host $host;
  58. proxy_set_header X-Real-IP $remote_addr;
  59. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  60. proxy_pass http://127.0.0.1:8083;
  61. }
  62. error_page 500 502 503 504 /50x.html;
  63. location = /50x.html {
  64. root html;
  65. }
  66. }

转载于:https://www.cnblogs.com/mufengforward/p/11008014.html

发表评论

表情:
评论列表 (有 0 条评论,496人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx 配置 ssl 证书

    > 在部署线下测试环境时浏览器会自动将http替换成https,又因为测试环境没有证书经常会导致跳转错误,所以自己配置一个ssl证书就可以避免这个问题。 > 下面记录我在M