一、概念
Nginx是基于异步框架的轻量级/高性能的Web 服务器/反向代理服务器/缓存服务器/电
子邮件(IMAP/POP3)代理服务器。
Nginx特点
- 优点:
高并发量:基于 epoll/kqueue 模型开发,支持高并发量,支持高达 5w 并发连接数的响应。
内存消耗少:善于处理静态文件,相较于其他web(比如:apache),占用更少的内存及资源。
简单稳定:配置简单(一个conf文件),运行简单(nginx命令),而且运行稳定。
模块化程度高:功能模块插件化设计,可以自由配置相应的功能。
支持Rwrite重写规则:能够根据域名、URL等请求关键点,实现定制化的高质量分发。
支持多系统:Nginx代码完全用C语言从头写成,可以在各系统上编译并使用。 - 缺点:
动态处理差:nginx善于处理静态文件,但是处理动态页面相较于Apache之类重量级的web软件能力稍欠缺。
rewrite弱:虽然nginx支持rewrite功能多,但是相较于Apache之类重量级的web软件能力稍欠缺。
二、docker安装nginx
vim nginx.yml
version: '3.3'
services:
nginx:
image: nginx:latest
container_name: mynginx
volumes:
- /nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /nginx/conf/conf.d:/etc/nginx/conf.d
- /nginx/log:/var/log/nginx
- /nginx/html:/usr/share/nginx/html
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
ports:
- "0.0.0.0:80:80"
restart: unless-stopped
privileged: true
logging:
driver: "json-file"
options:
max-size: "10m"
docker-compose -f nginx.yml up -d
三、配置文件详解
3.1、全局配置
user nginx; #设置使用用户
worker_processes auto; #并发连接数,跟cpu保持一致
error_log /var/log/nginx/error.log notice; #错误日志
pid /var/run/nginx.pid; #服务启动id
events { #定义事件相关属性
worker_connections 1024; #一个进程处理的最大连接数
}
http {
include /etc/nginx/mime.types; #文件拓展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
access_log /var/log/nginx/access.log main; #nginx访问日志
sendfile on; #开启高效文件传输模式
#tcp_nopush on; #防止网络阻塞
keepalive_timeout 65;#长连接超时时间
#gzip on; #gzip压缩输出
include /etc/nginx/conf.d/*.conf; #文件拓展名与文件类型映射表
}
server {
listen 80; #监听端口
listen [::]:80; #监听 0.0.0.0:80 全地址监听
server_name localhost; #主机名
location / {
root /usr/share/nginx/html; #:定义Server相应请求的html文件所在路径
index index.html index.htm; #定义响应请求后返回的文件名称或格式
allow 192.168.70.129; #允许访问地址
deny all; #禁止所有访问
expires 7d; #相应请求的文件过期时间
try_files $uri $uri/ =404; #如果root指定的路径下有查找文件,就返回,否则报404
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
3.2、location配置属性
location主要是根据Server匹配到的请求路径和关键字去响应和处理。如果匹配到多个,则使用location_match最长的
匹配规则
类型 | 匹配方式 | 样式 |
---|---|---|
~ or !~ or ~* or !~* | 正则符号 | location ~ .(jpg?g)$ {} |
~* | 正则符号 | location ~* .(jpg?g)$ {} |
=/路径 | 精确匹配 | location = /home {} |
^~ | 优先匹配 | location ^~ /home {} |
@ | 内部重定向 | location @backend {} |
/ | 通用匹配 | location / {} |
location = / {
#精确规则 A
}
location = /login {
#精确规则 B
}
location ^~ /static/ {
#优先规则 C
}
location ~ \.(gif|jpg|png|js|css)$ {
#正则规则 D
}
location ~* \.png$ {
#正则规则 E
}
location !~ \.xhtml$ {
#正则规则 F
}
location !~* \.xhtml$ {
#正则规则 G
}
location / {
#通用规则 H
}
访问根目录/, 比如http://a.com/ 将匹配规则A
访问 http://a.com/login 将匹配规则B
访问 http://a.com/static/a.html 将匹配规则C
访问 http://a.com/a.gif, http://a.com/b.png 规则D和E均适合,按顺序优先使用规则D,而 http://a.com/static/c.png 则优先匹配到规则C
访问 http://a.com/a.PNG 则匹配规则E,因为规则E不区分大小写。
访问 http://a.com/a.XHTML 使用规则F。
访问 http://a.com/category/id/1111
优先级:=/(精确匹配) > ^~(优先匹配) > 正则 > /(通用匹配)
重定向:@用来定义一个命名 location。主要用于内部重定向,不能用来处理正常的请求。其用法如下:
location / {
try_files $uri $uri/ @container
}
location @container{
}
root 和 alias 所起的作用都是指定响应请求所用文件的路径,只是他们有些许的区别
root 表示 location 匹配内容的相对路径
alias 表示 一个绝对路径,而且必须以"/"结尾
一般情况下,在location /中配置root,在location /other中配置alias
location /img/ {
alias /var/www/image/;
}
location /img/ {
root /var/www/image;
}
访问http://localhost/img/,nginx找/var/www/image/目录下的文件
访问http://localhost/img/,nginx找/var/www/image/img/目录下的文件
3.3、try_files
如果能找到指定的 uri 那么就返回相应的内容,否则的话返回错误状态码 404
try_files $uri $uri/ =404;
如果能找到指定的文件 1/2.html 那么就返回相应的内容,否则的话返回 6.html 文件内容
try_files 1.html 2.html /6.html;
如果能找到指定的 uri 那么就返回相应的内容,否则的话就内部重定向到后端名称为@backup 的 location
try_files $uri @backup;
四、nginx代理
server {
listen 192.168.8.14:80;
server_name www.sswang.com;
location / {
proxy_pass http://192.168.70.129:5000/hello/;
}
}
server {
listen 192.168.70.129:5000;
location /hello/ {
alias /web/dist;
try_files $uri $uri/ =404; #192.168.70.129:5000/hello/不存在返回404
}
}
五、负载均衡
upstream backends {
server 192.168.70.129:5000;
server 192.168.70.129:5001;
server 192.168.70.129:5002;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backends;
}
}
server {
listen 192.168.70.129:5000;
location / {
root /web/dist1;
try_files $uri $uri/ =404;
}
}
server {
listen 192.168.70.129:5001;
location / {
root /web/dist2;
try_files $uri $uri/ =404;
}
}
server {
listen 192.168.8.14:5002;
location / {
root /web/dist3;
try_files $uri $uri/ =404;
}
}