1.Nginx简介
C语言编写,采用服务器实现负载均衡,高性能的HTTP和反向代理web服务器,处理高并发能力是十分强大,最高能支持5w个并发连接数。
2.Nginx 的优点
- 高并发。静态小文件
- 占用资源少。2万并发、10个线程,内存消耗几百M。
- 功能种类比较多。web,cache,proxy。每一个功能都不是特别强。
- 支持epoll模型,使得nginx可以支持高并发。
- nginx 配合动态服务和Apache有区别。(FASTCGI 接口)
- 利用nginx可以对IP限速,可以限制连接数。
- 配置简单,更灵活。
3. Nginx应用场合
- 静态服务器(图片,视频服务),另个lighttpd。并发几万,html,js,css,flv,jpg,gif等。
- 动态服务,nginx—fastcgi 方式运行PHP,jsp。(PHP并发约500-1500,MySQL 并发约300-1500)。
- 反向代理,负载均衡。日pv2000W以下,都可直接用nginx做代理。
- 缓存服务。类似 SQUID,VARNISH。
4.Nginx Docker构建和指定配置启动
编写dockerfile文件并且构建镜像
#基于ubuntu构建的镜像
from java_ubuntu
user root
run mv /etc/apt/sources.list /etc/apt/alibaba_sources.list
run mv /etc/apt/ubuntu_sources.list /etc/apt/sources.list
run apt-get update
#nginx install
run apt-get install -y nginx
run mkdir /etc/nginx/html
run mkdir /etc/nginx/logs
copy nginx.conf /etc/nginx/
copy index.html /etc/nginx/html
cmd nginx -c /etc/nginx/nginx.conf && tail -f /dev/null
#验证配置文件
nginx -tc /etc/nginx/conf/nginx_my.conf
nginx -t -c /etc/nginx/conf/nginx_my.conf
#启动
nginx -c etc/nginx/conf/nginx_my.conf
#重启
nginx -s reload -c /etc/nginx/conf/nginx_my.conf
#停止
nginx -s stop -c /etc/nginx/conf/nginx_my.conf
5.Nginx配置文件
nginx.conf主要由三部分组成
- 全局块,
- events块
- http块
完整的配置文件
nginx.conf
worker_processes 1; # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
events {
worker_connections 1024; # 每个工作进程连接数
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志格式
log_format access '$remote_addr - $remote_user [$time_local] $host "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /etc/nginx/logs/access.log access; # 日志输出目录
gzip on;
sendfile on;
# 链接超时时间,自动断开
keepalive_timeout 60;
# 虚拟主机,访问静态文件
server {
listen 8080;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost.access.log access;
# 路由
location / {
root /etc/nginx/html; # 访问根目录
index index.html index.htm; # 入口文件
}
location /html {
root /etc/nginx;
index index.html index.htm;
}
location /htmlalias {
alias /etc/nginx/html/;
index index.html index.htm;
}
location /returnok {
return 200 "masker success";
}
}
# 访问其他服务
# server {
# listen 9090;
# server_name localhost;
# charset utf-8;
# access_log /etc/nginx/logs/localhost9090.access.log access;
# # 路由
# location /nginxproducterone {
# proxy_pass http://producter-one:8080/;
# }
# }
server {
# 不指定端口默认监听80端口
# http默认监听80端口
# https默认监听443端口
# listen 8080;
server_name nginx.test.com; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/nginxtestcom.log access;
location /returnok {
return 200 "masker success nginx.test.com";
}
}
# 引入其他的配置文件
#include servers/*;
include /etc/nginx/conf/server.conf;
}
server.conf
upstream proxyserver {
server localhost:18080;
server localhost:28080;
}
server {
listen 18080;
server_name localhost;
charset utf-8;
access_log /etc/nginx/logs/localhost_18080.log access;
location / {
return 200 "18080 returnok";
}
}
server {
listen 28080;
server_name localhost;
charset utf-8;
access_log /etc/nginx/logs/localhost_28080.log access;
location / {
return 200 "28080 returnok";
}
}
server {
listen 60000;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost60000.access.log access;
location /returntest {
# 结尾不带/,则用proxy_pass+location,结尾带/,则只使用proxy_pass
proxy_pass http://proxyserver/;
}
}
5.1 全局块
nginx服务器全局生效的配置命令
worker_processes 1; # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
5.2 events块
影响nginx和用户网络的连接
worker_connections 1024; #最大连接数1024个,需灵活配置
5.3 http块
nginx配置最频繁的部分,比如代理,日志,缓存、第三方模块等等。http块可以包括http全局块和server块
http {
include mime.types;
default_type application/octet-stream;
gzip on;
sendfile on;
server{
}
server {
}
}
1 http的全局块
包括文件引入、MIME-TYPE定义,日志自定义、连接超时等等
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; # 访问到未定义的扩展名的时候,就默认为下载该文件
}
2 http的server块
与虚拟主机有密切关系,主要是为了节省硬件成本,一个http块可以包含多个server块,而一个server块就等于一个虚拟主机server块又包含全局server块和location块。
a.server块
server {
listen 8080;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost.access.log access;
location / {
xxx xxx;
}
}
b.location块
语法规则: location [=|~|~*|^~] /uri/ { … }
- = 严格匹配。如果请求匹配这个location,那么将停止搜索并立即处理此请求
- ~ 区分大小写匹配(可用正则表达式)
- ~* 不区分大小写匹配(可用正则表达式)
- !~ 区分大小写不匹配
- !~* 不区分大小写不匹配
- ^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式
# 路由
location / {
root /etc/nginx/html; # 访问/etc/nginx/html/index.html
index index.html index.htm; # 入口文件
}
location /html/ {
root /etc/nginx; # 访问/etc/nginx/html/other/index.html
index index.html index.htm; # 入口文件
}
location /htmlalias/ {
alias /etc/nginx/html/;
index index.html index.htm;
}
location /producterone/ {
proxy_pass http://172.18.0.4:8080; #转发到其他服务http://172.18.0.4:8080//producterone/say
}
location /returnok/ {
return 200 "success"; #返回200和消息
}
#root 实际访问文件路径会拼接URL中的路径
#alias 实际访问文件路径不会拼接URL中的路径
#proxy_pass 表示代理转发
注意:
producter-one的访问地址: http://producter-one:8080/producterone/say
1.location 中的最后有没有 / 都没有影响。/producterone和/producterone/都是可以正常请求的。
2. proxy_pass url结尾的/,如果结尾不带/,则用proxy_pass+location的路径,表示相对路径,;如果结尾带/,则只使用proxy_pass,表示绝对根路径。
a.proxy_pass结尾带/
访问只使用了proxy_pass
则访问:localhost:9090/producterone/say 访问会提示404
所以需要修改请求地址:localhost:9090/nginx-producterone/producterone/say
a.proxy_pass结尾不带/
访问使用proxy_pass+location,则访问:localhost:9090/producterone/say 正常
6. 负载均衡规则
在http块中增加upstream 的配置。upstream的三种策略分别是ip_hash,weight权重,轮询(默认),轮询不需要特殊设置,是直接的默认方式。这三种方式优先级是ip_hash > weight > 轮询 。
upstream配置语法
upstream <名称> {
[ip_hash策略];
server <服务信息> [权重信息];
server <服务信息> [权重信息];
}
6.1轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除
upstream producteroneserver {
server producter-one:8080;
server producter-one2:8080;
}
server {
listen 9090;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost9090.access.log access;
# 路由
location /producterone {
proxy_pass http://producteroneserver;
}
}
6.2 weight权重
weight 代表权重默认为 1,通过设置权重值指定集群中不同机器的权重,权重越高,落到该机器的请求次数越多。
upstream producteroneserver {
server producter-one:8080 weight=2;
server producter-one2:8080 weight=1;
}
server {
listen 9090;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost9090.access.log access;
# 路由
location /producterone {
proxy_pass http://producteroneserver;
}
}
6.3 ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题
upstream producteroneserver {
ip_hash;
server producter-one:8080;
server producter-one2:8080;
}
server {
listen 9090;
server_name localhost; # 浏览器访问域名
charset utf-8;
access_log /etc/nginx/logs/localhost9090.access.log access;
# 路由
location /producterone {
proxy_pass http://producteroneserver;
}
}
参考:
Nginx配置使用详解_shstart7的博客-CSDN博客_nginx 配置详解
nginx详细参数配置(史上最全) - hanease - 博客园