一、nginx基本介绍
1.1:什么是Nginx
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源、体现在直接获取Nginx的源代码
高性能,体现在支持海量的并发
高可靠,体现在服务稳定
1.2:为什么选择Nginx
- 高性能、高并发
- 高扩展性
- 高可靠性
- 热部署
- Nginx使用Epool网络模型
- 其次使用Nginx统一技术栈,降低维护城堡,同时降低技术更新城堡
1.3:Nginx有哪些使用场景
Nginx的主要使用场景我归纳为三个,分别是静态资源服务、代理资源服务、安全服务。
1、nginx web服务
2、nginx反向代理
3、nginx反向代理-->负载均衡
4、nginx反向代理-->缓存
5、nginx静态加速
6、nginx动静分离
7、nginx全栈https
二、Nginx安装部署
1、配置Nginx官方源(推荐)
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[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
[root@web01 ~]# yum install nginx -y
2、nginx运行一个网站
[root@web01 conf.d]# gzip default.conf
[root@web01 conf.d]# cat test.com.conf
server {
listen 80;
server_name test.com;
location / {
root /code;
index index.html;
}
}
3、语法检查
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4、上传代码
[root@web01 code]# git clone https://gitee.com/linex/battlecity.git
[root@web01 code]# mv battlecity/* ./
5、重启服务
[root@web01 code]# systemctl restart nginx
6、访问测试
注意:使用域名请配置Windows Host劫持,否则无法访问自己配置的域名(host文件位置百度)
三、nginx配置文件详解
user nginx; # Nginx进程的运行用户身份
worker_processes 1; # Nginx运行的worker进程数
error_log /var/log/nginx/error.log warn; # Nginx错误日志存放的路径
pid /var/run/nginx.pid; # Nginx进程运行的PID号
events {
worker_connections 1024; # 每个worker进程能接受的最大连接数
use epoll;
}
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"';
#访问日志的路径 访问日志的存储格式是main格式, main格式在log_format中进行定义
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
#server<--定义网站
server {
listen 80; # 监听80端口
server_name localhost; # 网站的域名
location / { # 匹配网站的uri
root /usr/share/nginx/html; # 返回资源的具体路径
index index.html index.htm; # 返回的具体资源名称
}
}