安装nginx
5.1 安装 Nginx
参考 Nginx 官方文档 (opens new window),安装 Nginx 服务。命令如下:
## 添加 yum 源
yum install epel-release
yum update
inx
yum install nginx
## 启动 nginx
nginx
1
2
3
4
5
6
7
2
3
4
5
6
7
Nginx 默认配置文件是 /etc/nginx/nginx.conf。
下面,来看两种 Nginx 的配置,分别满足服务器 IP、独立域名的不同场景。
# (opens new window)5.2 方式一:服务器 IP 访问
① 修改 Nginx 配置,内容如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k; # 设置允许压缩的页面最小字节数
gzip_buffers 4 16k; # 用来存储 gzip 的压缩结果
gzip_http_version 1.1; # 识别 HTTP 协议版本
gzip_comp_level 2; # 设置 gzip 的压缩比 1-9。1 压缩比最小但最快,而 9 相反
gzip_types gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定压缩类型
gzip_proxied any; # 无论后端服务器的 headers 头返回什么信息,都无条件启用压缩
server {
listen 80;
server_name yolo.com; ## 你的外网 IP/域名
location / { ## 前端项目
root /work/projects/yolo-ui-admin;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /yolo-admin-api/ { ## 后端项目 - 管理后台
proxy_pass http://localhost:8090/yolo-admin-api/; ## 重要!!!proxy_pass 需要设置为后端项目所在服务器的 IP
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
② 执行 nginx -s reload 命令,重新加载 Nginx 配置。
③ 分别请求上面配置的前后端地址,成功访问项目。
上次更新: 2023/08/06, 22:51:57
