• Nginx配置ssl证书实现https访问


    目录

    一、申请下载ssl证书

    二、配置ssl证书

    2.1、添加ssl模块

    2.2、修改配置文件


    一、申请下载ssl证书

    配置ssl证书之前,先准备ssl证书,至于获取的途径很多(阿里云服务,第三方购买)

     我这里购买的是阿里云的免费证书。

    买完申请一下就行。

     下载nginx专用的就行

    二、配置ssl证书

    2.1、添加ssl模块

    如果我们已经安装了nginx我们就需要对他进行重新的编译安装。

    编译前需要安装的依赖环境

    yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make

    开始编译 

    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

     make &&make install

    nginx -V #验证一下模块是否添加

    2.2、修改配置文件

    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 443 ssl;
    24. server_name www.yz1999.top; #证书绑定域名
    25. ssl_certificate /usr/local/8399859_www.yz1999.top.pem; #添加.pem路径
    26. ssl_certificate_key /usr/local/8399859_www.yz1999.top.key; #添加.key路径
    27. ssl_session_cache shared:SSL:1m;
    28. ssl_session_timeout 5m;
    29. ssl_ciphers HIGH:!aNULL:!MD5; #表示使用的加密套件的类型
    30. ssl_prefer_server_ciphers on;
    31. location / {
    32. root html;
    33. index index.html index.htm;
    34. }
    35. }
    36. server {
    37. listen 80;
    38. server_name localhost;
    39. rewrite ^/(.*) https://www.yz1999.top/$1 redirect; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    40. #charset koi8-r;
    41. #access_log logs/host.access.log main;
    42. #location / {
    43. # root html;
    44. # index index.html index.htm;
    45. #}
    46. #error_page 404 /404.html;
    47. # redirect server error pages to the static page /50x.html
    48. #
    49. error_page 500 502 503 504 /50x.html;
    50. location = /50x.html {
    51. root html;
    52. }
    53. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    54. #
    55. #location ~ \.php$ {
    56. # proxy_pass http://127.0.0.1;
    57. #}
    58. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    59. #
    60. #location ~ \.php$ {
    61. # root html;
    62. # fastcgi_pass 127.0.0.1:9000;
    63. # fastcgi_index index.php;
    64. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    65. # include fastcgi_params;
    66. #}
    67. # deny access to .htaccess files, if Apache's document root
    68. # concurs with nginx's one
    69. #
    70. #location ~ /\.ht {
    71. # deny all;
    72. #}
    73. }
    74. # another virtual host using mix of IP-, name-, and port-based configuration
    75. #
    76. #server {
    77. # listen 8000;
    78. # listen somename:8080;
    79. # server_name somename alias another.alias;
    80. # deny access to .htaccess files, if Apache's document root
    81. # concurs with nginx's one
    82. #
    83. #location ~ /\.ht {
    84. # deny all;
    85. #}
    86. # another virtual host using mix of IP-, name-, and port-based configuration
    87. #
    88. #server {
    89. # listen 8000;
    90. # listen somename:8080;
    91. # server_name somename alias another.alias;
    92. #
    93. #server {
    94. # listen 8000;
    95. # listen somename:8080;
    96. # server_name somename alias another.alias;
    97. # location / {
    98. # root html;
    99. # index index.html index.htm;
    100. # }
    101. #}
    102. # HTTPS server
    103. #将ssl证书绑定到www.yz1999.top上
    104. }

    重新加载一下配置文件

    我们去访问一下,这里输入IP也能自动跳转到域名访问

    发现红框内变成安全图标,到此就完成配置了。

  • 相关阅读:
    dbeaver连接国产数据库
    游戏模拟——Position based dynamics
    Angular 指令介绍及使用(三)
    软件测试/测试开发丨Postman实战练习 学习笔记
    C++ STL 容器简单讲解
    C++面试八股文:用过std::set/std::map吗?
    excel中的引用与查找函数篇3
    SpringBootConfiguration注解的简介说明
    python 基于PHP+MySQL的学生成绩管理系统
    ARM架构与编程 · 基于IMX6ULL
  • 原文地址:https://blog.csdn.net/qq_57377057/article/details/126646333