Nginx服务的搭建与配置(二)----Nginx的反向代理
Nginx的反向代理
web网站使用反向代理,除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,
 还可以进行负载均衡,将用户请求分配给多个服务器。
1.删除残留文件(没有,就跳过)
cd /etc/nginx/conf.d
rm -rf default.conf.rpmsave
2.修改配置文件,配置反向代理。
vi default.conf
#添加如下内容:
server {  
        listen       80;  
        server_name  www.007.com;  
        access_log      /var/log/nginx/host.access.log   main;
        location / {  
            proxy_pass   http://test;           //反向代理服务器的地址,也可以是IP地址。
            index            index.html index.htm index.php;  
                         }       
            }  
upstream test1 {                                                                # 后端web服务器
    server 192.168.205.149:8080  weight=1;                     # 默认端口80,默认权重为1.
    }  
upstream test2 {  
    server192.168.205.150:8080;  
    }  
server {  
        listen       80;  
        server_name  cat.com;  
        access_log  /var/logs/nginx/cat.access.log  main;   # 自己创文件夹及文件哦!
  
        location / {  
            proxy_pass   http://test1;                                     # 反向代理服务器的地址
            index  index.html index.htm index.php;  
        }       
    }  
server {  
        listen       80;  
        server_name  god.com;  
        access_log  /var/logs/nginx/god.access.log   main;  
  
        location / {  
            proxy_pass   http://test2;  
            index  index.html index.htm index.php;  
        }          
    } 
保存,退出
3.重启nginx
systemctl restart nginx
4.数据访问流程
使用浏览器访问http://www.cat.com,由于nginx反向代理接受客户机请求,找到server_name为http://www.cat.com的server节点, 根据proxy_pass对应的http路径,将请求转发到upstream test上,即ip为192.168.205.149的服务器。
感谢大家,点赞,收藏,关注,评论!
 
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号