day01
1.nginx基本介绍
2.nginx优势
3.nginx应用场景
1.nginx web服务
2.nginx反向代理
3.nginx反向代理–>负载均衡
4.nginx反向代理–>缓存
5.nginx 静态加速
6.nginx动静分离
7.nginx 全栈https
4.nginx安装
1.源码安装 <--不考虑
2.epel源安装 <--不考虑,配置乱 容易出问题
3.官方源安装 <--极其推荐
[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
module_hotfixes=true
[root@web01 ~]# yum install nginx -y
5.nginx配置文件
(进程能够以什么样的方式访问一个文件, 取决运行这个进程的用户身份对这个文件拥有什么样的权限)
[root@web01 ~]# cat /etc/nginx/nginx.conf
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; #返回的具体资源名称
}
}
http <---负责请求与响应
* server1 <---定义一个具体的网站
* location <---根据用户请求的uri 进行匹配
* location2
* location3
* server2
* location <---根据用户请求的uri 进行匹配
* location2
* location3
* server....
6.nginx运行一个网站
1.注释默认的配置, 新增一个网站的server配置 域名
[root@web01 conf.d]# gzip default.conf #压缩, 注释
[root@web01 conf.d]# zcat default.conf.gz #查看内容
[root@web01 conf.d]# cat game.oldxu.com.conf
server {
listen 80;
server_name game.oldxu.com;
location / {
root /code/;
index index.html;
}
}
[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
[root@web01 code]# systemctl restart nginx
2.准备好代码的仓库位置
[root@web01 conf.d]# mkdir /code
[root@web01 conf.d]# cd /code/
3.上传代码
[root@web01 code]# git clone https://gitee.com/linex/battlecity.git
[root@web01 code]# mv battlecity/* ./
4.访问测试 配置WIndows Host劫持
不要使用IE QQ浏览器
google firefox
7.如何新增一个网站:
1.新增一个nginx的配置文件
[root@web01 code2]# cat /etc/nginx/conf.d/gd.oldxu.com.conf
server {
listen 80;
server_name gd.oldxu.com;
location / {
root /code2;
index index.html;
}
}
[root@web01 code2]# nginx -t
[root@web01 code2]# systemctl restart nginx
2.创建代码存放路径
[root@web01 ~]# mkdir -p /code2
[root@web01 ~]# cd /code2
3.上传游戏
rz
4.配置域名劫持
8.在新增一个html的站点 : html.oldxu.com
9.访问流程
请求的url: http://html.oldxu.com /game/jianren/index.html
真实映射服务器的路径: /code3/game/jianren/index.html
10.nginx虚拟主机
虚拟主机 ---> 在一台服务器上运行多个网站
虚拟主机实现方式有三种:
基于IP <--几乎不怎么使用
基于端口 <--
基于域名
1.基于IP 10.0.0.7 172.16.1.7
目的:
访问10.0.0.7: 10-wan
访问172.16.1.7: 172-lan
实现方式:
[root@web01 conf.d]# cat ip.oldxu.com.conf
server {
listen 10.0.0.7:80;
location / {
root /code/wan;
index index.html;
}
}
server {
listen 172.16.1.7:80;
location / {
root /code/lan;
index index.html;
}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# mkdir /code/{wan,lan} -p
[root@web01 conf.d]# echo "10-wan" > /code/wan/index.html
[root@web01 conf.d]# echo "172-lan" > /code/lan/index.html
#curl获取url的资源
[root@web01 ~]# curl http://10.0.0.7
10-wan
[root@web01 ~]# curl http://172.16.1.7
172-lan
2.基于Port 10.0.0.7 8080 10.0.0.7 8081
场景:
1.内网环境,没有域名, 通过多个端口来区分不同的项目
目的:
访问10.0.0.7 8080 : 8080 over
访问10.0.0.7 8081 : 8081 over
实现:
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# > ip.oldxu.com.conf
[root@web01 conf.d]# cat /etc/nginx/conf.d/ip.oldxu.com.conf
server {
listen 8080;
location / {
root /code/8080;
index index.html;
}
}
server {
listen 8081;
location / {
root /code/8081;
index index.html;
}
}
[root@web01 conf.d]# mkdir /code/{8080,8081} -p
[root@web01 conf.d]# echo "over 8080" > /code/8080/index.html
[root@web01 conf.d]# echo "over 8081" > /code/8081/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
测试
[root@web01 conf.d]# curl http://10.0.0.7:8080
over 8080
[root@web01 conf.d]# curl http://10.0.0.7:8081
over 8081
基于域名: 必须用!
1.同一个IP,多个域名 Host: html.oldxu.com
一个文件对应一个网站 每个网站中的域名都不一样
[root@web01 conf.d]# cat game.oldxu.com.conf
server {
listen 80;
server_name game.oldxu.com;
location / {
root /code;
index index.html;
}
}
[root@web01 conf.d]# cat html.oldxu.com.conf
server {
listen 80;
server_name html.oldxu.com;
location / {
root /code3;
index index.html;
}
}