企业—Nginx基础搭建

                                              Red Hat Enterprise Linux 7.5下Nginx安装

一、简介

Nginx("engine x")是一款是由俄罗斯的程序设计师 Igor Sysoev 所开发高性能的Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品。

Nginx 特点

Nginx 做为 HTTP 服务器,有以下几项基本特性:

  • 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.

  • 无缓存的反向代理加速,简单的负载均衡和容错.

  • FastCGI,简单的负载均衡和容错.

  • 模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCGI 或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。

  • 支持 SSL 和 TLSSNI.

Nginx 的工作方式

Nginx 是以多进程的方式来工作的,当然 Nginx 也是支持多线程的方式的,只是我们主流的方式还是多进程的方式,也是 Nginx 的默认方式。

Nginx 在启动后,会有一个 master 进程和多个 worker 进程。master 进程主要用来管理 worker 进程,包含:接收来自外界的信号,向各 worker 进程发送信号,监控 worker 进程的运行状态,当 worker 进程退出后(异常情况下),会自动重新启动新的 worker 进程。而基本的网络事件,则是放在 worker 进程中来处理了。多个 worker 进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个 worker 进程中处理,一个 worker 进程,不可能处理其它进程的请求。worker 进程的个数是可以设置的,一般我们会设置与机器cpu核数一致,这里面的原因与 Nginx 的进程模型以及事件处理模型是分不开的。

Nginx 的进程模型

我们前面有提到,worker 进程之间是平等的,每个进程,处理请求的机会也是一样的。当我们提供 80 端口的 http 服务时,一个连接请求过来,每个进程都有可能处理这个连接,怎么做到的呢?首先,每个worker进程都是从master进程fork 过来,在 master 进程里面,先建立好需要 listen 的 socket(listenfd)之后,然后再 fork 出多个 worker 进程。所有 worker 进程的 listenfd 会在新连接到来时变得可读,为保证只有一个进程处理该连接,所有 worker 进程在注册 listenfd 读事件前抢 accept_mutex,抢到互斥锁的那个进程注册 listenfd 读事件,在读事件里调用 accept 接受该连接。当一个 worker 进程在 accept 这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接,这样一个完整的请求就是这样的了。我们可以看到,一个 请求,完全由 worker 进程来处理,而且只在一个 worker 进程中处理。

为什么 Nginx 采用异步非阻塞的方式来处理呢

首先,请求过来,要建立连接,然后再接收数据,接收数据后,再发送数据。具体到系统底层,就是读写事件,而当读写事件没有准备好时,必然不可操作,如果不 用非阻塞的方式来调用,那就得阻塞调用了,事件没有准备好,那就只能等了,等事件准备好了,再继续。阻塞调用会进入内核等待,cpu 就会让出去给别人用了,对单线程的 worker 来说,显然不合适,当网络事件越多时,大家都在等待呢,cpu 空闲下来没人用,cpu利用率自然上不去了,更别谈高并发了。

非阻塞就是,事件没有准备好,马上返回 EAGAIN,告诉你,事件还没准备好呢,你过一会,再来检查一下事件,直到事件准备好了为止,在这期间,你就可以先去做其它事情,然后再来看看事件好了没。阻塞调用会进入内核等待,cpu 就会让出去给别人用了,对单线程的 worker 来说,显然不合适,当网络事件越多时,大家都在等待呢,cpu 空闲下来没人用,cpu利用率自然上不去了,更别谈高并发了。

优点

Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高

Nginx 是一个很强大的高性能Web反向代理服务器,它具有很多非常优越的特性:

在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型

二、准备

1、环境

系统平台:Red Hat Enterprise Linux Server release 7.5

内核版本:3.10.0-514.el7.x86_64

2、安装编译工具和库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

3、安装pcre

PCRE 作用是让 Ngnix 支持 Rewrite 功能。

查看是否安装pcre

# pcre-config --version

上述表明已安装。

若未安装,参照以下步骤:

1)下载

地址:https://sourceforge.net/projects/pcre/files/pcre/

2)解压安装包:
# tar zxvf pcre-8.35.tar.gz

3)编译安装
# cd pcre-8.35

# ./configure

# make && make install

三、安装

1、下载 nginx 安装包

http://nginx.org/download/

下载完成以后,放在windows桌面,直接拖进linux系统中

2、解压

# tar zxvf nginx-1.6.2.tar.gz

首先,在解压之前需要增加权限  chmod +x  nginx-1.6.2.tar.gz

类似于这样的包,篇幅有限,截图不太多

3、编译

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

4、安装

[root@localhost nginx-1.6.2]# make && make install

5、测试

查看nginx版本

# /usr/local/nginx/sbin/nginx -v

显示版本信息,证明已安装成功

1、创建用户

创建 Nginx 运行使用的用户 ruready:
# /usr/sbin/groupadd ruready
# /usr/sbin/useradd -g ruready ruready

2、配置nginx.conf

# vi /usr/local/nginx/conf/nginx.conf

user  ruready ruready;
worker_processes  2;
 
error_log  /usr/local/nginx/logs/error.log crit; # 日志位置和日志级别
pid        /usr/local/nginx/logs/nginx.pid;
 
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  65535;
}
 
 
http {
    include      mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /usr/local/nginx/logs/access.log  main;
 
    sendfile        on;
    tcp_nopush    on;
 
    keepalive_timeout  60;
 
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
 
    # 下面是server虚拟主机的配置
    server {
        listen      80;#监听端口
        server_name  localhost;#域名
 
        charset utf-8;
 
        access_log  /usr/local/nginx/logs/host.access.log  main;
 
        location / {
            root  html;
            index  index.html index.htm;
        }
 
        error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
 
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 
        #location ~ \.php$ {
        #    proxy_pass  http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       
        location ~ \.php$ {
            root          html;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen      8000;
    #    listen      somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root  html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen      443 ssl;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root  html;
    #        index  index.html index.htm;
    #    }
    #}
 
}

3、检查配置文件ngnix.conf的正确性

# /usr/local/nginx/sbin/nginx -t

2、访问测试

猜你喜欢

转载自blog.csdn.net/y805805/article/details/82946375