Nginx安装篇
# Nginx 安装篇
# Linux 直装
# 安装指令
以 CentOS 7/8/9 或 Rocky Linux 为例
1. 安装 EPEL 仓库(如果尚未安装)
sudo yum -y epel-release
1
EPEL 提供了官方 Nginx 包(比默认 repo 更新)。
2. 安装 Nginx
sudo yum install -y nginx
1
3. 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
1
2
2
4. 检查状态
sudo systemctl status nginx
1
✅ 正常应显示 active (running)
# 开放防火墙端口
# 开放 HTTP (80) 和 HTTPS (443)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
1
2
3
4
2
3
4
如果你用的是云服务器(如阿里云、腾讯云),还需在安全组中放行 80/443 端口。
# 关键目录说明
| 用途 | 路径 |
|---|---|
| 主配置文件 | /etc/nginx/nginx.conf |
| 站点配置目录 | /etc/nginx/conf.d/ |
| 默认网页根目录 | /usr/share/nginx/html/ |
| 日志文件 | /var/log/nginx/access.log 和 /var/log/nginx/error.log |
简单配置
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root /var/www/cm-gallery-frontend;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /admin-api/ {
proxy_pass http://localhost:48080/admin-api/;
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;
}
location ~ ^/v3/api-docs/(.*) {
proxy_pass http://localhost:48080/v3/api-docs/$1;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
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
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
# 配置站点
1. 创建 SSL 证书目录(规范存放)
sudo mkdir -p /etc/nginx/ssl
1
将你的证书和私钥复制进去:
sudo cp yourdomain.com.crt /etc/nginx/ssl/
sudo cp yourdomain.com.key /etc/nginx/ssl/
1
2
2
设置权限(安全,可选):
sudo chmod 600 /etc/nginx/ssl/*.key
sudo chmod 644 /etc/nginx/ssl/*.crt
1
2
2
2. 放置前端静态文件
sudo rm -rf /usr/share/nginx/html/*
sudo cp -r your-vue-dist/* /usr/share/nginx/html/
# 也可以另外新建目录存放
# ruoyi示例:/home/ruoyi/projects/ruoyi-ui
1
2
3
4
5
2
3
4
5
3. 编写站点配置
创建 /etc/nginx/conf.d/yourdomain.conf:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
# 强制跳转 HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
# SSL 证书
ssl_certificate /etc/nginx/ssl/yourdomain.com.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.com.key;
# 推荐 SSL 配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
ssl_prefer_server_ciphers off;
# 前端
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
# 后端 API 代理(Spring Boot 运行在 8080)
location /api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
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
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
具体案例配置:
server {
listen 80;
server_name cmty256.cloud www.cmty256.cloud;
# 强制跳转 HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name cmty256.cloud www.cmty256.cloud;
# SSL 证书
ssl_certificate /etc/nginx/ssl/cmty256.cloud.crt;
ssl_certificate_key /etc/nginx/ssl/cmty256.cloud.key;
# 前端
location / {
root /var/www/cm-gallery-frontend;
try_files $uri $uri/ /index.html;
}
# 后端 API 代理(Spring Boot 运行在 8080)
location /admin-api/ {
proxy_pass http://127.0.0.1:48080/admin-api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
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
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
# 测试配置 & 重载
# 测试语法
# 如果看到 `syntax is ok` 和 `test is successful`,说明配置正确。
sudo nginx -t
# 重载配置(不中断服务)
sudo nginx -s reload
1
2
3
4
5
6
2
3
4
5
6
# 验证是否成功
# 本地测试
curl -I https://yourdomain.com
curl -I https://cmty256.cloud
# 查看日志
tail -f /var/log/nginx/access.log
1
2
3
4
5
6
2
3
4
5
6
浏览器访问 https://yourdomain.com 应该能打开你的 Vue 页面,并且 /api/xxx 能正常调用后端。
# 常用命令速查
| 操作 | 命令 |
|---|---|
| 启动 Nginx | sudo systemctl start nginx |
| 停止 Nginx | sudo systemctl stop nginx |
| 重启 Nginx | sudo systemctl restart nginx |
| 重载配置 | sudo nginx -s reload |
| 查看状态 | sudo systemctl status nginx |
| 查看错误日志 | sudo tail -f /var/log/nginx/error.log |
# docker 安装
docker run 指令
# 创建挂载目录
mkdir -p /opt/nginx/{conf,html,logs,ssl}
# 生成容器
docker run --name nginx -p 80:80 -d nginx
# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf
# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf/conf.d
# 将容器中的html文件夹复制到宿主机
docker cp nginx:/usr/share/nginx/html /opt/nginx/
# 删除正在运行的nginx容器
docker rm -f nginx
# 启动 Nginx 容器
docker run -d \
--name nginx \
-p 80:80 \
-p 443:443 \
-v /opt/nginx/conf.d:/etc/nginx/conf.d \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx \
-v /opt/nginx/ssl:/etc/nginx/ssl \
--add-host=host.docker.internal:host-gateway \
--restart unless-stopped \
nginx
docker run \
-p 9002:80 \
--name nginx \
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /opt/nginx/log:/var/log/nginx \
-v /opt/nginx/html:/usr/share/nginx/html \
-d nginx
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
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
# jdk 相关
1、下载并安装 OpenJDK 11(以 Ubuntu/CentOS 为例)
# 方法一:使用 yum 安装(CentOS/RHEL)
sudo yum install -y java-11-openjdk-devel
# 方法二:使用 apt 安装(Ubuntu/Debian)
sudo apt update
sudo apt install -y openjdk-11-jdk
# 方法三:手动下载 Oracle JDK 或 OpenJDK
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
2、验证是否安装成功
java -version
# 输出类似:
openjdk version "11.0.18" 2023-01-16 LTS
OpenJDK Runtime Environment (build 11.0.18+10-LTS)
OpenJDK 64-Bit Server VM (build 11.0.18+10-LTS, mixed mode)
1
2
3
4
5
6
2
3
4
5
6
3、设置或切换默认 Java 版本
# 查看所有已安装 Java 版本
alternatives --config java
# 会看到类似输出:
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7_9.x86_64/jre/bin/java
2 /usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java
Enter to keep the current selection[+], or type selection number:
# 输入 2(对应 Java 11 的编号),回车。
# 这样就可以切换默认 Java 版本了
# 或者设置环境变量
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
上次更新: 2026/2/24 17:35:26