nginx优化之nginx介绍与安装

nginx介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。

其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

单机情况下可以支持10w并发

反向代理

反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。

同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。

负载均衡

负载均衡(Load Balance)其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上面,将原先请求到单个服务器上面的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,这就是所说的负载均衡。

动静分离

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低单个服务器的压力。

nginx的安装

安装前的准备

安装make

yum install gcc automake autoconf libtool make

安装g++

yum install gcc gcc-c++

安装pcre库

https://ftp.pcre.org/pub/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /home
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make
make install

安装zlib库

http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /home
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

cd /home
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g

make -j2
mkae install

--with-pcre=/usr/local/src/pcre-8.44 指的是pcre-8.44 的源码路径。

--with-zlib=/usr/local/src/zlib-1.2.11指的是zlib-1.2.11 的源码路径。

安装成功后 /usr/local/nginx 目录下如下

drwx------. 2 nobody root       6 3月   6 16:59 client_body_temp
drwxr-xr-x. 2 root   root    4096 3月   6 17:19 conf
-rw-r--r--. 1 root   root    1077 3月   6 16:59 fastcgi.conf
-rw-r--r--. 1 root   root    1077 3月   6 16:59 fastcgi.conf.default
-rw-r--r--. 1 root   root    1007 3月   6 16:59 fastcgi_params
-rw-r--r--. 1 root   root    1007 3月   6 16:59 fastcgi_params.default
drwx------. 2 nobody root       6 3月   6 16:59 fastcgi_temp
drwxr-xr-x. 2 root   root      57 3月   6 17:05 html
-rw-r--r--. 1 root   root    2837 3月   6 16:59 koi-utf
-rw-r--r--. 1 root   root    2223 3月   6 16:59 koi-win
drwxr-xr-x. 2 root   root      41 3月   6 16:59 logs
-rw-r--r--. 1 root   root    5231 3月   6 16:59 mime.types
-rw-r--r--. 1 root   root    5231 3月   6 16:59 mime.types.default
-rwxr-xr-x. 1 root   root 7963984 3月   6 16:59 nginx
-rw-r--r--. 1 root   root    2937 3月   6 17:28 nginx.conf
-rw-r--r--. 1 root   root    2656 3月   6 16:59 nginx.conf.default
drwxr-xr-x. 3 root   root      17 3月   6 16:50 nginx--conf-path=
-rw-r--r--. 1 root   root       6 3月   6 19:51 nginx.pid
drwx------. 2 nobody root       6 3月   6 16:59 proxy_temp
-rw-r--r--. 1 root   root     636 3月   6 16:59 scgi_params
-rw-r--r--. 1 root   root     636 3月   6 16:59 scgi_params.default
drwx------. 2 nobody root       6 3月   6 16:59 scgi_temp
-rw-r--r--. 1 root   root     664 3月   6 16:59 uwsgi_params
-rw-r--r--. 1 root   root     664 3月   6 16:59 uwsgi_params.default
drwx------. 2 nobody root       6 3月   6 16:59 uwsgi_temp
-rw-r--r--. 1 root   root    3610 3月   6 16:59 win-utf

启动nginx

/usr/local/nginx/nginx

注意:关闭虚拟机防火墙:systemctl stop firewalld(服务器需要开启80端口)

验证:

原文地址:https://www.cnblogs.com/studyandstudy/p/14988711.html