1、什么是Nginx?
- Nginx 是高性能的 HTTP 和反向代理的服务器,特点是占用内存少。处理高并发能力是十分强大的,能经受高负载的考验,有报告表明能支持高达 50,000 个并发连接数
- Nginx 可以作为静态页面的 web 服务器,同时还支持 CGI 协议的动态语言,比如 perl、php等。但是不支持 java。Java 程序只能通过与 tomcat 配合完成。Nginx 专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率
- Nginx支持热部署。它的启动非常容易,并且几乎可以做到不间断运行
比喻Nginx:就好比你去餐馆吃饭,nginx就相当于服务员,tomcat就相当于厨师,你要一瓶饮料(相当于静态资源),服务员就能给你拿,而你说你要一盘鱼香肉丝(动态资源),那服务员只好给厨师讲,让他给你做。
2、几个小概念
2.1、正向代理
客户端(浏览器)并不是直接通过发送请求访问网址,而是通过配置一个游览器配置代理服务器,再通过代理服务器访问网址返回要求请求的内容
2.2、反向代理
客户端(浏览器)并不是直接通过发送请求到达tomcat端口,而是通过一个反向代理服务器端口,再请求tomcat连接
2.3、负载均衡
单个服务器解决不了,我们通过增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器的情况改为将请求分发到多个服务器上,将负载分发到不同服务器(请求平均分发到服务器),这就是负载均衡
2.4、动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同服务器解析,加快解析速度,降低原来单个服务器的压力
当客户端来向tomcat发送访问资源请求的时候,不是直接访问,而是通过中间中介:Nginx(内存访问的服务器动态资源和静态资源)来拿,要什么就拿什么!
3、Linux部署Nginx
一、打开liunx在空白桌面右击打开终端, 输入
cat /etc/redhat-release # 查看liunx操作系统版本
二、下载Nginx
链接地址:http://Index of /download/ (nginx.org)
这里选择nginx-1.22.1.tar.gz版本,用wget命令下载
wget http://nginx.org/download/nginx-1.22.1.tar.gz
这里要注意自己去链接地址下载到本地vms文件夹下,可以用xftp拖拽到Linux系统新建的目标文件夹。centos具体目录为root下
下载完后要安装pcre openssl gcc库及源码包
1、安装Nginx前需要一些依赖库,创建nginx文件夹:
mkdir -p nginx
2、进入nginx文件夹:
cd nginx
3、安装pcre openssl gcc库及源码包:
yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++
三、安装Nginx
1、解压
tar zxvf nginx-1.22.1.tar.gz
进入解压后的nginx-1.22.1文件夹
cd nginx-1.22.2
2、创建Nginx用户
[root@nginx nginx-1.22.1] # useradd nginx -s /sbin/nologin -M
[root@nginx nginx-1.22.1] # ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx nginx-1.22.1] # make && make install
[root@nginx nginx-1.22.1] # ln -s /usr/local/nginx/sbin/* /usr/local/sbin
四、启动Nginx服务器
1、检查配置文件
[root@nginx nginx-1.22.1] # /usr/local/sbin/nginx -t
出现以下情况证明配置文件完整:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2、启动
[root@nginx nginx-1.22.1] # /usr/local/sbin/nginx -t
3、查看端口、进程
[root@nginx nginx-1.22.1] # netstat -lntup | grep nginx
[root@nginx nginx-1.22.1] # lsof -i :80
输入自己的IP网点,可以看到,启动后有两个nginx进程运行则表示启动成功!!!
安装借鉴:
https://blog.csdn.net/qq_60870118/article/details/129045912
可能会遇到的问题:
Failed to start nginx.service: Unit not found.
参考https://blog.csdn.net/weixin_44080445/article/details/106197213即可
Nginx常用命令(启动、重启、关闭、检查)
https://blog.csdn.net/qq_28624243/article/details/115598416
首先进入到目录:
cd /usr/local/nginx/sbin
查看版本号
./nginx -v
启动Nginx+关Nginx+重载Nginx
./nginx ./nginx -s stop ./nginx -s reload
4、Nginx配置文件和结构
文件位置:/usr/local/nginx/cong/nginx.conf
源码
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#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 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
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
#
#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 ~ /\.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;
# }
#}
}
结构
... #全局块
events { #events块
...
}
http #http块
{
... #一个http全局块开始
server #多个server块
{
... #一个server全局块
location [PATTERN] #多个location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
... #一个server全局块和多个location块
}
... #http全局块结束
}
第一部分:全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。 比如上面第一行配置的:
worker_processes 1;
这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。
第二部分:events 块
events {
worker_connections 1024;
}
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 work process 可以同时支持的最大连接数等。上述例子就表示每个 work process 支持的最大连接数为 1024。
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
第三部分:http 块
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http 块也可以包括 http 全局块、server 块。
- http全局块:
http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
- server块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。
每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 location 块。
全局server块:最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。
location块:一个 server 块可以配置多个 location 块。这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
5、Nginx实践
5.1、ngingx配置实例 1-反向代理
1、实现效果:
打开浏览器,在浏览器的地址栏中 www.123.com,跳转到 liunx 系统中 tomcat 主页面中
(1)第一步进入HOSTS文件
(2)tomcat
在liunx系统中 安装 tomcat, 使用默认端口 8080
进入tomcat 的 bin目录中, ./startup.sh 启动
对外开放访问的端口
firewall-cmd -add-port=8080/tcp --permanent
firewall-cmd -reload
查看开放的端口
firewall-cmd --list-all
3、访问过程分析
通过nginx.conf配置文件中端口80和地址,使得nginx直接转到Tomcat地址8080端口,这就是反向代理
4、具体实现
开启tomcat+nginx
开启端口或者关闭防火墙
配置nginx中 nginx.conf 以及 hosts 开启域名重定向
Nginx多地址配置
进入nginx的conf文件然后配置多个http的地址和端口,但是映射同一个要跳转的页面
5.2、ngingx配置实例 2-负载均衡
nginx.conf配置
在nginx.conf中加上以下内容
负载均衡效果
负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。快速增长的访问量和数据流量催生了各式各样的负载均衡产品,很多专业的负载均衡硬件提供了很好的功能,但却价格不菲,这使得负载均衡软件大受欢迎,nginx就是其中一个,在linux下有Nginx、LVS、Haproxy等等服务可以提供负载均衡服务,而且Nginx提供了几种分配方式:
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器宕掉,能自动剔除。
2、weight
weight 代表权重,默认为1,权重越高被分配用户越多。
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
3、ip_hash
每个请求按照ip的hash结果进行分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
4、fair(第三方)
按照后端服务器的响应时间来分配请求,相应时间短的优先分配/
5.3、ngingx配置实例 3-动静分离
为了提高访问效率,动态和静态给不同服务器处理
www和image分别放置a.html和01.jpg
在nginx.conf修改IP和location(为两文件的地址)
autoindex on可以不用,主要是列出你当前文件夹的内容
5.4、ngingx配置实例 4-高可用集群
5.4.1、高可用集群的作用?
在请求过程中nginx可能宕机,所以要配置多态nginx
5.4.2、如何实现高可用?
- 需要两台及多台nginx服务器
- 需要keepalived
- 需要虚拟ip
就是说当keepalived检测到主机nginx1若是宕机,通过绑定的虚拟ip复制到备份从nginx中,扶持nginx2上位(类似于Redis哨兵?cluster?)
1、使用主从节点分别安装nginx
主节点绑定虚拟ip即可
从节点修改两处
2、安装keepalived(在nginx内部安装,进入nginx的usr)
yum install keepalived
安装之后,在etc生成目录keepalived,有文件keepalived.conf
修改etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 10.10.10.130
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/nginx/nginx_check.sh"
interval 2 # 检测脚本执行的间隔
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 # VRRP H 虚拟地址
}
}
在/usr/local/nginx/中添加一个nginx_check.sh脚本
#!/bin/bash
A=`ps -C nginx ¨Cno-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
启动nginx和keepalived
service keepalived start
3、在浏览器地址栏输入虚拟ip进行测试,之后将主服务器停掉,然后再访问ip,查看高可用效果
主节点还活着展示效果
主节点宕机,从节点上位效果展示
仍然可以访问!!!
ADD:
keepalived.conf文件配置及参数详解
从节点配置注意事项
6、Nginx原理
一个Master多个Worker的好处?
一旦进行微服务,Nginx一定不能宕机!
可与使用nginx -s reload重新热部署
需要设置多少个worker?
worker数量最好和CPU数量一致
最大连接数
worker_connections:最大连接数
worker_processes:几个worker
Nginx面试题
1、Nginx和Tomcat区别
- Nginx常用做静态内容服务和代理服务器,直接将外来请求,转发给后面的应用服务器,比如:tomcat等。
- Nginx在静态上的功能非常强大,主要直接把外来的请求做分发,不做后端处理。
- 而Tomcat则是需要做后端处理,比如:使用tomcat用在java后台程序上,让java web泡在里面的应用服务容器。
一句话总结:如果想要使用 Java 程序来动态生成资源内容,就会使用到Tomcat为代表的应用服务器。
2、最大并发/最大连接数?