作用
- 正向代理
- 反向代理 (与正向代理的区别?前者代理的是客户端,后者代理的是服务端)
- 负载均衡(轮询、权限)
- 动静分离
前端中的作用
web服务器、反向代理、跨域
安装nginx
// 安装管理工具
yum install gcc-c++
// 具体安装教程和出现404 解决方案如下:
https://blog.csdn.net/qq1195566313/article/details/123965492?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167101739916800213043724%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167101739916800213043724&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-2-123965492-null-null.nonecase&utm_term=linux&spm=1018.2226.3001.4450
配置
cd nginx-1.19.9/
./configure // 执行
make // 编译
whereis nginx // 找到nginx路径
// 进入启动目录 并启动
cd sbin
ls
./nginx
// 尝试访问公网Ip 会显示欢迎nginx 如果不显示则修改端口和打开防火墙端口
...
// 配置环境变量以便使用nginx命令
...
命令
ps -ef | gref nginx // 查找nginx 进程
nginx // 启动nginx
nginx -s stop // 停止nginx进程
nginx -s quit // 停止nginx进程(会等待请求完毕再停止)
nginx -s reload // 重载nginx配置文件
代理
nginx.conf
// 访问域名代理
location / {
root html;
index index.html index.htm;
# 代理localhost:80 到 bilibili
proxy_pass: http://bilibili.com;
}
// 解决跨域:原理类似webpack配置的 一些东西
// 访问api代理 比如: api前缀为 /api的请求被转发给服务器的ip
location /api/ {
// 将前端请求的/api/ 代理到 后端的服务器下 http://localhost:9999
proxy_pass: http://localhost:9999
}
history 404模式
location / {
...
try_files: $uri $uri/index.html;
}