#nginx
nginx是一个开源且高性能、可靠的HTTP Web服务、代理服务。
Nginx和Apache的区别
Nginx
image.png
Apache
image.png
Nginx应用场景
image.png
Nginx架构
LNMP Linux Nginx MySQL PHP Python
LNMT Linux Nginx MySQL Tomcat Java
Nginx安装
1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.便于管理)
2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读)
配置官方yum源
vim/etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum install - y nginx
Nginx配置文件相关
/etc/nginx/nginx.conf #这是nginx的主配置文件
/etc/nginx/conf.d/default.conf #也是nginx的主配置文件,是nginx.conf中的一部分。
/var/log/nginx/access.log #正常访问nginx的日志
/var/log/nginx/error.log #访问nginx的错误日志
nginx配置详解
[root@web01 nginx]# cat nginx.conf
user nginx; # nginx的启动用户
worker_processes 1; # work工作进程的数量 默认1 工作中配置为和核心数量相同
error_log /var/log/nginx/error.log warn; # 错误日志的位置
pid /var/run/nginx.pid; # 服务的进程PID
-----------------------事件模块---------------------------------------------
events {
worker_connections 1024; # 每个worker最大的连接数 默认1024 65535
}
-----------------------HTTP区块----------------------------------------------
http {
include /etc/nginx/mime.types; # 媒体类型
default_type application/octet-stream; # 如上面的文件中没有请求的类型 默认为下载
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; # 访问日志
sendfile on; # 高效传输
#tcp_nopush on; # 高效传输
keepalive_timeout 65; # 长连接的超时时间
#gzip on; #是否压缩
include /etc/nginx/conf.d/*.conf; # 包含.conf文件
}
[root@web01 nginx]# cat /etc/nginx/conf.d/default.conf
[root@web01 nginx]# cat /etc/nginx/conf.d/default.conf
-----------------------------------server区块-------------------
server {
listen 80; # 监听的端口80
server_name localhost; # nginx的域名
location / { # 路径 匹配规则
root /usr/share/nginx/html; # 规定了用户默认访问我的目录是哪个
index index.html index.htm; # 默认返回给用户的类型
}
目前所用配置优化
vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
启动Nginx
-直接输入Nginx启动
[root@web01 ~]# nginx -s stop # 停止Nginx服务
[root@web01 ~]# nginx -s reload # 重新加载
-使用systemctl启动
[root@web01 ~]# systemctl start nginx # 启动Nginx
[root@web01 ~]# systemctl enable nginx # 开机自启动
[root@web01 ~]# systemctl status nginx # 查看状态
[root@web01 ~]# systemctl restart nginx # 重新启动
[root@web01 ~]# systemctl reload nginx # 重新加载
只能使用一种方式运行, 不能混合使用(ex:使用Nginx启动后,不能使用systemctl关闭)
检查Nginx配置是否正常
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful