以前从未系统的了解过nginx,对于以后的配置学习和使用还是必不可少的。
nginx的作用
动静分离动静分离反向代理反向代理负载均衡负载均衡Viewer does not support full SVG 1.1
反向代理:和正向代理相区分,正向代理是通过一个代理服务器上网,类似一个由内到外的过程,而反向代理
我觉得是由外到内的过程,用户通过访问服务器暴露在外部host:port,能够映射服务器内部的web容器。
负载均衡:当用户访问量达到一定量级时,提升一台物理机的硬件已经不足以应对,则需要用到分而治之的思想了,
通过多台服务器合作,处理用户的请求,是一对多的一种关系,通过nginx可以将用户的请求分发到不同的服务器。
动静分离:在一个web项目中,有静态资源(图片、js、css等)和动态资源(需要实时访问服务器获取的资源),nginx可
以判断请求的资源类型,分发到不同的服务器,大概是这样吧。
nginx的配置文件
默认的nginx配置文件大致如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#定义nginx运行的用户
user nginx;
#nginx的进程数
worker_processes auto;
#错误日志的存储
error_log /var/log/nginx/error.log notice;
#进程文件
pid /var/run/nginx.pid;
#工作模式与连接数上限
events {
#单个进程的最大连接数
worker_connections 1024;
}
#设置http服务器
#只一部分需要配置的内容很多,但是作为初级使用是不需要配置的。
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#需要配置最多配置就在这里
include /etc/nginx/conf.d/*.conf;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
server {
#监听端口号
listen 80;
listen [::]:80;
#监听ing域名
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
#匹配Url进行方向代理
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
|
环境说明
https://labs.play-with-docker.com/ 一个免费在线的docker环境服务器
反向代理一
实现目标:通过访问nginx能够映射到tomcat。
1
2
3
4
|
docker pull nginx
docker pull tomcat
docker run -itd --name tomcat-1 -p 8081:8080 tomcat
docker run -itd --name nginx-master -p 80:80 nginx
|
tomcat默认的访问路径webapps,但是docker版的这个文件夹是空的,真正的是在webapps.list
需要先将webapps删除,再将webapps.list名字更改为webapps。
1
2
|
rm -rf webapps
mv webapps.list webapps
|
进入到nginx配置文件所在目录
更改server配置项
1
2
3
|
location / {
proxy_pass http://192.168.0.28:8081
}
|
保存。
nginx -t 检查nginx配置文件是否正确
nginx -s reload 重启,新的配置将生效。
反向代理二
实现目标:通过访问nginx的不同的路径,能够映射到不同的tomcat服务器。
1
2
3
4
5
|
docker pull nginx
docker pull tomcat
docker run -itd --name tomcat-1 -p 8081:8080 tomcat
docker run -itd --name tomcat-2 -p 8082:8080 tomcat
docker run -itd --name nginx-master -p 80:80 nginx
|
默认tomcat服务访问的路径是 webapps/ROOT 目录,docker默认webapps为空文件夹,需要自己在里面创建index.html文件,来显示不同的内容。
1
2
3
4
5
6
7
8
|
#进入tomcat-1容器中
docker exec -it tomcat-1 bash
cd webapps
mkdir one
cd one
#写入8081
vim index.html
|
1
2
3
4
5
6
7
8
|
#进入tomcat-2容器中
docker exec -it tomcat-2 bash
cd webapps
mkdir one
cd one
#写入8082
vim index.html
|
使用免费的容器反向代理会把url后面的链接带上。
进入到nginx配置文件所在目录
更改server配置项
1
2
3
4
5
6
|
location ~ /one {
proxy_pass httpL//192.168.0.28:8081
}
location ~ /two {
proxy_pass httpL//192.168.0.28:8082
}
|
保存。
nginx -t 检查nginx配置文件是否正确
nginx -s reload 重启,新的配置将生效。
负载均衡
上面的反向代理通过访问不同的路径映射到了不同的tomcat服务器,而负载均衡是访问同一个路径,也就是一个请求会根据对应的策略转发到不同的服务器。
实现目标:一个请求能够被转发到不同的服务器上。
1
2
3
4
5
|
docker pull nginx
docker pull tomcat
docker run -itd --name tomcat-1 -p 8081:8080 tomcat
docker run -itd --name tomcat-2 -p 8082:8080 tomcat
docker run -itd --name nginx-master -p 80:80 nginx
|
默认tomcat服务访问的路径是 webapps/ROOT 目录,docker默认webapps为空文件夹,需要自己在里面创建index.html文件,来显示不同的内容。
1
2
3
4
5
6
|
#进入tomcat-1容器中
docker exec -it tomcat-1 bash
cd webapps
#写入8081
vim index.html
|
1
2
3
4
5
6
|
#进入tomcat-2容器中
docker exec -it tomcat-2 bash
cd webapps
#写入8082
vim index.html
|
更改server配置项
nginx.conf
1
2
3
4
|
upstream myserver {
server 192.168.0.8:8081 weight=1;
server 192.168.0.8:8082 weight=1;
}
|
当所有的请求weight相等的时候就相当于轮询。
conf.d/default.conf
1
2
3
4
5
|
location / {
root html;
index index.html index.htm;
proxy_pass http://myserver;
}
|
weight是负载均衡策略的一种。会根据权重的不同,改变请求落在一个包的几率。
nginx/1.21.0默认的负载均衡策略是轮询,会根据时间顺序轮流落在每个服务器上。
动静分离