Nginx基础知识
1.什么是Nginx?
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
Apache和Nginx的区别:
Apache它是同步阻塞型
Nginx它是异步非阻塞型
同步和异步的区别?
同步就是烧开水,需要自己去轮询(每隔一段时间去看看水开了没),异步就是水开了,然后水壶会通知你水已经开了,你可以回来处理这些开水了。
同步和异步是相对于操作结果来说,会不会等带结果返回。
什么是阻塞和非阻塞?
阻塞就是说在煮水的过程中,你不可以去干其他的事情,非阻塞就是在同样的情况下,可以同时去干其他的事情。
阻塞和非阻塞是相对于线程是否被阻塞。
2.常见的Web服务器
常用的web服务器有httpd、nginx、tengine和openresty
httpd是由Apache研发的Web服务器项目
tengine是由淘宝网发起的Web服务器项目
3.Nginx主要应用场景:
反向代理
负载均衡
代理缓存 (proxy_cache)
静态资源
动静分离
Https安全
冰山模型中的一角 ---> 还有很多个使用场景
Nginx安装与配置
1.Nginx安装
源码安装:较难实现标准化,升级繁琐,不推荐。
epel仓库:nginx的版本较低,配置不易读。
官方仓库:nignx的版本较新,配置易读,维护简单。
1.配置官方仓库源
[root@web01 ~]# cat /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
2.使用yum直接安装
[root@web01 ~]# yum install nginx -y
2.Nginx启动
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx #开机自启
3.Nginx相关配置文件 了解
为什么访问nginx启动的网页,点文件就会下载?
因为这个文件的类型不属于/etc/nginx/mime.types这个目录里的文件类型
3.1 Nginx主配置文件
路径 | 类别 | 作用 |
---|---|---|
/etc/nginx/nginx.conf | 配置文件 | nginx主配置文件 |
/etc/nginx/conf.d/default.conf | 配置文件 | 默认网站配置文件 |
3.2 Nginx代理相关参数文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置文件 | Fastcgi代理配置文件 |
/etc/nginx/scgi_params | 配置文件 | scgi代理配置文件 |
/etc/nginx/uwsgi_params | 配置文件 | uwsgi代理撇脂文件 |
3.3 Nginx日志相关目录与文件
路径 | 类型 | 作用 |
---|---|---|
/var/log/nginx | 目录 | Nginx默认存放日志目录 |
/etc/logrotate.d/nginx | 配置文件 | Nginx默认日志切割 |
3.4 Nginx管理相关命令
路径 | 类型 | 作用 |
---|---|---|
/usr/sbin/nginx | 命令 | Nginx命令行管理终端工具 |
/usr/sbin/nginx-debug | 命令 | Nginx命令行与终端调试工具 |
4.Nginx主配置文件
cat /etc/nginx/nginx.conf
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx; # nginx进程的用户身份
worker_processes 1; # nginx的工作进程数量
error_log /var/log/nginx/error.log warn; # 错误日志的路径 [警告级别才会记录]
pid /var/run/nginx.pid; # 进程运行后,会产生一个pid
events { # 事件模型
worker_connections 1024; # 每个work能够支持的连接数
use epoll; # 使用epoll网络模型
}
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; #启用压缩功能
----------这段是配置网站的文件,可以单独写一个配置文件,不需要再nginx主配置文件中写-----
#使用Server配置网站, 每个Server{}代表一个网站
server {
listen 80;
server_name test.oldxu.com;
location / { #控制网站访问的路径
root ...;
}
}
-----------------------------------------------------------------------------
include /etc/nginx/conf.d/*.conf; #该配置文件包含该目录下以.conf结尾的所有配置文件
}
Nginx搭建游戏网站
Nginx中的http、server和location之间的关系是?
http:标签主要用来解决用户的请求与响应。
server:标签主要用来响应具体的某一个网站。
location :标签主要用于匹配网站具体url路径。
http{} 层下允许有多个Server{},可以有多个网站.
一个Server{} 下又允许有多个location{} 每个网站的uri路径不同,所以要分别进行匹配.
多个域名可以基于一个IP地址上搭建Nginx
配置完nginx后一定要语法检测!!nginx -t
1.基于多域名的方式(常用)
1.注释掉nginx默认的网站
[root@web01 ~]# gzip /etc/nginx/conf.d/default.conf
2.编写游戏网站nginx配置文件
[root@web01 /etc/nginx/conf.d]# vim game.wang.com.conf #创建配置文件
server {
listen 80; #该网站提供访问的端口
server_name game.wang.com; #访问该网站的域名
location / { #定位,控制网站访问的路径
root /tmp/code; #访问该网站指向本地的哪个文件
index index.html; #网页
}
}
[root@web01 /etc/nginx/conf.d]# vim game.ze.com.conf
server{
listen 80;
server_name game.ze.com;
location / {
root /tmp/tk/phaser_battkeCity;
index index.html;
}
}
3.根据Nginx的配置文件,初始化环境
[root@web01 /etc/nginx/conf.d]# mkdir /tmp/code
[root@web01 /etc/nginx/conf.d]# mkdir /tmp/tk
4.上传代码
[root@web01 /etc/nginx/conf.d]# cd /tmp/code/
[root@web01 /tmp/code]# rz html5.zip
[root@web01 /tmp/code]# unzip html5.zip
[root@web01 /tmp/tk]# rz tk.zip
[root@web01 /tmp/tk]# unzip tk.zip
5.检测语法
[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
6.重启服务
[root@web01 ~]# systemctl restart nginx
7.Windows配置劫持ip地址(这是虚拟的网址,所以为了实验做本地劫持,在浏览器输入虚拟的网址来访问)
C:\Windows\System32\drivers\etc
10.0.0.7 game.ze.com
10.0.0.7 game.wang.com
2.基于主机多IP地址的方式
1.编写配置文件
[root@web01 /etc/nginx/conf.d]# cat ip_eth0.conf
server {
listen 10.0.0.7:80;
location / {
root /ip1;
index index.html;
}
}
[root@web01 /etc/nginx/conf.d]# cat ip_eth1.conf
server {
listen 172.16.1.7:80;
location / {
root /ip2;
index index.html;
}
}
2.根据配置文件初始化环境
[root@web01 conf.d]# mkdir /ip1 /ip2
[root@web01 conf.d]# echo "10...." > /ip1/index.html
[root@web01 conf.d]# echo "172...." > /ip2/index.html
3.重启nginx服务
[root@web01 conf.d]# systemctl restart nginx
4.测试访问
[root@web01 ~]# curl http://10.0.0.7
10....
[root@web01 ~]# curl http://172.16.1.7
172....
3.基于端口的配置方式
应用场景:公司内部有多套系统,希望部署在一台服务器上,而内网又没有域名。所以,我们可以通过相同IP,不同的端口,访问不同的网站页面。
1.创建配置文件
[root@web01 conf.d]# cat port1.conf
server {
listen 81;
location / {
root /81;
index index.html;
}
}
[root@web01 conf.d]# cat port2.conf
server {
listen 82;
location / {
root /82;
index index.html;
}
}
2.根据初始化环境配至
[root@web01 conf.d]# mkdir /81 /82
[root@web01 conf.d]# echo "81" > /81/index.html
[root@web01 conf.d]# echo "82" > /82/index.html
3.重启服务
[root@web01 conf.d]# systemctl restart nginx
4.测试访问
[root@web01 ~]# curl http://10.0.0.7:81
81
[root@web01 ~]# curl http://10.0.0.7:82
82
Nginx访问的整体流程
http:// game.oldxu.com / game/yibihua/index.html
请求的uri: /game/yibihua/index.html
真实映射位置: /code/game/yibihua/index.html
同步和异步的
同步
异步
阻塞
阻塞就是说在煮水的过程中,你不可以去干其他的事情,非阻塞就是在同样的情况下,可以同时去干其他的事情。阻塞和非阻塞是相对于线程是否被阻塞。
非阻塞
同步阻塞
同步非阻塞
异步阻塞
异步非阻塞