Nginx部署前后端

后端打包

进入idea后端项目 右侧点击Maven 先执行clean 在执行package

进入目录找到 auto-repair.jar

前端打包

进入前端项目:D:\Code\auto-repair\RuoYi-Vue-master\ruoyi-ui 执行CMD

bash 复制代码
# 在 Windows 下递归删除 dist 目录(/s 删除目录树,/q 静默模式)清理
rmdir /s /q dist

# 运行 npm 脚本 build:prod,用于生产环境打包(将源码编译、压缩后输出到 dist 目录)
npm run build:prod

执行完会在目录出现一个dist 目录文件夹

统一存放

jar 放前端包,web放后端包,service放启动脚本。

Nginx配置

D:\nginx-1.28.2\conf 找到 nginx.conf 打开 配置如下

XML 复制代码
        # 前端静态文件配置
        location / {
            root   D:/Code/auto-repair/project/web/dist;   # 修改为你的 dist 实际路径
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;  # 支持 Vue 路由 history 模式
        }

        # 后端接口反向代理
        location /prod-api/ {
            proxy_pass http://localhost: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;
        }

完整版

XML 复制代码
#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 前端静态文件配置
        location / {
            root   D:/Code/auto-repair/project/web/dist;   # 修改为你的 dist 实际路径
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;  # 支持 Vue 路由 history 模式
        }

        # 后端接口反向代理
        location /prod-api/ {
            proxy_pass http://localhost: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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # 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;
    #    }
    #}

}

启动脚本配置

1. startServices.bat

注:指向你的nginx-1.28.2位置,如 cd /d D:\nginx-1.28.2

注:指向你的jar位置,如 cd /d D:\Code\auto-repair\project\jar

bash 复制代码
@echo off
cd /d D:\nginx-1.28.2
echo Stopping nginx...
nginx -s stop 2>nul

echo Stopping backend on port 8080...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr :8080 ^| findstr LISTENING') do (
    taskkill /f /pid %%a >nul 2>&1
)
timeout /t 2 /nobreak >nul

echo Starting nginx...
start nginx.exe

echo Starting backend...
cd /d D:\Code\auto-repair\project\jar
start javaw -jar auto-repair.jar

echo All services started.
exit

2. stopServices.bat

bash 复制代码
@echo off
cd /d D:\nginx-1.28.2
echo Stopping nginx...
nginx -s stop 2>nul

echo Stopping backend on port 8080...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr :8080 ^| findstr LISTENING') do (
    taskkill /f /pid %%a >nul 2>&1
)

echo All services stopped.
exit

3.执行脚本

访问:http://localhost/

相关推荐
大树8814 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠14 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质14 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工15 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智16 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_16 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉17 小时前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦17 小时前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
java_cj18 小时前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes
lsyeei18 小时前
linux 系统目录详解
linux·运维·服务器