LINUX环境安装nginx并且配置为为ip hash,可以不用处理session共享

     **以下命令均需root权限执行**:首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定**/usr/local为安装目录,以下具体版本号根据实际改变。

1、安装gcc gcc-c++:

yum install -y gcc gcc-c++
  
  

2、 安装PCRE库:


  
  
  1. cd /usr/local/
  2. wget http: //jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
  3. tar -zxvf pcre -8.33.tar.gz
  4. cd pcre -8.33
  5. ./configure
  6. make && make install

3、 安装SSL库:


  
  
  1. cd /usr/ local/
  2. wget http://www.openssl.org/ source/openssl-1.0.1j.tar.gz
  3. ar -zxvf openssl-1.0.1j.tar.gz
  4. cd openssl-1.0.1j
  5. ./config
  6. make && make install

4、安装zlib库:


  
  
  1. cd /usr/local/
  2. wget http: //zlib.net/zlib-1.2.11.tar.gz
  3. tar -zxvf zlib -1.2 .11.tar.gz
  4. cd zlib -1.2 .11
  5. ./configure
  6. make && make install

wget下载遇到下面情况,windows下载上传到/usr/local目录

 

5、安装nginx:


  
  
  1. cd /usr/ local/
  2. wget http: //nginx.org/download/nginx-1.12.2.tar.gz
  3. tar -zxvf zlib- 1.2. 11.tar.gz
  4. cd zlib- 1.2. 11
  5. ./configure --user=nobody --group=nobody --prefix=/usr/ local/nginx -- with-http_stub_status_module -- with-http_gzip_static_module -- with-http_realip_module -- with-http_sub_module -- with-http_ssl_module
  6. ./configure -- with-openssl=/usr/ local //链接SSL库
  7. make && make install

6、启动:


  
  
  1. whereis nginx(nginx: /usr/ local/nginx) //查找目录
  2. cd /usr/ local/nginx/sbin
  3. ./nginx

默认端口为 80端口

访问:curl localhost

Windows下访问:ip:80/ip,如果不能访问且ping ip是通的,很可能是Linux80端口没有开放 ,执行(以下命令适用于CentOS7+)

首先查看所有打开端口

firewall-cmd --zone=public --list-ports
  
  

添加一个端口:

firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
  
  

重新载入:

firewall-cmd --reload
  
  

 7、基本命令:

重启:./nginx -s reload

停止:./nginx -s stop

离开:./nginx -s quit

*nginx -s stop and -s quit 什么区别:

          quit 关闭方式,nginx在退出前完成已经接受的连接请求。

          stop 是快速关闭,不管有没有正在处理的请求。

8、配置负载均衡策略:

修改配置文件:/usr/local/nginx/conf/nginx.conf,目前Nginx的upstream模块支持6种分配方式

(1)ip_hash,根据Ip分配方式,该方式在用户第一次访问时就会进行节点分配,分配后,运行的节点不会改变,可以不用处理session共享问题:

**proxy_cookie_path   /项目名  / :防止cookie丢失,开始由于没有配置这个导致登陆时获取不到cookie自动退出。

(2)轮询,默认方式

(3)weight,权重方式

(4)least_conn,最少连接方式

(5)fair(第三方),响应时间方式

(6)url_hash(第三方),依据url分配方式

猜你喜欢

转载自blog.csdn.net/ljq354004063/article/details/84744433
今日推荐