nginx修改完配置文件,一定要重启或者 nginx -s reload !!!!!!!!!!!

nginx各种配置

location路径配置原则和优先级

nginx一般用做代理服务器,并且通过location 来配置路径,以下通过几个例子来进行说明:

反向代理

location /{
	proxy_pass http://localhost:12345;
}
即是把 http://localhost:12345的地址代理到'ip/' ,ip要看部署nginx服务器的ip

location /a{
	proxy_pass http://localhost:12345;
}
这种写法是 将 http://localhost:12345/a 代理到 'ip/a',


更多的时候,我们想要将 http://localhost:12345 代理到'ip/a'(即不添加location 后面的部分),这时应采用如下的写法:
location /a/{
	proxy_pass http://localhost:12345/;
}

反向代理总结

location /a{
	proxy_pass http://ip;
}
location /b/{
	proxy_pass http://ip/;
}
上述配置的结果:
/a/x ==> http://ip/a/x;
/b/x ==>http://ip/x;

负载均衡

1、在server配置上面,添加

upstream group1{
server 192.168.0.1:80;
server 192.168.0.1:81;
}

2、编写location代理

location /a/{
proxy_pass http://group1/;
}

3、others

此外,还可以配置负载均衡的权重:

如:

server 192.168.0.1:80 weight=1;
server 192.168.0.1:81 weight=1;
weight 代表权重