CentOs7 配置Nginx 部署Java前后端项目
1.下载Nginx
# 下载Nginx
wget http://nginx.org/download/nginx-1.19.6.tar.gz
# 解压
tar -xzf nginx-1.19.6.tar.gz
# 切换到目录下
cd nginx-1.19.6
2.安装nginx的依赖环境
# yum升级
yum update
# yum安装依赖
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 配置ssl模块
./configure
./configure --with-http_ssl_module
# 安装
make & make install
# 查看是否安装成功
/usr/local/nginx/sbin/nginx -V
3.配置Nginx
# 切换到Nginx配置文件目录下
cd /usr/local/nginx/conf
# 修改配置文件
vi nginx.conf
Nginx的配置文件(https协议)
有几点注意项:
1.location /backend/
代理的是后端, location /
代理的是前端
2.backend
得在后台程序中的application.yml
中定义
3.启动项目之后,开启了防火墙的话,一定要开放443、80和项目中的端口号
4.前端访问后端的地址得是VUE_APP_BASE_API = 'https://192.168.3.105/backend'
,https协议的,不然会出现协议混淆,访问不到后端接口。
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/cer/server.crt;
ssl_certificate_key /usr/local/nginx/cer/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/local/nginx/html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /backend/{
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;
proxy_pass http://localhost:8991;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置完之后,就启动Nginx
# 切换到sbin目录下
/usr/local/nginx/sbin
# 启动Nginx
./nginx
# 改动配置之后重启
./nginx -s reload