nginx使用与入门
参考:https://docs.nginx.com/nginx/admin-guide/basic-functionality/runtime-control/
https://github.com/dunwu/nginx-tutorial
1. nginx部署与安装
此部分暂时省略,请详细阅读官方文档,如有需要后续会补充。
2. nginx.conf模块配置
配置文件为分块结构,详细指令后接对应参数,参数后会有一个分号结尾。通用结构大体如下:
1 | user nobody; # a directive in the 'main' context |
#日志配置
#Context
顶级指令有四个,分别是:
- events:通用连接处理
- http:http流量
- mail:邮件流量
- stream:TCP和UDP流量
#events
event{
#新连接进来是是否唤醒所有线程,false则唤醒,true则轮流处理
accept_mutex on|off
#如果accept_mutex未开启,限定一个工作者等待最长时间
accept_mutex_delay time
设置debug地址
debug_connection 127.0.0.1
#一个工作者某时刻接收一个新连接还是所有的新连接
multi_accept on|off
#指定连接处理方法,通常不用显示指定,nginx会自动选择最高效的
user select|poll|epoll|kqueue
#在使用epoll的AIO模式的时候,为单个工作进程设置最大的异步IO数
work_aio_requests number
#单个进程可以打开的最大同步连接数
work_connections 512
}
因为events设置数目较少,后面几个配置项比较多,就跟着其他部分一起了解。
#http
#指定谁可以启动nginx,nobody代表所有用户都可以
user nobody;
#取决于CPU数目和存储数据的硬盘驱动数,一般CPU是几核就写几
worker_processes number|auto;
#指定错误日志位置,还可以在http/server/location块中配置
error_log logs/error.log notice;
#引入配置文件
include file;
#指定pid存放的位置,有时候需要停止nginx需要pid来杀死进程。
pid file;
#通用连接指令
event{
#新连接进来是是否唤醒所有进程,false则唤醒,true则轮流处理
accept_mutex on|off
#如果accept_mutex未开启,限定一个进程等待最长时间
accept_mutex_delay time
设置debug地址
debug_connection 127.0.0.1
#一个进程某时刻接收一个新连接还是所有的新连接
multi_accept on|off
#指定连接处理方法,通常不用显示指定,nginx会自动选择最高效的
user select|poll|epoll|kqueue
#在使用epoll的AIO模式的时候,为单个工作进程设置最大的异步IO数
work_aio_requests number
#单个进程可以打开的最大同步连接数
work_connections 512
}
http{
#配置日志的格式,参数都是内置参数
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#sendfile是一种函数调用,直接在两个文件描述符之间传输数据(内核操作),避免了数据在内核缓冲区和用户缓冲区之间的拷贝,称之为零拷贝,开启可提高性能。
sendfile on;
keepalive_timeout 65;
#影响meme-type散列表的冲突率
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 1024M;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#访问 /last.html 的时候,页面内容重写到 /index.html 中,并继续后面的匹配,浏览器地址栏URL地址不变
#https://www.jianshu.com/p/10ecc107b5ee(rewrite和proxy_server区别)
rewrite /last.html /index.html last;
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ / last;
}
location /user_manage {
proxy_pass http://localhost:7777;
}
location /application_access {
proxy_pass http://localhost:9090;
}
location /devices {
proxy_pass http://localhost:8088/;
}
location /kuaijian {
proxy_read_timeout 7200s;
proxy_pass http://192.168.199.174:8989/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
3. Http负载均衡(7层负载均衡)
基本使用
http {
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server 192.0.0.1 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
}
选择负载均衡方法
1. Round Robin(轮询)
请求被顺序分发到不同的后端、可以设置权重增加某个后端被分配的概率。
upstream backend {
# no load balancing method is specified for Round Robin
server backend1.example.com;
server backend2.example.com;
}
2. Least Connections(最少连接数)
请求将被发送到活跃连接数最少的服务器上,依然可以使用权重。
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
3. IP hash
计算客户端IP的散列值,将不同IP的客户端散列到不同的后端,在分布式Session中常用到IP_hash,但是这种方式不适合服务器的动态伸缩的问题,所以用更好的一致性hash替代。
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
4. 通用hash
由用户自己决定将请求发送到哪个uri
upstream backend {
hash $request_uri consistent;
server backend1.example.com;
server backend2.example.com;
}
5. least time(最少连接时长)
从字面意思看,就是Nginx会选择相应时间最短的server来服务,定义响应时间最短nginx给了三个选项
- header:接收来自Server的第一个字节的时长。
- last_byte:接收完整报文的时长。
last_byte inflight:接收完整报文的时长,但是考虑不完整的请求。
upstream backend { least_time header; server backend1.example.com; server backend2.example.com;}
6. 随机
upstream backend {
random two least_time=last_byte;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server backend4.example.com;
}
4. TCP和UDP负载均衡(4层负载均衡)
简单理解四层负载均衡和七层负载均衡的区别:所谓四层就是基于IP+端口的负载均衡;七层就是基于URL等应用层信息的负载均衡;同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡。 换句换说,二层负载均衡会通过一个虚拟MAC地址接收请求,然后再分配到真实的MAC地址;三层负载均衡会通过一个虚拟IP地址接收请求,然后再分配到真实的IP地址;四层通过虚拟IP+端口接收请求,然后再分配到真实的服务器;七层通过虚拟的URL或主机名接收请求,然后再分配到真实的服务器。
所谓的四到七层负载均衡,就是在对后台的服务器进行负载均衡时,依据四层的信息或七层的信息来决定怎么样转发流量。 比如四层的负载均衡,就是通过发布三层的IP地址(VIP),然后加四层的端口号,来决定哪些流量需要做负载均衡,对需要处理的流量进行NAT处理,转发至后台服务器,并记录下这个TCP或者UDP的流量是由哪台服务器处理的,后续这个连接的所有流量都同样转发到同一台服务器处理。七层的负载均衡,就是在四层的基础上(没有四层是绝对不可能有七层的),再考虑应用层的特征,比如同一个Web服务器的负载均衡,除了根据VIP加80端口辨别是否需要处理的流量,还可根据七层的URL、浏览器类别、语言来决定是否要进行负载均衡。举个例子,如果你的Web服务器分成两组,一组是中文语言的,一组是英文语言的,那么七层负载均衡就可以当用户来访问你的域名时,自动辨别用户语言,然后选择对应的语言服务器组进行负载均衡处理。
配置使用
#stream模块配置TCP和UDP流量
stream{
server{
listen 12345;
proxy_pass stream_backend;
}
server{
#监听UDP需要加上udp参数
listen 12346 udp;
}
upstream stream_backend{
#最少连接时长
least_time first_byte;
server 127.0.0.1:12345;
}
}
5. nginx基础命令
- nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
- nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
- nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。
- nginx -s reopen 重新打开日志文件。
- nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。
- nginx -t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
- nginx -v 显示 nginx 的版本。
- nginx -V 显示 nginx 的版本,编译器版本和配置参数。
6. nginx常用配置
1. nginx负载均衡配置
上面基本已经介绍了,略过。。
2. 配置http反向代理
#运行用户
#user somebody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志
error_log D:/Tools/nginx-1.10.1/logs/error.log;
error_log D:/Tools/nginx-1.10.1/logs/notice.log notice;
error_log D:/Tools/nginx-1.10.1/logs/info.log info;
#PID文件,记录当前启动的nginx的进程ID
pid D:/Tools/nginx-1.10.1/logs/nginx.pid;
#工作模式及连接数上限
events {
worker_connections 1024; #单个后台worker process进程的最大并发链接数
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型(邮件支持类型),类型由mime.types文件定义
include D:/Tools/nginx-1.10.1/conf/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 D:/Tools/nginx-1.10.1/logs/access.log main;
rewrite_log on;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
keepalive_timeout 120;
tcp_nodelay on;
#gzip压缩开关,。压缩传输的字节大小
gzip on;
#设定实际的服务器列表
upstream zp_server1{
server 127.0.0.1:8089;
}
#HTTP服务器
server {
#监听80端口,80端口是知名端口号,用于HTTP协议
listen 80;
#定义使用www.xx.com访问
server_name www.helloworld.com;
#首页
index index.html
#指向webapp的目录
root D:\01_Workspace\Project\github\zp\SpringNotes\spring-security\spring-shiro\src\main\webapp;
#编码格式
charset utf-8;
#代理配置参数
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr;
#反向代理的路径(和upstream绑定),location 后面设置映射的路径
location / {
proxy_pass http://zp_server1;
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root D:\01_Workspace\Project\github\zp\SpringNotes\spring-security\spring-shiro\src\main\webapp\views;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
}
#错误处理页面(可选择性配置)
#error_page 404 /404.html;
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
}
}
3. 配置文件服务器
autoindex on;# 显示目录
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on;# 显示文件时间
server {
charset utf-8,gbk; # windows 服务器下设置后,依然乱码,暂时无解
listen 9050 default_server;
server_name _;
#设置根目录
root /share/fs;
}
4. 前后端静态分离
http{
upstream backend {
server 127.0.0.1:9040;
}
server {
listen 80;
server_name www.demo04.com;
# windows 下前后端分离场景,使用相对路径无法被识别,暂时搞不定
root D:/Codes/ZP/Others/nginx-tutorial/demos/reactapp/dist;
#响应后台请求
location ~ ^/api/ {
proxy_pass http://backend;
#重写请求uri格式
rewrite "^/api/(.*)$" /$1 break;
}
location ~* \. (json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {}
}
}
5. 搭建静态站点服务器
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip开启可以压缩字节大小
gzip on;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png;
gzip_vary on;
server {
listen 80;
server_name static.zp.cn;
location / {
root /app/dist;
index index.html;
#转发任何请求到 index.html
}
}
}