基本介绍

Nginx与它的老大哥Apache相比有很多改进之处,
比如,

  • 在性能上,Nginx占用的系统资源更少,能支持高并发连接(特别是静态小文件场景下),达到更高的访问效率;
  • 在功能上,Nginx不但是一个优秀的Web服务软件,还可以作为反向代理负载均衡及缓存服务使用;
  • 在安装配置上,Nginx更为方便,简单,灵活

Nginx的重要特性

  • 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
  • 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB
  • 可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能
  • 具备Squid等专业缓存软件等的缓存功能。
  • 支持异步网络I/O事件模型epoll(linux2.6+)

Nginx软件的主要企业功能应用
(1)作为Web服务软件
(2)反向代理或负载均衡服务
(3)前端业务数据缓存服务

为什么Nginx总体性能比Apache高?
• Nginx使用最新的epoll(Linux2.6内核)和kqueue(freebsd)异步网络I/O模型,而Apache使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid,Memcached软件采用的都是epoll模型。
• 处理大量连接的读写时,Apache所采用的select网络I/O模型比较低效。
image.png

安装、基本测试

当前使用环境
版本 centos7.6
内核 3.10
image.png

安装

nginx软件版本1.18
nginx-1.18.0.tar.gz

yum install -y pcre-devel openssl-devel gcc-c++ gcc #用本地yum仓库安装依赖包
#wget -q http://nginx.org/download/nginx-1.10.2.tar.gz #下载软件源码包
useradd -s /sbin/nologin -M www #创建程序用户
tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解压缩
cd /usr/src/nginx-1.10.2
./configure —user=www —group=www —prefix=/usr/local/nginx —with-http_stub_status_module —with_http_ssl_module #预配置
image.png
make
image.png
make install #编译和安装
image.png
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #给命令做软连接,以便PATH能找到
image.png
/usr/local/nginx/sbin/nginx #启动nginx

[root@nginx-web01 nginx-1.18.0]#
[root@nginx-web01 nginx-1.18.0]# nginx -h
nginx version: nginx/1.18.0
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 #版本及config展示
-t : test configuration and exit #config文件test格式
-T : test configuration, dump it and exit #config文件test格式并下载
-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/local/nginx/) #指定默认配置文件路径
-c filename : set configuration file (default: conf/nginx.conf) #指定默认配置文件
-g directives : set global directives out of configuration file #从配置文件中设置全局指令

启停nginx

启动命令直接运行nginx即可
停止命令
nginx -s reload 平滑重新加载
ningx -s stop 平滑停止
image.png

测试nginx-测试服务可以运行

修改配置文件

配置文件在安装目录的./conf下,比如这里安装的目录为/usr/local/nginx

[root@nginx-web01 nginx]# cat conf/nginx.conf
#user nobody; #nginx启用用户
worker_processes 1; #nginx的CPU核数
error_log logs/error.log; #错误日志路径,注意是相对路径
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid; #nginx的pid文件路径
events { #事件设置
worker_connections 1024; #最大连接数
}
http { #http网页设置
include mime.types; #包含的文件类型
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘ #log_format日志内容
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
sendfile on; #高效传输打开
tcp_nopush on; #
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on; #高级压缩
server { #虚拟主机的服务配置;可以有多个,就表示多个虚拟主机地址
listen 80; #监听端口
server_name www.91test.com; #虚拟主机服务配置的地址
#charset koi8-r;
access_log logs/host.access.log main;
location / { #服务的文件路径,这里的根意思是/usr/local/nginx以他为根
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
#
#需要php访问的配置
#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匹配的配置
#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;
# }
#}
}
[root@nginx-web01 nginx]#

修改网页文件

网页文件在安装目录的./heml下,比如这里安装的目录为/usr/local/nginx
image.png

测试网页访问

image.png
浏览器访问,需要在本机hosts文件添加解析域名
192.168.0.91 www.91test.com
192.168.0.92 www.92test.com

linux文件路径/
etc/hosts

win文件路径
C:\Windows\System32\drivers\etc

保存退出后浏览器访问
image.png
image.png

虚拟主机概念和类型介绍

虚拟主机概念

所谓虚拟主机,在Web服务里就是一个独立的网站站点(www.baidu.org),这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问
这个独立的站点在配置里是由一定格式的标签段标记,对于Apache软件来说,一个虚拟主机的标签段通常被包含在内,而Nginx软件则使用一个server{}标签来标示一个虚拟主机一个Web服务里可以有多个虚拟主机标签对,即同时可以支持多个虚拟主机站点。

虚拟主机类型

常见的虚拟主机类型有如下几种。

• 基于域名的虚拟主机

所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站都是使用基于域名的虚拟主机,例如:www.etiantian.org。nginx.conf中对应server_name字段

• 基于端口的虚拟主机

同理,所谓基于端口的虚拟主机,意思就是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机地址里要带有端口,例如:http://www.baidu.com:80。主要体现在listen字段

• 基于IP的虚拟主机

同理,所谓基于IP的虚拟主机,意思就是通过不同的IP区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见,一般不同业务需要使用多IP的场景都会在负载均衡器上进行VIP绑定,而不是在Web上通过绑定IP区分不同的虚拟机。
三种虚拟主机类型均可独立使用,也可以互相混合一起使用

测试nginx-多域名的虚拟主机

上面的测试中,测试了nginx服务可以正常运行,这里测试多域名的服务虚拟主机
[root@nginx-web01 conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server { #server1虚拟主机
listen 80;
server_name www.91test.com;
access_log logs/host.access.log main;
location / {
root html/www; #对应创建虚拟主机页面文件路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server { #server2虚拟主机
listen 80;
server_name ops.91test.com;
access_log logs/host.access.log main;
location / {
root html/ops; #对应创建虚拟主机页面文件路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@nginx-web01 conf]#
在对应的网页文件目录创建网页文件,注意要写hosts文件,然后访问虚拟主机地址测试
image.png
这里有一个地方需要注意,如果通过ip访问的话,nginx并不知道要访问的是哪个网站。
因此,他默认从第一个虚拟主机的访问配置站点开始提供的服务
image.png
emmm…这是个问题啊,要怎么解呢?

备注

Nginx的功能模块说明

image.png

Nginx的规范化配置文件

上面的配置中,例如多虚拟主机的情况下,每个虚拟注意可以有自己的conf配置,比如www.conf,ops.conf
具体配置如下。
原有的nginx配置文件
[root@nginx-web01 conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
include www.conf; #这里原本的server虚拟主机现在成为了include引用的模式,www.conf具体什么样的呢?
include ops.conf;
}
[root@nginx-web01 conf]# #这样式儿的
[root@nginx-web01 conf]# cat www.conf
server {
listen 80;
server_name www.91test.com;
access_log logs/host.access.log main;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@nginx-web01 conf]#
还是能够正常访问的
image.png
同样的其他同类型的文件也可以规范处理的