一、Nginx是什么?

一、Nginx是什么?

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数。
Nginx支持热部署,启动简单,可以做到7*24不间断运行。几个月都不需要重新启动。

二、Nginx的反向代理(扩展:正向代理)

首先,看一张关于正向代理和反向代理的图片在这里插入图片描述

在这里,用通俗易懂的方式解释一下:

正向代理: 我们平时需要访问国外的浏览器是不是很慢,比如我们要看推特,看GitHub等等。我们直接用国内的服务器无法访问国外的服务器,或者是访问很慢。所以我们需要在本地搭建一个服务器来帮助我们去访问。那这种就是正向代理。(浏览器中配置代理服务器)

反向代理: 那什么是反向代理呢。比如:我们访问淘宝的时候,淘宝内部肯定不是只有一台服务器,它的内部有很多台服务器,那我们进行访问的时候,因为服务器中间session不共享,那我们是不是在服务器之间访问需要频繁登录,那这个时候淘宝搭建一个过渡服务器,对我们是没有任何影响的,我们是登录一次,但是访问所有,这种情况就是 反向代理。对我们来说,客户端对代理是无感知的,客户端不需要任何配置就可以访问,我们只需要把请求发送给反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,再返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器的地址。(在服务器中配置代理服务器)

三、Nginx的负载均衡

什么是负载均衡?
负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡(Load Balance)其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

简单来说就是:现有的请求使服务器压力太大无法承受,所有我们需要搭建一个服务器集群,去分担原先一个服务器所承受的压力,那现在我们有ABCD等等多台服务器,我们需要把请求分给这些服务器,但是服务器可能大小也有自己的不同,所以怎么分?如何分配更好?又是一个问题。

Nginx给出来三种关于负载均衡的方式:

轮询法(默认方法):
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
适合服务器配置相当,无状态且短平快的服务使用。也适用于图片服务器集群和纯静态页面服务器集群。
weight权重模式(加权轮询):
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的
情况。
这种方式比较灵活,当后端服务器性能存在差异的时候,通过配置权重,可以让服务器的性能得到充分发挥,有效利用资源。weight和访问比率成正比,用于后端服务器性能不均的情况。权重越高,在被访问的概率越大
ip_hash:
上述方式存在一个问题就是说,在负载均衡系统中,假如用户在某台服务器上登录了,那么该用户第二次请求的时候,因为我们是负载均衡系统,每次请求都会重新定位到服务器集群中的某一个,那么已经登录某一个服务器的用户再重新定位到另一个服务器,其登录信息将会丢失,这样显然是不妥的。
我们可以采用ip_hash指令解决这个问题,如果客户已经访问了某个服务器,当用户再次访问时,会将该请求通过哈希算法,自动定位到该服务器。每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
四、Nginx的动静分离!
Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术。动静分离技术其实是采用代理的方式,在server{}段中加入带正则匹配的location来指定匹配项针对PHP的动静分离:静态页面交给Nginx处理,动态页面交给PHP-FPM模块或Apache处理。在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式

目前,通过使用Nginx大大提高了网站的响应速度,优化了用户体验,让网站的健壮性更上一层楼!

在这里插入图片描述

部署nginx

//准备工作
[root@localhost  ~]#  hostnamectl  set-hostname  nginx .example .com [root@localhost  ~]#  bash

//关闭防火墙
[root@localhost  ~]#  systemctl  stop  firewalld
[root@localhost  ~]#  systemctl  disable  firewalld
[root@localhost  ~]#  cat  /etc/sysconfig/selinux
SELINUX=disabled


//配置yum源
[root@localhost  ~]#  ls  /etc/yum . repos .d/
CentOS-Base . repo      epel . repo                                       epel-testing . repo
epel-modular . repo    epel-testing-modular . repo


//创建nginx用户
[root@localhost  ~]#  useradd  - r  -M  -s  /sbin/nologin  nginx

//安装开发工具包
[root@localhost  ~]#  yum  -y  groups  mark  install   'Development  Tools ' Complete !
//安装依赖包和一些常用工具
[root@localhost  ~]#  yum  -y  install  pcre-devel  openssl  openssl-devel  gd-devel gcc  gcc-c++  make  wget
Complete !

//创建日志存放目录
[root@localhost  ~]#  cd  /var/log/
[root@localhost  log]#  mkdir  nginx
[root@localhost  log]#  ls  -ld  nginx/
d rwxr-x r-x  2  root  root  6  Oct  10  10 :55  nginx/

[root@localhost  log]#  chown  nginx .nginx  /var/log/nginx/  -R
[root@localhost  log]#  ls  -ld  nginx/
d rwxr-x r-x  2  nginx  nginx  6  Oct  10  10 :55  nginx/

//解压nginx源码包
[root@localhost  ~]#  ls
anaconda-ks .cfg    nginx-1 .22 .0 .tar .gz
[root@localhost  ~]#  tar  -zxf  nginx-1 .22 .0 .tar .gz  -C  /usr/local/
[root@localhost  ~]#  cd  /usr/local/
[root@localhost  local]#  ls
bin    etc    games    include    lib    lib64    libexec    nginx-1 .22 .0    sbin    share
s rc

//编译安装
[root@localhost  local]#  cd  nginx-1 .22 .0/
[root@localhost  nginx-1 .22 .0]#  ls
auto           CHANGES . ru    configure    html           man           s rc
CHANGES    conf                 contrib         LICENSE    README

[root@localhost nginx-1 .22 .0]#  ./configure  \
>  --prefix=/usr/local/nginx  \
>  --user=nginx  \
>  --group=nginx  \
>  --with-debug  \
>  --with-http_ssl_module  \
>  --with-http_ realip_module  \
>  --with-http_image_filter_module  \
>  --with-http_gunzip_module  \
>  --with-http_gzip_static_module  \
>  --with-http_stub_status_module  \
>  --http-log-path=/var/log/nginx/access .log  \
>  --error-log-path=/var/log/nginx/error .log



[root@localhost  nginx-1 .22 .0]#  make [root@nginx  nginx-1 .22 .0]#  make

[root@localhost  nginx-1 .22 .0]#  ls
auto           CHANGES . ru    configure
CHANGES    conf                 contrib



install


html
LICENSE






Makefile man






objs        s rc
README

//配置环境变量
[root@localhost  nginx-1 .22 .0]#  echo   'export  PATH=/usr/local/nginx/sbin:$PATH ' >  /etc/profile .d/nginx .sh

[root@nginx  nginx-1 .22 .0]#  .  /etc/profile .d/nginx .sh

//查看nginx端口有没有起来
[root@localhost  ~]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort


LISTEN
LISTEN


0
0


128
128


0.0.0.0 :22
[ : :] :22


0.0.0.0 :*
[ : :] :*


//没有,只需要输入nginx命令就可以起来
[root@localhost  ~]#  nginx
[root@localhost  ~]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort

LISTEN	0	128	0.0.0.0 :80	0.0.0.0 :*
LISTEN	0	128	0.0.0.0 :22	0.0.0.0 :*
LISTEN	0	128	[ : :] :22	[ : :] :*

//  -t检查配置文件的语法
[root@localhost  ~]#  nginx  -t
nginx :  the  configuration  file  /usr/local/nginx/conf/nginx .conf  syntax  is ok
nginx :  configuration  file  /usr/local/nginx/conf/nginx .conf  test  is  succes
sful

//  -v查看nginx的版本
[root@localhost  ~]#  nginx  -v
nginx  version :  nginx/1 .22 .0

//  -s  发送服务控制信号
[root@localhost  ~]#  nginx  -s  stop
[root@localhost  ~]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort
LISTEN        0                    128                                   0 .0 .0 .0 :22                                 0 .0 .0 .0 :*
LISTEN        0                    128                                          [ : :] :22                                        [ : :] :*
(此时nginx的80端口已经被关掉了,只需要输入nginx的命令就可以启动)



//  -c指定配置文件路径
[root@localhost  ~]#  cd  /usr/local/nginx  [root@nginx  nginx]#  ls                              client_body_temp    fastcgi_temp    logs
conf                              html                      proxy_temp

[root@localhost  nginx]#  cd  conf/
[root@localhost  conf]#  ls
fastcgi .conf                          koi-win      fastcgi .conf .default        mime .types





sbin               uwsgi_temp
scgi_temp



scgi_params
scgi_params .default



fastcgi_params                 fastcgi_params .default koi-utf


mime .types .default nginx .conf
nginx .conf .default


uwsgi_params
uwsgi_params .default
win-utf


[root@localhost  conf]#  cp  nginx .conf  /opt/
[root@localhost  conf]#  cp  mime .types  /opt/
[root@localhost  conf]#  ls  /opt/
mime .types    nginx .conf

[root@localhost  conf]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort


LISTEN
LISTEN


0
0


128
128


0.0.0.0 :22
[ : :] :22


0.0.0.0 :*
[ : :] :*

(在nginx80端口没有起来的情况下)
[root@localhost  conf]#  nginx  -c  /opt/nginx .conf
[root@localhost  conf]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort

LISTEN	0	128	0.0.0.0 :80	0.0.0.0 :*
LISTEN	0	128	0.0.0.0 :22	0.0.0.0 :*
LISTEN	0	128	[ : :] :22	[ : :] :*

此时要关掉opt下的nginx
[root@localhost  conf]#  nginx  -c  /opt/nginx .conf  -s  stop
[root@localhost  conf]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort


LISTEN
LISTEN


0
0


128
128


0.0.0.0 :22
[ : :] :22


0.0.0.0 :*
[ : :] :*


[root@localhost  conf]#  nginx  -c  /opt/nginx .conf  -t
nginx :  the  configuration  file  /opt/nginx .conf  syntax  is  ok
nginx :  configuration  file  /opt/nginx .conf  test  is  successful
(此时只是测试,服务并没有启动)

[root@localhost  conf]#  nginx  -c  /opt/nginx .conf
[root@localhost  conf]#  ss  -antl
State           Recv-Q        Send-Q               Local  Address :Port                 Peer  Address :P
ort

LISTEN	0	128	0.0.0.0 :80	0.0.0.0 :*
LISTEN	0	128	0.0.0.0 :22	0.0.0.0 :*
LISTEN	0	128	[ : :] :22	[ : :] :*
( -c可以在启动nginx的时候,可以指定要读取的配置文件)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值