Nginx:engine X ,2002年,开源,商业版 Nginx是免费的、开源的、高性能的HTTP和反向代理服务器、邮件代理服务器、以及TCP/UDP代理服务器解决C10K问题(10K Connections)。
Nginx官网:http://nginx.org
nginx的其它的二次发行版:
Tengine:由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。从2011年12月开始,Tengine成为一个开源项目。
官网:http://tengine.taobao.org
OpenResty:基于 Nginx与 Lua 语言的高性能 Web 平台,章亦春开发。
官网:http://openresty.org/cn/
nginx可以作为静态的web资源服务器html,图片,js,css,txt等静态资源
结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求(uWSGI是转发给python)
http/https协议的反向代理
imap4/pop3协议的反向代理
tcp/udp协议的请求转发(反向代理)
正向代理:有多个用户的请求发送给代理服务器,代理服务器将请求发送给nginx服务器,nginx再出去寻找相对应的资源
反向代理:访问web服务先访问nginx,然后nginx将请求调度到背后的web服务器
1.模块化设计,模块化可进行二次开发,具有较好的扩展性
2.高可靠性,可以让他支持更高的并发
3.支持热部署,不停机更新配置文件,升级版本,更换日志文件
4.较低的内存消耗,10000个keep-alive连接模式下的非活动连接仅需2.5M内存
5.event-driven,aio,mmap,sendfile
1.虚拟主机(server):基于域名,基于主机,基于端口
2.支持keep-alive和管道连接(利用一个连接做多次请求)
3.访问日志(支持基于日志缓冲提高其性能)
4.URLrewrite:重定向用户的请求,nginx把用户的请求定位到别的路径。
5.路径别名:定义路径到别的位置
6.基于IP及用户的访问控制
7.支持速率限制及并发数限制,可以防止***性操作
8.重新配置和在线升级而无需终端客户的工作进程。
阅读官方文档
http://nginx.org/en/linux_packages.html#RHEL-CentOS
1.安装yum-utils
[root@localhost ~]# yum install yum-utils -y
2.手动创建epel
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
3.如果你需要使用最新的测试版可以执行以下命令,然后执行第4步,否则则略过直接执行第4步
[root@localhost ~]# yum-config-manager --enable nginx-mainline
4.然后执行yum安装
[root@localhost ~]# yum install nginx -y
nginx安装完毕
1.登录官网获取下载链接直接wget
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
2.解压文件
[root@localhost ~]# tar -xf nginx-1.14.2.tar.gz
3.检查当前环境是否符合编译要求,并生成makefile文件
[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
4.根据makefile文件内容生成指定的模块
[root@localhost nginx-1.14.2]# make
5.将生成的模块复制到相应的目录,如果目录不存在会创建目录
[root@localhost nginx-1.14.2]# make install
6.创建服务脚本
服务脚本可以参考yum安装所生成的脚本进行修改,主要修改PIDFile、ExecStart两个文件
[root@localhost ~]# scp /usr/lib/systemd/system/nginx.service 172.22.27.20:/usr/lib/systemd/system/nginx.service
[root@localhost nginx-1.14.2]# vim /usr/lib/systemd/system/nginx.service
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid #此项为进程的编号,可以在nginx的配置文件中查询的相应的位置
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf #编译安装时nginx二进制程序所在的位置以及配置文件所在的位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
7.将执行文件关联到sbin下
[root@localhost nginx-1.14.2]# ln -s /apps/nginx/sbin/nginx /sbin/nginx
8.测试服务脚本是否能启动
[root@localhost ~]# ss -tnl | grep 80
LISTEN 0 128 *:80 *:*
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# ss -tnl | grep 80
[root@localhost ~]#
#已经能够正常使用
编译安装完毕
在企业中每个与应用相关的服务的UID、GID和用户名必须全部相同,否则可能导致权限不足而造成网站的页面无法访问。比如,在第一nginx服务器上所使用的user为www,那么在其余的服务器上也必须为www,并且他们的uid和gid也必须要全部相同
为nginx创建一个用户
[root@localhost ~]# useradd -u 2000 nginx
修改配置文件修改user为nginx,表示nginx启动后生成的work进程是以nginx用户运行的
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
user nginx;
启动服务
[root@localhost ~]# systemctl start nginx
查看nginx的主进程和work进程以谁的身份运行
[root@localhost ~]# ps aux
root 23642 0.0 0.2 46056 1164 ? Ss 21:48 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 23643 0.0 0.4 46508 1936 ? S 21:48 0:00 nginx: worker process
配置work进程的进程数,进程的最大数为cpu的核心数
worker_processes有两种配置方法
修改配置文件,指定进程数为2
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
worker_processes 2;
检查配置文件
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
重读配置文件后查看进程
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef | grep nginx
root 23642 1 0 21:48 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 23787 23642 0 22:01 ? 00:00:00 nginx: worker process
nginx 23788 23642 0 22:01 ? 00:00:00 nginx: worker process
#work进程数已变为2个
直接指定进程数有缺个缺点,会导致其工作进程会在CPU的各个核心上来回漂移,会加大cpu对进程的资源分配与回收以及内存管理,影响了nginx服务的性能,所以需要将其绑定在一个固定的cpu核心上
需要配合worker_cpu_affinity参数一起使用
修改配置文件
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
检查配置文件,重启服务
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
查看work进程的绑定情况
[root@localhost ~]# ps axo pid,cmd,psr | grep nginx
23856 nginx: master process /apps 2
23857 nginx: worker process 0
23858 nginx: worker process 1
23861 grep --color=auto nginx 1
在nginx 1.8版本之后还支持auto的选项自动识别cpu核心数来设置启动的work进程数
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
worker_processes auto;
worker_cpu_affinity 0001 0010 0100 1000;
检查配置文件,重启服务
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
查看work进程的绑定情况
[root@localhost ~]# ps axo pid,cmd,psr | grep nginx
23882 nginx: master process /apps 0
23883 nginx: worker process 0
23884 nginx: worker process 1
23885 nginx: worker process 2
23886 nginx: worker process 3
23889 grep --color=auto nginx 2
nginx的worker进程的最大连接数,当nginx作为反向代理时最大连接数的设定值需要减半,因为nginx需要先和客户端建立连接,还需要和后端建立连接所以需要减半
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
events {
worker_connections 100000;
}
http字段内可以配置多个虚拟主机
配置虚拟主机
1.创建一个存放配置文件的目录
[root@localhost ~]# mkdir /apps/nginx/conf/servers
2.修改配置文件
http{
...
include /apps/nginx/conf/servers/*.conf; #将servers下的所有以conf结尾的文件加载到配置文件内
}
3.在servers下创建虚拟主机的配置文件
vim /apps/nginx/conf/servers/virtualserver.conf
server {
listen 80;
server_name www.mylinuxops.com;
location / {
root /data/html;
index index.html;
}
location /linux36 {
root /data/html;
index index.html;
}
location /python {
root /data/html;
index index.html;
}
}
4.检查配置文件,重读配置文件
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
5.使用域名访问站点
[root@localhost ~]# curl -I www.mylinuxops.com
HTTP/1.1 404 Not Found
Server: nginx/1.14.2
Date: Mon, 27 May 2019 15:57:42 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
#显示没有找到页面
6.创建站点目录文件,并写入测试页面
[root@localhost ~]# mkdir /data/html/{linux36,python} -p
[root@localhost ~]# echo "mylinuxops.com" > /data/html/index.html
[root@localhost ~]# echo "linux36" > /data/html/linux36/index.html
[root@localhost ~]# echo "python" > /data/html/python/index.html
7.再次测试
[root@localhost ~]# curl www.mylinuxops.com
mylinuxops.com
[root@localhost ~]# curl www.mylinuxops.com/linux36/
linux36
[root@localhost ~]# curl www.mylinuxops.com/python/
python
新建一个server段
[root@localhost ~]# vim /apps/nginx/conf/servers/virtualserver.conf
server {
listen 80;
server_name www.mylinuxops.net;
location / {
root /data/www;
index index.html;
}
location /linux36 {
root /data/www;
index index.html;
}
location /python {
root /data/www;
index index.html;
}
}
检查配置文件,重启服务
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
创建网站页面
[root@localhost ~]# mkdir -p /data/www/{linux36,python}
[root@localhost ~]# echo "linux36 net" > /data/www/linux36/index.html
[root@localhost ~]# echo "python net" > /data/www/python/index.html
[root@localhost ~]# echo "mylinux.net" > /data/www/index.html
测试
[root@localhost ~]# curl www.mylinuxops.net
mylinux.net
[root@localhost ~]# curl www.mylinuxops.com
mylinuxops.com
[root@localhost ~]# curl www.mylinuxops.com/python/
python
[root@localhost ~]# curl www.mylinuxops.net/python/
python net
[root@localhost ~]# curl www.mylinuxops.net/linux36/
linux36 net
[root@localhost ~]# curl www.mylinuxops.com/linux36/
linux36
原文:https://blog.51cto.com/11886307/2403940