[笔记]nginx配置反向代理和负载均衡

1、nginx配置文件:源码安装情况下,nginx.conf在解压后的安装包内。yum安装,一般情况下,一部分在/etc/nginx/nginx.conf中,一部分在/etc/nginx/conf.d/default.conf中。

nginx配置反向代理和负载均衡,源码安装情况下在nginx.conf中配置,yum安装下,在/etc/nginx/conf.d/default.conf中配置

2、配置反向代理

  1 #设置反向代理
  2 upstream linux.test{
  3     server 111.111.111.111:80;
  4 }
  5 #要代理的服务器信息
  6 server {
  7     listen       80;
  8     server_name  linuxtest.com; #浏览器访问域名                  
  9 
 10     #charset koi8-r;
 11     #access_log  /var/log/nginx/host.access.log  main;
 12     
 13     location / {
 14         #设置代理
 15         proxy_pass http://linux.test;
 16     }   
 17     
 18 }

命令行下重新启动nginx后,没报错即完成。

流程:浏览器访问server下的server_name对应的域名(linuxtest.com)。服务器找到location /,location /下必须设置proxy_pass + 代理服务器url(proxy_pass http://linux.test;)。服务器通过代理服务器url找到upstream模块,找到后访问server对应的地址。

3、配置负载均衡

  1 #设置反向代理                                                
  2 upstream linux.test{
  3     #设置负载均衡
  4     server 111.111.111.111:80 weight=5;
  5     server 111.111.111.112:80 weight=1;
  6 }                            
  7 #要代理的服务器信息          
  8 server {
  9     listen       80;
 10     server_name  linux.com; #浏览器访问域名
 11     
 12     #charset koi8-r;
 13     #access_log  /var/log/nginx/host.access.log  main;
 14     
 15     location / {
 16         #设置代理
 17         proxy_pass http://linux.test;
 18     }
 19 
 20 }
 21 

在upstream中设置负载均衡,weight代表设置权重。可以不设置weight,默认处理请求的机会均等。

posted @ 2019-10-12 16:55  qetuo[  阅读(1340)  评论(0编辑  收藏  举报