介绍
知识网络结构图
Nginx
是一个高性能的HTTP
和反向代理服务器,特点是占用内存少,并发能力强,事实上nginx
的并发能力确实在同类型的网页服务器中表现较好。nginx
专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告nginx
能支持高达50000 个并发连接数。
代理
正向代理
局域网中的电脑用户想要直接访问网络是不可行的,只能通过代理服务器来访问,这种代理服务就被称为正向代理。
反向代理
客户端无法感知代理,因为客户端访问网络不需要配置,只要把请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据,然后再返回到客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址
动静分离
为了加快网站的解析速度,可以把动态页面和静态页面交给不同的服务器来解析,加快解析的速度,降低由单个服务器的压力
- 动静分离之前的状态
安装
nginx下载地址 下载完成后直接解压放在指定的目录即可
# 在 nginx 根目录下执行
./nginx -v # 检查是否正常输出版本号
启动
# 启动有三种方式
./nginx # 直接回车
start nginx # 或者双击 nginx.exe 文件
停止
./nginx -s stop
./nginx -s quit
重启
./nginx -s reload
全局使用 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;
#gzip on;
server {
listen 8081;
server_name localhost;
}
# another virtual host using mix of IP-, name-, and port-based configuration
# 县域项目
server {
listen 8080;
server_name localhost;
location / {
# root D:/work/authine/xianyu_project_frontEnd/newVersion/6.5.4/entries/portal/dist;
root D:/work/authine/shaoxing/newversion/6.3.32/entries/portal/dist;
index index.html index.htm;
# index graph-label-overlap.html graph-label-overlap.htm;
}
location /api/ {
proxy_pass https://jcjd.sx.gov.cn/;
}
}
# 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;
# }
#}
}
说明
从上面的配置文件可以大致分为http
、events
等模块配置项,接下来主要看http
模块中的sever
模块。
server {
listen 8082;
server_name localhost;
location / {
# root D:/work/authine/xianyu_project_frontEnd/newVersion/6.5.4/entries/portal/dist;
root D:/work/authine/shaoxing/newversion/6.3.32/entries/portal/dist;
index index.html index.htm;
# index graph-label-overlap.html graph-label-overlap.htm;
}
location /api/ {
proxy_pass https://jcjd.sx.gov.cn/;
}
}
listen
监听的端口号,当访问到 listen 监听的端口号时会触发相关的指令,如:listen 8080;
server_name
域名(主机名):如: 本地 server_name localhost;
百度 server_name www.baidu.com
location
定位资源,如:’/ ‘, location 命中 ‘/‘ 则触发 ‘/‘ 配置的行为;如:location 命中 ‘/api/‘ 则触发 ‘/api/‘ 配置的行为;location url 修饰符如下表:
修饰符 | 说明 |
---|---|
= | 用于不含正则表达式的 url 前,要求字符串与 url 严格匹配,匹配成功就停止向下搜索并处理请求 |
~ | 用于表示 url 包含正则表达式,并且区分大小写 |
~* | 用于表示 url 包含正则表达式,并且不区分大小写 |
^~ | 用于不含正则表达式的 url 前,要求 nginx 服务器找到表示 url 和字符串匹配度最高的location 后,立即使用此 location 处理请求,而不再匹配 |
如果有 url 包含正则表达式,不需要有~开头标识 |
location 配置
配置项 | 配置说明 |
---|---|
root | 资源的根目录 |
index | 指定根文件,可以是多个 |
proxy_pass | 代理地址 |
配置注意项:
location /api/ {
proxy_pass https://jcjd.sx.gov.cn/;
}
location /api/ {
proxy_pass https://jcjd.sx.gov.cn;
}
上面两段代码的区别是,proxy_pass
代理后面的配置有无 ‘/‘,如果访问地址为 localhost: 8080/api/,转发地址末尾不带 ‘/‘ 则实际转发到 https://jcjd.sx.gov.cn/api/ 如果带斜杠实际转发到 https://jcjd.sx.gov.cn/,注意区别。
实际应用
一、打包后项目测试
如下配置:
server {
listen 8080;
server_name localhost;
location / {
# root D:/work/authine/xianyu_project_frontEnd/newVersion/6.5.4/entries/portal/dist;
root D:/work/authine/shaoxing/newversion/6.3.32/entries/portal/dist;
index index.html index.htm;
# index graph-label-overlap.html graph-label-overlap.htm;
}
location /api/ {
proxy_pass https://jcjd.sx.gov.cn;
}
}
配置说明:
root
指定了资源目录,index
指定了入口文件,root
和index
主要用来指定资源。- 当主机 (域名 或 ip)访问根目录时,则会返回静态资源(即 root 和 index 指定的资源)
- 当访问路径中出现 ‘/api/‘ 则会将命中的路径 拉到
proxy_pass
后面。如:
路径中出现 /api/authine/getMomey/001,proxy_pass 如果无 ‘/‘,
则转发地址 https://jcjd.sx.gov.cn/api/authine/getMomey/001
如果有 ‘/‘ 则转发地址 https://jcjd.sx.gov.cn/authine/getMomey/001
二、开发环境代理
在云枢系统中使用了devServer.proxy
配置项处理反向代理。 当项目跑起来后发现,代理出了问题,需要在 .env.debug
文件中修改配置重启项目,云枢项目重启很慢。建议使用 nginx
代理。
首先注释掉 devServer.proxy
的配置。nginx
配置如下:
server {
listen 8083;
server_name localhost;
location / {
proxy_pass http://localhost:9100;
}
location ^~ /api/ {
proxy_pass https://jcjd3.sjw.qz.gov.cn/api/api/;
}
location /externalLink/ {
proxy_pass https://jcjd3.sjw.qz.gov.cn/api;
}
location /ext/ {
proxy_pass https://jcjd3.sjw.qz.gov.cn/api;
}
location /apis/ {
proxy_pass https://jcjd3.sjw.qz.gov.cn/;
}
location /v1/ {
proxy_pass https://jcjd3.sjw.qz.gov.cn/;
}
}
针对像县域项目服务多的情况,配置不同的端口号即可。访问指定的端口号即可,如下图所示:
当我们访问端口为 8083 根目录时,直接转发给了 http://localhost:9100 测试服务。
问题排查
问题一: 如下图所示出现如下报错,反向代理出现问题,请查看相关接口地址
可以通过postman
修改路径确认路径哪里出现了问题,找到原因后修改nginx
文件并重启。postman
测试如下图所示: