默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、安装Nginx
通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx。
sudo yum install -y nginx
3、启动Nginx并设置开机自动运行
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
4、浏览查看效果
在浏览器中输入您的服务器地址:服务ip
若想使用外部主机连接上虚拟机访问端口192.168.131.2,需要关闭虚拟机的防火墙:
centOS7关闭防火墙命令: systemctl stop firewalld.service
# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
#启动
systemctl start nginx
#关闭
systemctl stop nginx
#查看状态
systemctl status nginx
#重启
systemctl restart nginx
#开机自启动
systemctl enable nginx
4、Nginx配置信息
网站文件默认存放目录
/usr/share/nginx/html
网站默认站点配置
/etc/nginx/conf.d/default.conf
自定义站点配置文件存放目录
/etc/nginx/cond.d
Nginx全局配置
/etc/nginx/nginx.conf
Nginx启动
nginx -c nginx.conf
5、配置HTTPS
证书可以自行到阿里云或者腾讯云申请免费的证书,并下载对应Nginx的证书。
编辑 /etc/nginx/nginx.conf 文件
在http代码块里添加如下配置:
server {
listen 443;
server_name xxx.com;
ssl on;
ssl_certificate cert/a.pem; #证书文件路径
ssl_certificate_key cert/a.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_pass http://127.0.0.1:8080;
}
#配置后台业务路径(根据自身需求进行配置)
location /houtai {
alias /usr/tomcat/webapps/houtai/;
index index.html index.htm;
}
}
server {
listen 80;
server_name xxx.com; #你的域名
rewrite ^(.*)$ https://$host$1 permanent; #把http的域名请求转成https
}
原文:https://www.cnblogs.com/jianer/p/12883180.html