Nginx基础安装配置
1. 关于Nginx介绍
Nginx是异步框架的web服务器,也可以用作反向代理、负载均衡以及作为缓存服务器。Nginx是目前互联网公司web服务器的主流技术,用于处理高并发甚至海量并发的网站数据。Nginx具有社区版和商业版,社区版是完全开源的,Tengine就是淘宝在Nginx基础上二次开发,以获取更高的稳定性和并发能力,已经在淘宝,天猫等海量数据的电商网站上经过了双十一的技术洗礼,足以证明其稳定性和高性能。
1.1 Nginx主要特性
- 高并发、高性能 一台普通的服务器可以轻松处理上万并发链接,一般单台服务器最多建议处理三万左右的并发;
- 模块化设计 基于模块化设计,具有非常好的扩展性,可以通过加载、卸载某个模块(注意:模块动态加载在
Nginx V1.9.11版本之后才支持)以实现相应的功能; - 热部署、热更新
Nginx支持配置文件的热更新,版本热升级、动态加载模块、日志热切换; - 内存低消耗 据统计在
10000个keep-alive连接模式下的非活动连接,仅仅消耗内存2.5M; - 配置、维护简单
Nginx的配置非常简单,对于运维同学来说非常友好;
1.2 Nginx基本功能
web服务器 这是Nginx最基本的功能,也是非常重要的功能之一;- 反向代理服务器
Nginx可以作为http协议的反向代理服务器,也是生产环境中最常用的功能之一; FastCGI(php)、uWSGI(python)代理服务器 生产环境上经常使用Nginx作为客户端请求后端应用服务(注意:此时Nginx在用户看来依然是反向代理服务器,只不过这里代理的请求不再是http协议,而是跟后端服务相关的协议,比如后端服务如果是php开发的,则是fastCGI协议代理)- TCP/UDP代理服务器 也叫“调度器”,生产环境中,一些并发量并不大的情况下,有时候也使用Nginx作为四层调度器
- Mail邮件代理服务器 可以作为邮件代理服务器,不过几乎不使用;
1.3 Nginx基础架构
如下图,Nginx为master/workers结构,一个master主进程,负责管理和维护多个worker进程,真正接收并处理用户请求的其实是worker进程,master不对用户请求进行处理。即master主进程负责分析并加载配置文件,管理worker进程,接收用户信号传递以及平滑升级等功能。另外,Nginx具有强大的缓存功能,其中Cache Loader负责载入缓存对象,Cache Manaer负责管理缓存对象
2.Nginx安装与访问测试
这里我们先介绍yum安装方式,稍后我们再介绍编译安装。需要注意的是Centos基础yum源中是没有Nginx包的,我们需要添加好epel源。目前epel源中最新版本为Nginx V1.16.1,这也是官方最新稳定版。
【安装】
[root@Proxy ~]# yum install -y epel-release
...
[root@Proxy ~]# yum install -y nginx
这里安装好的版本为
nginx.x86_64 1:1.16.1-1.el7
我们可以看下nginx生成了很多文件,其中/usr/sbin/nginx为主程序文件,/etc/nginx为nginx的配置目录,还有一些是帮助文件,默认网页文件,日志文件等。我们先来看看/usr/sbin/nginx帮助命令(这是一个可执行二进制文件),如下:
[root@Proxy ~]# nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
-V: 该参数用于显示
nginx版本信息以及编译加载了哪些模块;
-t: 用于检查
nginx配置文件语法是否正确,我们在生产环境上修改好配置文件后,一定要先使用该参数进行检测;
-s: 给
nginx传递信号,其中stop强制停止、quit优雅退出: 即等待连接关闭之后再退出;reopen用于重新打开日志文件,一般用于日志文件的切割;reload重新加载配置文件;
-g: 用于指定指令,该指令高于配置文件中的设置。如
nginx -g 'daemon off;',将nginx设置为前台运行;
【启动】
nginx启动非常简单,我们可以通过systemctl start nginx启动,或添加开机自启动功能systemctl enable nginx.service.也可以直接使用命令nginx启动,如下:
[root@Proxy ~]# nginx
[root@Proxy ~]# ss -lntp|grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=99658,fd=6),("nginx",pid=99657,fd=6))
为了避免防火墙以及“selinux”对我们的实验产生影响,先关闭防火墙
firwalld/iptables以及selinux
[root@Proxy ~]# setenforce 0
setenforce: SELinux is disabled
[root@Proxy ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
[root@Proxy ~]# systemctl disable firewalld
[root@Proxy ~]# systemctl stop firewalld
【访问】
[root@Proxy ~]# curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 13 May 2020 20:09:19 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes
这样最简单的nginxweb服务器就已经启动好了。我们可以写个简单的test.html页面来验证是否正确,如下:
[root@Proxy ~]# echo '我们正常运行了' > /usr/share/nginx/html/test.html
[root@Proxy ~]# curl localhost/test.html
我们正常运行了
[root@Proxy ~]#
3. Nginx简单配置
我们通过yum方式安装的nginx程序,配置文件采用一主多子的配置方式,即一个主配置文件,通过将不同功能或者不同模块的子配置文件进行包含引入的方式,这样可以做到配置文件的清晰、便于管理。这种配置方式的思想也是一个优秀的工程师需要借鉴的地方。下面我们来看下nginx默认的主配置文件,部分内容省略。
[root@Proxy ~]# cat /etc/nginx/nginx.conf.default |grep -E -v '^.*#|^$'
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.1 main配置端
user nginx;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_processes 1;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
这些配置为nginx核心配置(也称为主配置main),可以使用的参数可以参考官方文档。这里有一些参数非常重要,涉及到nginx的性能优化。后续篇幅进行深入且全面的介绍。此处仅介绍默认配置文件中出现的参数:
user nginx: 用来指定运行nginx程序的用户。注意该用户是安装nginx时程序自动创建的,如果编译安装的话,需要手动创建相应的用户。
我们在安装
nginx时,有一个依赖包nginx-filesystem.noarch 1:1.16.1-1.el7被安装,我们可以查看下该依赖包安装时都执行了什么脚本
[root@Proxy ~]# yum deplist nginx|grep filesystem
依赖:nginx-filesystem
provider: nginx-filesystem.noarch 1:1.16.1-1.el7
[root@Proxy ~]# rpm -q --scripts nginx-filesystem
preinstall scriptlet (using /bin/sh):
getent group nginx > /dev/null || groupadd -r nginx
getent passwd nginx > /dev/null || \
useradd -r -d /var/lib/nginx -g nginx \
-s /sbin/nologin -c "Nginx web server" nginx
exit 0
即:nginx用户就是在这个程序安装时添加。
| 需要注意的是 , Nginx主进程。即master进程会以root身份运行,worker进程会以指定的nginx用户运行。所以用户的某个请求能否处理,则要看worker进程的用户权限是否足够。而能否监听某个端口,则是由master用户权限决定的。 |
worker_processes auto: 用来指定worker进程的个数,auto则会根据系统的cpu个数进行设置,比如我有4颗cpu,则worker进程数为4。
error_log /var/log/nginx/error.log: 用来定义nginx错误日志位置,我们也可以对不同的虚拟主机或者URL定义不用的错误日志。该参数可以在main/http/mail/stream/server/location部分中应用。
pid /run/nginx.pid: 用来定义nginx主进程的pid文件路径。注意:对于nginx应用的启动停止重载等信号传递操作都是依赖于该文件;
include /usr/share/nginx/modules/*.conf : 用于指定nginx模块配置文件所包含的子配置文件;
events {...}: 用于定义时间驱动相关配置,该配置与连接的处理密切相关,其中最重要的几个指令如下:
use epoll; # 定义nginx使用的哪种事件驱动类型,在centos中性能最好的是epoll模型
worker_connections 65535; # 定义每个worker进程可以处理的连接数
accept_mutex on; # 处理新连接的方法。on是指由各个worker进程轮流处理。off则会通知所有的worker进程,但是只有一个worker进程获得处理连接的权限。在Centos7将使用`reuseport`会有更好性能;
accept_mutex_delay 500ms; 时间可以设置,表示当一个worker进程在处理新连接时,多长时间以后才会重新接受下一个新请求。accept_mutex启动时才会启用该参数
3.2 http模块配置段
在上面的配置文件实例中,处理main配置段,还有http模块配置段,这两个参数是平行关系,即不是包含与被包含的关系。平行关系还有mail、stream。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
我们可以看到在http配置段中,我们可以设置多个server配置,该server就是用来配置虚拟主机的,可以基于IP地址,也可以基于port。当然生产环境中更多的还是使用基于域名的方式来配置虚拟主机。在server配置段内还可以在配置多个location字段,该字段用来配置虚拟主机不同URL的响应方式。这就是nginx作为web应用最简单的配置格式
4.配置虚拟主机
基于域名最简单的配置:
我们在/etc/nginx/conf.d下创建子配置文件。至于为什么在这里创建,可以看上面的http配置段中include /etc/nginx/default.d/*.conf;生产环境中,我们一般一个虚拟主机会单独创建一个配置文件,希望大家也有这样的好习惯。
# 创建几个备用目录,作为不同虚拟主机的家目录
[root@proxy tmp]# mkdir /data/nginx/{blog,erp} -pv
mkdir: 已创建目录 "/data/nginx"
mkdir: 已创建目录 "/data/nginx/blog"
mkdir: 已创建目录 "/data/nginx/erp"
【创建域名blog|www.blog.com的虚拟主机】
[root@Proxy ~]# vim /data/nginx/blog/blog.conf
server {
listen 80;
server_name blog.com www.blog.com;
root /data/nginx/blog;
}
[root@Proxy ~]# vim /data/nginx/erp/erp.conf
server {
listen 80;
server_name erp.com www.erp.com;
root /data/nginx/erp;
}
这里我们写了四行配置,其实listen可以省略,默认监听在80端口;root也可以省略,如果我们不在server中指定,则会继承http中定义的root目录作为自己的家目录
【创建测试页面】
(py3.6) [root@Master nginx]# echo "This is erp page" > /data/nginx/erp/index.html
(py3.6) [root@Master nginx]# echo "This is blog page" > /data/nginx/blog/index.htmlecho "This is erp page" > /data/nginx/erp/index.html
【测试基于域名的虚拟主机】
(py3.6) [root@Master nginx]# nginx -s reload
(py3.6) [root@Master nginx]# curl erp.com
This is erp page
(py3.6) [root@Master nginx]# curl blog.com
This is blog page
(py3.6) [root@Master nginx]#
【基于IP地址的虚拟主机】
这种方式很少使用。做个简单示例
(py3.6) [root@Master nginx]# cat /etc/nginx/conf.d/ip.conf
server {
listen 192.168.198.150:89;
root /data/nginx/ip;
}
(py3.6) [root@Master nginx]# curl 192.168.198.150:89
This is IP page
(py3.6) [root@Master nginx]# curl 127.0.0.1
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
【基于端口号的虚拟主机】
这种情况也不是很常用,一般跟基于ip的虚拟主机方式一`样,多数用于内部应用。
(py3.6) [root@Master nginx]# cat /etc/nginx/conf.d/port.conf
server {
listen 8080;
root /data/nginx/erp;
}
| 结论:所谓基于域名、IP或端口的虚拟主机。其实就是指当客户端通过不同的域名、IP或端口进行请求时,服务端会给出不同的响应。 |