nginx入门

  Nginx是一个轻量级的Web服务器/反向代理服务器。它的一个突出优点是反向代理实现负载均衡。
实现原理:
nginx作为代理服务器接收HTTP请求
根据各个负载节点的权重,会随机的由一个节点来处理这个请求,然后返回数据到nginx,最后响应http请求。

正向代理和反向代理

  正向代理就是客户端主动寻找一个服务器代理自己访问想要访问的服务端。比如:我们想访问外国的网站时遇到拦截,我们可以选择代理服务器,通过它访问我们想要的内容。

  反向代理是服务端主动设置一个服务器让它代理自己对外提供服务。比如:我们有一个服务器集群,我们需要一个统一的入口。这时我们可以使用Nginx服务器作为反向代理服务器,它作为统一入口代理我们的集群。

Nginx实现负载均衡

  通过Nginx的反向代理机制,我们可以实现负载均衡。实现方法主要是修改Nginx的配置文件,在upstream和server模块中添加负载均衡的信息。

upstream模块

  ip_hash参数:当新的请求到达时,先将其客户端 ip 通过哈希算法哈希出一个值,
在随后请求客户端Ip的哈希值只要相同,就会被分配至同一个服务器,该调度算法可以解决 session 问题,但有时会导致分配不均即,无法保证负载均衡。因此ip_hash应该是在前端的服务器使用,后端应该直接接应用服务器。

  weight参数:weight是权重默认是1,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大

upstream nginxproject {
    ## ip_hash;
    server 192.168.80.121 weight=3;
    server 192.168.80.122 weight=2;
    server 192.168.80.123;
}
server模块

  在server模块中主要设置反向代理地址proxy_pass,此参数应为upstream的名称。

server {
    listen 80;
    server_name localhost;
    location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                #禁用缓存
                proxy_buffering off;
                #反向代理的地址
                proxy_pass http://nginxproject;
        }
}

附录-编译安装

####编译安装nginx####
安装nginx的依赖库:
l、gzip模块需要 zlib zlib-devel 库

2、rewrite模块需要 pcre pcre-devel 库

3、ssl功能需要openssl openssl-devel库 

安装步骤:

####yum安装依赖库文件####
yum clean all
yum -y install zlib zlib-devel  
yum -y install pcre pcre-devel 
yum -y install openssl openssl-devel
yum -y install gcc gcc-c++ autoconf automake make

####创建nginx用户####
useradd nginx -s /sbin/nologin -M
cat /etc/passwd |grep -i nginx
  nginx:x:1000:1000::/home/nginx:/sbin/nologin

####编译安装nginx####
cd /var/soft/
tar zxf nginx-1.9.7.tar.gz
cd nginx-1.9.7
##设置安装目录,带版本升级管理方便##
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.9.7 --with-http_stub_status_module --with-http_ssl_module 
make && make install
##软链接方便使用##
ln -s /usr/local/nginx-1.9.7 /usr/local/nginx


####检查安装是否成功####
1、cd /usr/local/nginx   
   ls -l  查看文件
   /usr/local/nginx/sbin/nginx -v 查看版本  

2、/usr/local/nginx/sbin/nginx -t  检查配置 (可以带上配置路径 -c /usr/local/nginx/conf/nginx.conf)
nginx: the configuration file /usr/local/nginx-1.9.7/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.9.7/conf/nginx.conf test is successful


启动nginx  
cd /usr/local/nginx/sbin
./nginx
重启  
cd /usr/local/nginx/sbin
./nginx -s reload


####开机自启动设置####
上传nginx 开机启动脚本到/var/soft
cp /var/soft/nginx /etc/init.d/nginx
chmod a+x /etc/init.d/nginx  ## 添加执行权限 (a+x ==> all user can execute  所有用户可执行)
systemctl daemon-reload  ## 修改过/etc/init.d中的文件,需要systemctl daemon-reload 扫描新的或有变动的单元
chkconfig --add nginx
chkconfig nginx on    
chkconfig --list|grep -i nginx  

现在启动nginx可以使用
systemctl start nginx
systemctl stop nginx
systemctl status nginx

####直接使用nginx命令####
echo 'export PATH=$PATH:/usr/local/nginx/sbin'>> /etc/profile
tail -1 /etc/profile
source /etc/profile 如果不执行source 当前环境下是不生效的


检查进程和端口
ps -ef|grep -i nginx
lsof -i :80
查看页面
curl -i http://localhost/
浏览器中输入http://localhost可以验证是否安装启动成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值