云服务器 centOS 7 +Nginx+HTTPS配置

CentOS上安装Nginx

安装依赖

yum -y install gcc gcc-c++ wget net-tools pcre-devel zlib-devel openssl-devel

如果报错的话,可能要清理一下缓存以及重新建立缓存

rm -f /var/lib/rpm/__db*
rpm --rebuilddb

然后下载Nginx安装包

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.7.12.tar.gz
tar zxvf nginx-1.7.12.tar.gz
cd nginx-1.7.12
./configure --prefix=/usr/local/nginx --with-threads --with-file-aio --with-ipv6 --with-pcre --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-mail --with-mail_ssl_module
make & make install
/usr/local/nginx/sbin/nginx

安装完设置开机自启

vim /etc/init.d/nginx
chmod a+x /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
service nginx restart
chkconfig nginx on

这时候打开网页,http://ip地址就可以访问了
在这里插入图片描述

配置https

在nginx下新建文件夹cert,用于存放ssl证书。

mkdir cert
#这是可以将下载的ssl证书放在该目录下,可以使用winSCp
#接下来,修改配置文件
vi /usr/local/nginx/conf/nginx.conf
#修改配置文件
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
  server {
  #监听443端口
    listen 443;
    #你的域名
    server_name 域名; 
    ssl on;
    #ssl证书的pem文件路径
    ssl_certificate  xx.crt;
    #ssl证书的key文件路径
    ssl_certificate_key xx.key;
}
server {
    listen 80;
    server_name 域名;
    #将请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}
}

这时候使用命名检查配置文件是否正确

/usr/local/nginx/sbin/nginx -t

如果结果是这样的,配置就是正确
在这里插入图片描述
如果报错,没有event上下文错,需要设置默认

events {
  worker_connections  1024;  ## Default: 1024
} 

接下来重启nginx

/usr/local/nginx/sbin/nginx -s reload

这时候打开网页,输入https://域名,如果是这样的,说明配置成功
在这里插入图片描述
如果还是加载不出来,可能是80端口和443端口被占用或者没有开放。开放端口并重启防火墙

systemctl unmask firewalld.service
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
systemctl stop firewalld.service
systemctl start firewalld.service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值