(一 )nginx简介及安装
1、nginx简介
nginx是一款轻量级的web服务器,反向代理服务器,具有内存占用少,启动速度快,高并发能力强等优点,被广泛使用在互联网项目中。
2、nginx安装
环境准备:
centos主机,关闭防火墙;
sudo systemctl stop firewalld
sudo systemctl disable firewalld
使用yum安装:
sudo yum install yum-utils net-tools
配置nginx仓库:
cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
查看配置的仓库:yum repolist
开始安装nginx:
yum install nginx
验证安装:
通过 nginx
命令启动,可以访问80端口 。
nginx日志存放在 var/log/nginx
目录下,有问题查看日志时可在此目录下进行。
tail -f error.log
3、常用命令
nginx
nginx -s stop # 立即停止
nginx -s quit # 执行完当前请求再停止
nginx -s reload # 重新加载配置文件,相当于restart
nginx -s reopen # 将日志写入一个新的文件
nginx -t #测试配置文件
日志位于/var/log/nginx/
4、启动、停止、重新加载nginx服务
在centos 7中,用systemctl启动nginx可能出现如下错误,
nginx: [emerg] bind() to 0.0.0.0:8000 failed (13: Permission denied)
这是由于selinux的安全策略引起的。解决方法如下:
- setenforce 0 (临时)
- 修改/etc/selinux/config,设置SELINUX=disabled (永久有效,需重启)
systemctl start nginx
systemctl status nginx
#查看日志
journalctl -xe
systemctl stop nginx
systemctl reload nginx
#配置开机启动
systemctl enable nginx
5 配置文件
nginx配置文件位于 /etc/nginx/nginx.conf
,下列命令会引用 /etc/nginx/conf.d
目录下所有 .conf
文件,这样可以保持主配置文件的简介,同时配置多个.conf
文件方便区分,增加可读性.
include /etc/nginx/conf.d/*.conf;
默认配置: /etc/nginx/conf.d/default.conf
server {
listen 80; # 监听端口
server_name localhost;
location / {
root /usr/share/nginx/html; # 根目录
index index.html index2.html; #首页,多个可通过空格隔开
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
6 配置文件结构
http {
server { # 虚拟主机
location {
listen 80;
server_name loaclhost;
}
location {
}
}
server {
}
}