目录
Nginx简介
1. 为什么使用Nginx
2. 安装Nginx
Nginx的核心功能
1. Nginx反向代理功能
2. Nginx的负载均衡
3 Nginx动静分离
Nginx简介
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。并发能力: 50,000
1. 为什么使用Nginx
-  高性能:Nginx是一个高性能的HTTP和反向代理服务器,能够处理大量的并发连接和请求,适合高流量的网站。 
-  高可靠性:Nginx使用事件驱动和异步处理方式,能够在保持高性能的同时,提供高可靠性的服务。 
-  灵活性:Nginx支持多种配置方式,可以通过配置文件灵活地调整其行为,满足不同的需求。 
-  扩展性:Nginx可以通过模块扩展其功能,支持负载均衡、缓存、SSL加密等多种功能。 
-  低资源消耗:Nginx是一个轻量级的服务器,占用的系统资源较少,适合在资源有限的环境中部署。 
-  跨平台:Nginx支持多种操作系统,包括Linux、Unix、MacOS等,具有很好的跨平台兼容性。 
-  社区支持:Nginx有一个活跃的开源社区,提供了大量的文档、教程和模块,方便用户学习和使用。 
-  安全性:Nginx提供了一些基本的安全功能,如防止DDoS攻击、防止SQL注入等,可以提高网站的安全性。 
-  易于维护:Nginx的配置文件简单明了,易于理解和维护,降低了运维的难度。 
-  支持多种协议:Nginx不仅支持HTTP和HTTPS协议,还支持其他协议如SMTP、POP3等,可以作为多种服务的代理服务器。 

2. 安装Nginx
nginx可以独立安装在一台服务器--也可以和项目在同一个服务器。
安装Nginx的依赖插件
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
下载nginx
源码。 编译---安装
nginx: download https://nginx.org/en/download.html
https://nginx.org/en/download.html

创建一个目录作为nginx的安装路径
mkdir /usr/nginx
解压
tar -zxvf nginx-1.26.1.tar.gz
进入解压后的目录
cd nginx-1.26.1
指定nginx的安装路径
./configure --prefix=/usr/nginx
编译和安装
nginx make install
nginx目录结构

启动nginx
nginx 启动
nginx -s stop 关闭
nginx -s reload 重新加载配置文件

访问nginx 80
http://nginx所在的ip:nginx的端口/

nginx配置文件
#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;
server {listen 81;server_name localhost;location /{root static;index main.html;}}
#gzip  on;server {listen       80; # 监听的端口号server_name  localhost; # 监听的主机名.域名
#charset koi8-r;
#access_log  logs/host.access.log  main;
# 资源/ location / {root   html; #根目录index  index.html main.html; # 资源}
#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;#    }#}
}Nginx的核心功能
1. Nginx反向代理功能
正向代理
代理的为客户端,对于服务器不知道真实客户的信息。例如:翻墙软件。

反向代理服务器
代理的为服务器端。对于客户来说不知道服务器的信息。例如: nginx

项目部署图

这是我的端口:
准备web项目---103。
准备nginx----101
启动web项目

配置nginx
server {
listen 82;
server_name localhost;
location /{
# 代理的服务器地址
proxy_pass http://192.168.111.132:8080;
}
}
启动nginx
./opt/nginx/sbin/nginx

2. Nginx的负载均衡
负载均衡(Load Balance [4])其意思就是把请求分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
web项目必须搭建的为集群模式。

web服务器项目至少搭建2台以上。192.168.111.132 8081 8082
nginx服务器
springboot项目

解压
java -jar xxx.jar

配置nginx完成负载均衡

重新加载nginx配置
/usr/nginx/sbin/nginx -s reload
测试
http://192.168.111.188:83/getInfo

负载均衡的策略
默认为轮询。
权重策略: 服务器硬件配置不同时。

ip_hash策略: 根据访问者客户的ip固定访问对应的web服务器。

花钱买第三方策略插件:
3 Nginx动静分离
动:动态资源[接口] 静:静态资源 [css js image]。
分离: 之前我们把静态资源和动态资源全部放在web服务器下。 把静态资源放入nginx服务器下。动态资源web服务器下

准备web项目

把静态资源放到nginx中

配置nginx

测试

下期写nginx的HA高可用的搭建