centos7 安装nginx配置负载均衡、反代理

Linux就该这么学

1、安装c++编译环境,如已安装可略过

 

[html] view plain copy

 

  1. [root@xxx]# yum install gcc-c++  


 

2、下载组件,下载到 cd /usr/local/dowload/

[root@xxx]# wget http://nginx.org/download/nginx-1.10.2.tar.gz

[root@xxx]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz

[root@xxx]# wget http://zlib.net/zlib-1.2.11.tar.gz

[root@xxx]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz


3、各自解压安装

openssl安装

 

[html] view plain copy

 

  1. [root@xxx]# tar zxvf openssl-fips-2.0.10.tar.gz  
  2. [root@xxx]# cd openssl-fips-2.0.10  
  3. [root@localhost openssl-fips-2.0.10]# ./config && make && make install  

 

zlib安装

[html] view plain copy

 

  1. [root@xxx]# tar zxvf zlib-1.2.11.tar.gz  
  2. [root@xxx]# cd zlib-1.2.11  
  3. [root@localhost zlib-1.2.11]# ./configure && make && make install  

openssl安装

 

[html] view plain copy

 

  1. [root@xxx]# tar zxvf pcre-8.40.tar.gz  
  2. [root@xxx]# cd pcre-8.40  
  3. [root@localhost pcre-8.40]# ./configure && make && make install  

nginx安装

[html] view plain copy

 

  1. [root@xxx]# tar zxvf nginx-1.10.2.tar.gz  
  2. [root@xxx]# cd nginx-1.10.2  
  3. [root@localhost nginx-1.10.2]# ./configure && make && make install  





4、Nginx启动和测试

 

 

[html] view plain copy

 

  1. [root@xxx]# whereis nginx  




[html] view plain copy

 

  1. cd /usr/local/nginx/  

启动

 

[html] view plain copy

 

  1. sbin/nginx  

 

上面表示已经启动过

测试

 

[html] view plain copy

 

  1. curl localhost:81  




 

我的修改过,把80修改了81

浏览器测试

 

 

 

---------------------------------------成功安装------------------------------------------------------------

5、负载均衡简单配置

修改配置

  vim /usr/local/nginx/conf/nginx.conf

我的nginx.conf,去掉了暂时不需要的

 

[html] view plain copy

 

  1. worker_processes  1;  
  2.   
  3. events {  
  4.     worker_connections  1024;  
  5. }  
  6.   
  7.   
  8. http {  
  9.     include       mime.types;  
  10.     default_type  application/octet-stream;  
  11.     sendfile        on;  
  12.     keepalive_timeout  65;  
  13.     upstream bbb.test.com{  
  14.         ip_hash;  
  15.         server 10.150.7.248:8080;  
  16.                       
  17.     }  
  18.     server {  
  19.         listen       8080;  
  20.         server_name  aaa.test.com;  
  21.         location / {  
  22.             proxy_pass http://bbb.test.com;  
  23.         }  
  24.         error_page   500 502 503 504  /50x.html;  
  25.         location = /50x.html {  
  26.             root   html;  
  27.         }  
  28.     }  
  29.     server {  
  30.             listen       81;  
  31.             server_name  nginx.test1.com;  
  32.             location / {  
  33.                 root   html;  
  34.                 index  index.html index.htm;  
  35.             }  
  36.             error_page   500 502 503 504  /50x.html;  
  37.             location = /50x.html {  
  38.                 root   html;  
  39.             }  
  40.         }  
  41.   
  42. }  

配置说明

 


upstream xxx{};upstream 模块是命名一个后端服务器组,组名必须为后端服务器站点域名,内部可以写多台服务器ip和port,

还可以设置跳转规则及权重等等


ip_hash;代表使用ip地址方式分配跳转后端服务器,同一ip请求每次都会访问同一台后端服务器
server;代表后端服务器地址
server{};server模块依然是接收外部请求的部分
server_name;代表外网访问域名
location / {};同样代表过滤器,用于制定不同请求的不同操作
proxy_pass;代表后端服务器组名,此组名必须为后端服务器站点域名


server_name和upstream{}的组名可以不一致,server_name是外网访问接收请求的域名,upstream{}的组名是跳转后端服务器时站点访问的域名

----------------------------------------------说明一下-----------------------------------------------------------------------------------------------------------------------------

window是我启动了tomcat----8080-----------------------------10.150.7.248:8080

window 下载switchhosts----------------------------------------ip重定向-----10.150.7.157  aaa.test.com

centos7的ip-----------------10.150.7.157

 

window下浏览器测试 aaa.test.com:8080

 

-------------------------成功-----------------------------------------------------------

是不是有点蒙圈

本文由 CentOS中文站 - 专注Linux技术 作者:centos 发表,其版权均为 CentOS中文站 - 专注Linux技术 所有,文章内容系作者个人观点,不代表 CentOS中文站 - 专注Linux技术 对观点赞同或支持。如需转载,请注明文章来源。

358847481

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注