2.19 nginx的详细介绍

一、一些概念的介绍,apache和nginx的对比

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


网页的编辑语言:

html:为静态
php: 轻量级,动态
jsp(JAVA):重量级,动态


常用的性能评测检测,及其概念:


pv( page view) :统计浏览次数
uv(user view): 用户或阅度计算
qps(quest per Second):   每秒请求数
TPS(吞吐量):

apache和nginx的区别

apache

apache的工作模式是prework。prefork采用预派生子进程方式,用单独的子进程来处理不同的请求。进程之间彼此独立,每个进程只含有一个线程。
因此apache特别的稳定,但是apache占用的资源也多

nginx
nginx是轻量级的apache

nginx使用的工作模式是worker。相对于prefork,worker全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是,worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。

因此nginx  可处理高并发,多线程,内存消耗小。负载均衡成本低(比较F5)。 支持rewrite 。支持热部署。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二、nginx的详细介绍

1、nginx的安装,这里安装1.15.7版本,解压安装包,修改配置文件

tar zxf nginx-1.15.7.tar.gz
ls
cd nginx-1.15.7
vim src/core/nginx.h

 14 #define NGINX_VER          "nginx/"  ##隐藏版本号,如果不隐藏nginx的版本号,会受到攻击

vim auto/cc/gcc

171 # debug
172 #CFLAGS="$CFLAGS -g"    ##关闭debug,如果不打开安装包大小为900+KB,如果打开安装包大小为6M,并且日志量非常大

2、编译环境检查,编译,安装

./configure --prefix=/usr/local/nginx/ --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make clean    ##清理编译环境
./configure --prefix=/usr/local/nginx/ --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--with-http_ssl_module        ##安全套接字模块
--with-http_stub_status_module     ##状态监控模块
--with-threads             ##线程并发模块
--with-file-aio         ##文件异步输入输出模块

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make && make install

3、查看nginx的大小

cd /usr/local/
du -sh nginx/

下面是安装后,的大小
[root@server1 local]# du -sh nginx/
992K    nginx/

4、开启nginx,查看进程

cd /usr/local/nginx/sbin/
./nginx
ps aux

root      9559  0.0  0.0  47312  1204 ?        Ss   14:48   0:00 nginx: master p
nobody    9560  0.0  0.1  47768  2112 ?        S    14:48   0:00 nginx: worker p

kill 9 9560
发现有主进程,和worker进程,当worker进程被kill后,主进程还会启动worker进程。

5、修改配置文件,及配置文件的解读

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nginx下有四个目录:conf  html  logs  sbin
conf:配置文件目录
html:默认发布目录
logs:日志目录
sbin:启动目录
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cd /usr/local/nginx/conf/
vim nginx.conf

user  nginx nginx;        ##进程开启是用nginx用户开启的
worker_processes  4;        
##也可以worker_processes  auto;这里一般与cpu的数量相同。这里需要关闭虚拟机,增加cpu
worker_cpu_affinity 0001 0010 0100 1000;    ##配置cpu

6、添加用户

cat /etc/passwd
userdel -r nginx    ##删除原来的用户
useradd -s /sbin/nologin -M -d /usr/local/nginx nginx
cat /etc/passwd


7、添加cpu

lscpu
poweroff

8、加载nginx,查看进程,变为五个(1主,4worker)名用户名为nginx

cd /usr/local/nginx/sbin/
./nginx -t
./nginx -s reload
 ps aux


结果如下


root      1303  0.0  0.0  47304  1204 ?        Ss   14:59   0:00 nginx: master p
nginx     1304  0.0  0.0  47772  1844 ?        S    14:59   0:00 nginx: worker p
nginx     1305  0.0  0.0  47772  1844 ?        S    14:59   0:00 nginx: worker p
nginx     1306  0.0  0.0  47772  1844 ?        S    14:59   0:00 nginx: worker p
nginx     1307  0.0  0.0  47772  1812 ?        S    14:59   0:00 nginx: worker p
 

9 、修改并发文件的多少

vim /usr/local/nginx/conf/nginx.conf

 13 events {
 14     worker_connections  65535;    ##同时处理多少并发
 15 }


10、反向代理,负载均衡

vim /usr/local/nginx/conf/nginx.conf

 18 http {
 19         upstream westos {
            #ip_hash;            ##一个客户端值可以访问一个ip
 20                 server 172.25.38.2:80;
 21                 server 172.25.38.3:80;
            #server 127.0.0.1:80 backup;     ##备用
 22         }


121     server {
122                 listen 80;
123                 server_name www.westos.org;
124
125                 location / {
126                     proxy_pass http://westos;
127                 }
128         }
129 }


cd /usr/local/nginx/sbin/
./nginx -s reload

测试:访问http://172.25.38.1

测试机器需要修改本地解析
[root@foundation38 images]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.38.1 server1 www.westos.org bbs.westos.org
172.25.38.3 server3

[root@foundation38 kiosk]# curl www.westos.org
<h2>server2</h2>
[root@foundation38 kiosk]# curl www.westos.org
server3


实现:网页维护

11、编辑配置文件

vim /usr/local/nginx/conf/nginx.conf

 18 http {
 19         upstream westos {
 20                 server 172.25.38.2:80;
 21                 server 172.25.38.3:80;
 22                 server 127.0.0.1:80 backup;      ##127.0.0.1代表本机
 23         }

12、编辑本机的默认发布目录

cd /usr/local/nginx/html/
ls
cp index.html index.html.back
vim index.html

系统正在维护

13、重新加载nginx


cd /usr/local/nginx/sbin/
./nginx -t
./nginx -s reload


测试:关闭server2和server3,

[root@foundation38 images]# curl www.westos.org
系统正在维护

猜你喜欢

转载自blog.csdn.net/qq_41627390/article/details/87803468
今日推荐