因为购买了域名,学习需要,所以学习了nginx
nginx
常用简单命令
./nginx #启动
./nginx -s stop #快速停止
./nginx -s quit #在退出前,完成已经接受的连接请求
./nginx -s reload #重载
目录说明
nginx基础配置 - 基于注释方便学习
worker_processes 1; # 默认1 : 工作进程个数
events {
worker_connections 1024; # 一个worker可以多少个连接
}
http {
include mime.types; # 引入别的配置文件
### 这个mime.types:就是告诉浏览器他是什么类型
### 在当前目录看mime.types:
### 前面: 浏览器的Content-type 后面: 本地文件的后缀名
default_type application/octet-stream; # 默认文件格式,这个是下载流
sendfile on; # 数据0拷贝
keepalive_timeout 65;
# 虚拟主机 - vhost
server { # 一个server就是这个vhost的具体配置
listen 80; # 监听的端口
server_name localhost; # 配置 域名、主机名
# location之后是uri
# http://cyt.com/xxoo/index.html
# 后面的的 xxoo/index.html 就是uri : 资源定位符
# http://cyt.com/xxoo/index.html 这整个是URL
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
虚拟主机与域名解析 (ez)
下面就是一个虚拟主机( 一个完整的server就是一个虚拟主机 )
server { # 一个server就是这个vhost的具体配置
listen 80; # 监听的端口
server_name www.ctian-api.top; # 配置 域名、主机名
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx常用功能
- 多用户二级域名
- 短网址
- httpdns
- 即基于http的dns解析服务,而这个所谓的dns解析服务,其实就是一个nginx
- 一般适用于
C/S架构和APP
反向代理
关键字 - proxy_pass 效果: 浏览器url不会变,但是页面展示会变 例如
- 在这里
如果我访问192.168.80.100,
那么实际上我访问的是www.toutiao.com
只不过url是192.168.80.100,www.toutiao.com被反向代理了
server { # 一个server就是这个vhost的具体配置
listen 80; # 监听的端口
server_name localhost; # 配置 域名、主机名
location / {
proxy_pass http://www.toutiao.com;
# root html;
# index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
负载均衡 ( upstream与proxy_pass配合使用 )
配置使用
当然upstream里一般是别的服务器,这里是通过不同端口去模拟不同的服务器
upstream httpds{
server 192.168.80.100:81;
server 192.168.80.100:82;
}
server { # 一个server就是这个vhost的具体配置
listen 80; # 监听的端口
server_name localhost; # 配置 域名、主机名
location / {
proxy_pass http://httpds; # 这里就是反向代理
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
- 参数
- ⭐weight : 权重
- down : 宕机
- backup : 备份机,正常情况下不适用,只有全挂采用
负载均衡策略
- 轮询
下面四个都不用
- ip_hash 同一个ip转发到同一台服务器,可以保持会话
- least_connection
- url_hash 同一个url转向一台服务器,可以流量定向
- fair 后端响应时间少的优先转发过去
正则配置
参数介绍 - 开头的 ~ 表示 : 开始正则了 - ~后面的*表示 : 它不区分大小写
location ~*/(js|img|jpg){
# 这里配的是静态资源访问路径
root html;
index index.html index.htm;
}