#通过nginx部署前后端项目
#虚拟主机04--开启科研管理系统
server {
listen 80;
#域名,该域名指向公网ip
server_name scientific.lovelsj.com;
#前端地址,这里是绝对路径
location / {
root /www/dist;
index index.html index.htm;
}
#后端路径,注意这里的Scientific是application.yml里面的server-servlet-context-path: /Scientific,寻找/Scientific下的接口
location /Scientific{
proxy_pass http://192.168.30.131:8060;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx担当高性能的web服务器负载均衡器,流量网关,日志网关,日志服务器,贯穿了整个项目



2. 配置静态ip地址








yum install -y gcc
./configure ==prefix=/usr/local/nginx

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

./nginx
./nginx -s stop
./nginx -s quit
./nginx-s reload

systemctl stop firewalld.service
systemctl disable firewalld.service
firewalld-cmd --zone=public --add-port=80/tcp --permanent
systemctl status firewalld
firewall-cmd --reload

随后能进去网页


vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl reload nginx
systemctl enable nginx.service


更改默认页
就是更改index.html
基本运行原理

客户端发起一个请求,nginx端主目录下/sbin有个nginx可执行文件,将这个文件跑起来,会开启master的主的进程,在主进程中读取配置文件并且再次做一个校验,然后开启子进程worker,主进程不处理业务,子进程解析请求
#工作的子进程个数,一个cpu内核对应一个进程
worker_processes 1;
events {
//每一个worker进程对应的连接数
worker_connections 1024;
}
#核心配置
http {
#引用其他的配置文件mime.types到主配置文件里,记录所有mime的type,mime的type就是请求的头,头里面记录着返回或者发送的文件是什么类型
include mime.types;
#如果mime.types里面没有对应的类型,哪么就用默认流形式
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#虚拟主机,每一个主机之间互相不干扰
server {
#监听80端口
listen 80;
server_name localhost; #域名,主机名
#http://lsj.cash/xxoo/index.html,location是匹配域名后面的资源 /xxoo/index.html
location / {
#相对路径
root html;
index index.html index.htm;
}
#http://lsj.cash/50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}




主机记录填写*这个是泛解析,前缀不管是啥都行






#需要注意,端口号+域名是唯一的就行
#虚拟主机01
server {
listen 80;
server_name localhost;
#前面没有/,就是相对路径,相对于nginx的安装目录
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#虚拟主机02
server {
listen 80;
server_name video.lovelsj.com;
#没有从根目录开始就是相对路径,从根目录开始就是绝对路径: /www/video;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#虚拟主机03
server {
listen 80;
server_name pic.lovelsj.com;
#没有从根目录开始就是相对路径,从根目录开始就是绝对路径: /www/pic;
location / {
root /www/pic;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tNKV0Ful-1656209981199)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f39bf796bebe4b50b4ddd86e79bf6971~tplv-k3u1fbpfcp-watermark.image?)]
2. video.lovelsj.com

3. pic.lovelsjcom



[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FYfZnIvF-1656209981200)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4e5d079e7765475bb8d457306b2f0d12~tplv-k3u1fbpfcp-watermark.image?)]
反向代理再系统架构中的应用场景
nginx做请求中转
Nginx的反向代理配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FDID0Sez-1656209981200)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f47195e648474d75b9f91be3792b9a83~tplv-k3u1fbpfcp-watermark.image?)]

故障转移
nginx后面有许多业务服务器,负载均衡就是有一台服务器不可用了,就可以吧故障转移,另一台服务器提供的服务与第一台的服务相同,需要被负载均衡的服务器焦作服务器的集群,全都服务都一样,当用户请求数据的时候,nginx反向代理给集群的每一台服务器这种负载均衡叫做(轮询,但是无法保持会话)
权重(weight)
比例
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D5VBWjBK-1656209981200)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c219342ee4a9496ab9c86e75c260ca5d~tplv-k3u1fbpfcp-watermark.image?)]



下面是不太常用的负载均衡策略


首先http端口是80,https端口是443
HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。
HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。
HTTPS和HTTP的区别主要如下:
1、https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用。
2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。
3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
4、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。


点击下载


一共两个,一个是pem,一个是key,证书和私钥



./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#依赖
yum -y install pcre-devel openssl openssl-devel



下周复习并发和多线程