我是靠谱客的博主 愉快中心,这篇文章主要介绍Java必备技能之实战篇—Nginx各种应用场景配置(可以直接复制修改使用),现在分享给大家,希望可以做个参考。

nginx正向代理

复制代码
1
2
3
4
5
6
7
8
9
10
11
server { listen 8090; server_name www.gps.com; location / { resolver 218.85.157.99 218.85.152.99; resolver_timeout 30s; proxy_pass http://$host$request_uri; } access_log /data/httplogs/proxy-$host-aceess.log; }

测试:
http://www.gps.com:8090

resolver指令
语法: resolver address … [valid=time];
默认值: —
配置段: http, server, location
配置DNS服务器IP地址。可以指定多个,以轮询方式请求。
nginx会缓存解析的结果。默认情况下,缓存时间是名字解析响应中的TTL字段的值,可以通过valid参数更改。

resolver_timeout指令
语法: resolver_timeout time;
默认值: resolver_timeout 30s;
配置段: http, server, location
解析超时时间。

nginx反向代理

1、普通轮询

复制代码
1
2
3
4
5
6
7
8
9
10
server { listen 80; server_name www.123.com; location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm index.jsp; } }

我们监听80端口,访问域名为www.123.com,不加端口号时默认为80端口,故访问该域名时会跳转到127.0.0.1:8080路径上。

nginx 负载均衡

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
upstream OrdinaryPolling { server 127.0.0.1:8080; server 127.0.0.1:8081; } server { listen 80; server_name localhost; location / { proxy_pass http://OrdinaryPolling; index index.html index.htm index.jsp; } }

2、基于比例加权轮询

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
upstream OrdinaryPolling { server 127.0.0.1:8080 weight=5; server 127.0.0.1:8081 weight=2; } server { listen 80; server_name localhost; location / { proxy_pass http://OrdinaryPolling; index index.html index.htm index.jsp; } }

基于IP路由负载

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
upstream OrdinaryPolling { ip_hash; server 127.0.0.1:8080 weight=5; server 127.0.0.1:8081 weight=2; } server { listen 80; server_name localhost; location / { proxy_pass http://OrdinaryPolling; index index.html index.htm index.jsp; } }

3、基于服务器响应时间负载分配

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
upstream OrdinaryPolling { server 127.0.0.1:8080 weight=5; server 127.0.0.1:8081 weight=2; fair; } server { listen 80; server_name localhost; location / { proxy_pass http://OrdinaryPolling; index index.html index.htm index.jsp; } }

4、对不同域名实现负载均衡

复制代码
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
upstream wordbackend { server 127.0.0.1:8080; server 127.0.0.1:8081; } upstream pptbackend { server 127.0.0.1:8082; server 127.0.0.1:8083; } server { listen 80; server_name localhost; location /word/ { proxy_pass http://wordbackend; index index.html index.htm index.jsp; } location /ppt/ { proxy_pass http://pptbackend; index index.html index.htm index.jsp; } }

静态资源服务器

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
server { listen 8300; server_name localhost; location / { root /home/testStatic; access_log on; autoindex on; } }

server_name:域名

listen:端口

root:静态页的路径

最后

以上就是愉快中心最近收集整理的关于Java必备技能之实战篇—Nginx各种应用场景配置(可以直接复制修改使用)的全部内容,更多相关Java必备技能之实战篇—Nginx各种应用场景配置(可以直接复制修改使用)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部