1.安装Nginx
首先安装依赖包
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目录,然后解压
cd /usr/local
tar -xvf nginx-1.18.0.tar.gz
cd /usr/local/nginx
重点配置SSL模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
然后编译
make
make install
然后运行Nginx,执行命令nginx -V,查看是否安装成功
[root@VM-4-15-centos conf]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
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证书再进行下面的操作
server {
listen 443 ssl;
server_name your_domain; #要设置的域名;
root html;
index index.html index.htm;
ssl_certificate /usr/local/nginx/conf/cert/3478976_abc.baidu.com.pem; #后缀的证书位置
ssl_certificate_key /usr/local/nginx/conf/cert/3478976_abc.baidu.com.key; #后缀的证书位置
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location ~*^.+$ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#可选配置,配置http重定向到https
server
{
listen 80;
server_name your_domain; #要设置的域名;
rewrite ^(.*)$ https://$host$1 permanent; #将所有http请求通过rewrite重定向到https。
location ~*^.+$ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然后访问域名即可~