src=http___hbimg.b0.upaiyun.com_44943051dbade219c6264aceee12d7f3974268dc79f7-4xdMZj_fw658&refer=http___hbimg.b0.upaiyun.webp

1.安装Nginx

首先安装依赖包

  1. yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

然后进入Nginx官网下载Nginx(http://nginx.org/download/nginx-1.18.0.tar.gz),利用FTP工具上传到

/usr/local目录,然后解压

  1. cd /usr/local
  2. tar -xvf nginx-1.18.0.tar.gz
  3. cd /usr/local/nginx

重点配置SSL模块

  1. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

然后编译

  1. make
  2. make install

然后运行Nginx,执行命令nginx -V,查看是否安装成功

  1. [root@VM-4-15-centos conf]# nginx -V
  2. nginx version: nginx/1.18.0
  3. built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
  4. built with OpenSSL 1.1.1k FIPS 25 Mar 2021
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

此时你的Nginx已支持SSL模块

注意:如若你已经安装过了Nginx,则只需要执行配置SSL模块后,只make就行,如果执行了make install会覆盖你的Nginx

2.配置文件

首先去阿里云申请一个Nginx的SSL证书再进行下面的操作

  1. server {
  2. listen 443 ssl;
  3. server_name your_domain; #要设置的域名;
  4. root html;
  5. index index.html index.htm;
  6. ssl_certificate /usr/local/nginx/conf/cert/3478976_abc.baidu.com.pem; #后缀的证书位置
  7. ssl_certificate_key /usr/local/nginx/conf/cert/3478976_abc.baidu.com.key; #后缀的证书位置
  8. ssl_session_timeout 5m;
  9. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  10. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  11. ssl_prefer_server_ciphers on;
  12. location ~*^.+$ {
  13. proxy_redirect off;
  14. proxy_set_header Host $host;
  15. proxy_set_header X-real-ip $remote_addr;
  16. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  17. }
  18. }
  19. #可选配置,配置http重定向到https
  20. server
  21. {
  22. listen 80;
  23. server_name your_domain; #要设置的域名;
  24. rewrite ^(.*)$ https://$host$1 permanent; #将所有http请求通过rewrite重定向到https。
  25. location ~*^.+$ {
  26. proxy_redirect off;
  27. proxy_set_header Host $host;
  28. proxy_set_header X-real-ip $remote_addr;
  29. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  30. }
  31. }

然后访问域名即可~