Nginx(engine x)是一个免费的,开源的,高性能的 HTTP 服务器, IMAP/POP3 代理服务器 和 TCP/UDP 代理服务器,一般能够用于进行反向代理和实现基于软件的负载均衡,除此以外,它还具有如下特性:css
Nginx 可以同时支持正向代理和反向代理,这两种代理模式的区别以下:html
Nginx Shell 的基本使用格式以下:node
nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
复制代码
/usr/app/nginx-1.16.1/
;conf/nginx.conf
,其实际指向的路径为 prefix + filename
;Nginx 的配置由全局配置和局部配置(指令块)共同组成,全部的指令块都遵循相同的配置格式:nginx
<section>{
<directive> <parameters>;
}
复制代码
指令块使用大括号进行划分,大括号内是独立的配置上下文,包含指令和具体的参数,每行指令以分号做为结尾。除此以外,Nginx 的配置中还支持如下操做:git
include
语法来引入外部配置文件,从而能够将每一个独立的配置拆分到单独的文件中;#
符号来添加注释;$
符号来引用变量;Nginx 的配置文件支持如下空间和时间单位:github
空间单位:不加任何单位默认就是 bytes,除此以外还支持 k/K,m/M,g/G 等经常使用单位。web
空间单位:支持 ms (毫秒) ,s (秒) ,m (分钟) ,h (小时) ,d (天),w (星期),M (月,30天),y (年,365天) 等经常使用单位,并支持组合使用,如 1h 30m
(1小时 30分),1y 6M
(1年零6个月)。正则表达式
在安装 Nginx 后,在安装目录的 conf 目录下会有一个官方的配置样例 nginx.conf ,其内容以下:spring
# 使用这个参数来配置worker进程的用户和组,若是没有指定组,则默认为指定用户所处的组,默认值为nobody
user nobody;
# 指定用于处理客户端链接的worker进程的数量,一般设置为CPU的核心数。
# 若是是为IO密集型操做进行负载,能够设置为核心数的1.5 ~ 2倍
worker_processes 1;
# 指定日志的位置和日志级别,日志级别按照由低到高的顺序以下:[debug|info|notice|warn|error|crit]
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定记录主进程的ID的文件
pid logs/nginx.pid;
events {
# 指定每一个工程线程的最大链接数,总的链接数 max_clients = worker_processes * worker_connections
worker_connections 1024;
}
http {
# 使用include来引用外部文件
include mime.types;
# 指定默认MIME类型
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 logs/access.log main;
# 是否开启系统调用方法sendfile(),开启后能够直接在内核空间完成文件的发送,即零拷贝
sendfile on;
# 是否开启Socket选项,它只有在sendfile启用后才会生效
tcp_nopush on;
# 链接超时时间
keepalive_timeout 65;
# 开启文件压缩
gzip on;
# 配置nginx服务器(虚拟主机)
server {
# 监听端口
listen 80;
server_name localhost;
# 默认字符集
charset koi8-r;
# 配置当前虚拟主机的访问日志的存放位置
access_log logs/host.access.log main;
# 虚拟主机对应的映射目录
location / {
root html;
index index.html index.htm;
}
# 错误重定向页面
# error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 禁止访问根目录下以ht结尾的文件
location ~ /\.ht {
deny all;
}
}
# 支持配置多台虚拟主机
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 配置Https服务
server {
listen 443 ssl;
server_name localhost;
# 指定数字证书
ssl_certificate cert.pem;
# 指定密匙
ssl_certificate_key cert.key;
# 设置存储session的缓存类型和大小
ssl_session_cache shared:SSL:1m;
# session缓存时间
ssl_session_timeout 5m;
# 返回客户端支持的密码列表
ssl_ciphers HIGH:!aNULL:!MD5;
# 指定在使用SSLv3和TLS协议时,服务器密码应优先于客户端密码
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
复制代码
Nginx 一般用做 HTTP 服务器来部署静态资源,其具体的操做步骤以下:shell
修改 nginx.conf
,并在 http 指令块中增长以下配置:
server {
# 监听端口号
listen 9010;
# 若是有域名的话,能够在这里进行配置
server_name _;
# 存放静态页面的目录
root /usr/web;
# 主页面
index index.html;
}
复制代码
在 /usr/web
目录下新建一个测试页面 index.html,内容以下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Nginx静态资源网站</title>
</head>
<body>
<h1 style="text-align: center">Nginx静态资源网站</h1>
</body>
</html>
复制代码
使用 -t
参数来检查配置,出现 successful 则表明配置正确:
[root@node1 web]# nginx -t -c conf/nginx.conf
nginx: the configuration file /usr/app/nginx-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/app/nginx-1.16.1/conf/nginx.conf test is successful
复制代码
启动 Nginx ,若是 Nginx 已经启动,可使用以下命令重载配置:
nginx -s reload
复制代码
访问 http://hostname:9010/index.html ,便可查看到静态网站首页。
这里我使用 Docker 来部署两个 Tomcat,以后将测试项目 WAR 包分别拷贝到 /usr/webapps001
和 /usr/webapps002
两个挂载的容器卷下,程序会自动解压并运行,两个项目的端口号分别为 8080 和 8090:
run -d -it --privileged=true -v /usr/webapps01:/usr/local/tomcat/webapps \
-p 8080:8080 --name tomcat8080 96c4e536d0eb
复制代码
run -d -it --privileged=true -v /usr/webapps02:/usr/local/tomcat/webapps \
-p 8090:8080 --name tomcat8090 96c4e536d0eb
复制代码
修改 nginx.conf
,并在 http 指令块中增长以下配置:
# 这里指令块的名称能够随意指定,只要和下面的proxy_pass的值相同便可,一般配置为项目名
upstream springboot {
server 192.168.0.226:8080;
server 192.168.0.226:8090;
}
server {
listen 9020;
location / {
proxy_pass http://springboot;
}
}
复制代码
重载配置后,打开浏览器,经过 9020 端口访问项目,此时 Nginx 会以轮询的方式将请求分发到 8080 和 8090 端口上。在测试负载均衡策略的时候,最好将浏览器的缓存功能关闭,避免形成影响。
在上面的配置中,咱们没有配置任何负载均衡策略,默认采用的是轮询策略,除此以外,Nginx 还支持如下负载均衡策略:
经过为不一样的服务分配不一样的权重来进行转发,配置示例以下:
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com weight=2;
server srv3.example.com;
}
复制代码
将请求转发给链接数最少的服务,配置实例以下:
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
复制代码
经过对请求的 IP 地址进行哈希运算并取模来决定转发对象,配置示例以下:
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
复制代码
以上是 Nginx 内置的基础的负载均衡策略,若是想要实现其余更加复杂的负载均衡策略,能够经过第三方模块来实现。
在上面任何负载均衡策略当中,均可以经过 backup 参数来添加备用服务,示例以下:
server backup1.example.com:8080 backup;
复制代码
处于备用状态下的服务并不会参与负载均衡,除非全部主服务都已宕机,此时 Nginx 才会将请求转发到备用服务上。
Nginx 可以支持高并发的访问,并具备静态资源缓存等特性,所以相比于 Tomcat 等动态资源应用服务器,其更加适合于部署静态资源。想要实现动静分离,只须要在 server 指令块中经过正则表达式来划分静态资源,并指定其存放位置,示例以下:
server {
listen 9020;
location / {
proxy_pass http://springboot;
}
# 经过正则来控制所须要分离的静态资源
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
# 静态资源存放目录
root /usr/resources/;
}
}
复制代码
第一个常见的问题是找不到静态资源,此时能够查看 logs 目录下的 error.log
日志,一般输出以下:
2019/09/01 17:12:43 [error] 12402#0: *163 open() "/usr/resources/spring-boot-tomcat/css/show.css"
failed (2: No such file or directory), client: 192.168.0.106, server: ,
request: "GET /spring-boot-tomcat/css/show.css HTTP/1.1", host: "192.168.0.226:9020",
referrer: "http://192.168.0.226:9020/spring-boot-tomcat/index"
复制代码
出现这个问题,是由于 Nginx 要求静态资源的请求路径必须和原有请求路径彻底相同,这里个人项目在 Tomcat 中解压后的项目名为 pring-boot-tomcat
,以 show.css 文件为例,其正确的存储路径应该为:
/usr/resources/spring-boot-tomcat/css/show.css
复制代码
即: 静态资源根目录 + 项目名 + 原有路径,一般咱们在建立目录时会忽略掉项目名这一层级,从而致使异常。
路径正确后,另一个常见的问题是权限不足,错误日志以下。此时须要保证配置文件中的 user 用户必须具备静态资源所处目录的访问权限,或者在建立静态资源目录时,直接使用 user 配置的用户来建立:
2019/09/01 17:15:14 [error] 12402#0: *170 open() "/usr/resources/spring-boot-tomcat/css/show.css"
failed (13: Permission denied), client: 192.168.0.106, server: ,
request: "GET /spring-boot-tomcat/css/show.css HTTP/1.1", host: "192.168.0.226:9020",
referrer: "http://192.168.0.226:9020/spring-boot-tomcat/index"
复制代码
官方文档:nginx documentation ,Using nginx as HTTP load balancer
更多文章,欢迎访问 [全栈工程师手册] ,GitHub 地址:github.com/heibaiying/…