Java必备技能之实战篇—Nginx各种应用场景配置(可以直接复制修改使用)

nginx正向代理

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、普通轮询

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 负载均衡

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、基于比例加权轮询

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路由负载

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、基于服务器响应时间负载分配

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、对不同域名实现负载均衡

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;

        }
    }

静态资源服务器

server {

       listen 8300;
       server_name localhost;

       location / {
          root /home/testStatic;
           access_log   on;
               autoindex  on;
       }

 }

server_name:域名

listen:端口

root:静态页的路径

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/111503753