Nginx 前后端分离部署

Nginx 配置

后端路径为 /api,前端路径为 /api/app

#user  nobody;
worker_processes  1;

events {
  worker_connections  1024;
}

http {
  include       mime.types;
  default_type  application/octet-stream;

  sendfile        on;
  keepalive_timeout  65;

  server {
    listen       1234;
    server_name  127.0.0.1;

    location = /api {
      rewrite ^ /api/app;
    }
    location = /api/ {
      rewrite ^ /api/app;
    }

    location ^~ /api/app {
      alias  /opt/nginx-1.18.0/api/html;
      try_files $uri $uri/ /index.html;
      # index  index.html;
    }

    location /api {
      proxy_pass http://127.0.0.1:8848/api;
      client_max_body_size    1024m;
      proxy_set_header Origin '';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
}

Jenkins 启动脚本

jar 包中不含依赖

执行 mvn dependency:copy-dependencies 会将项目依赖复制到 target/dependency 目录中,然后将依赖拷贝到 lib 目录下

后端

export BUILD_ID=dontKillMe

count=`jps | grep api | wc -l`
if [ $count -gt 0 ]; then
 jps | grep api | awk '{print $1}' | xargs kill -9
fi

rm -rf /opt/nginx-1.18.0/api/api.jar
cp /opt/jenkins/workspace/api-springboot/target/api.jar /opt/nginx-1.18.0/api/api.jar

cd /opt/nginx-1.18.0/api/
nohup java -Dloader.path=lib -jar api.jar &

前端

export BUILD_ID=dontKillMe

npm run clean:dist
npm run ng build --prod --aot=false --build-optimizer=false --optimization --progress --extractCss

rm -rf /opt/nginx-1.18.0/api/app
mv ./dist /opt/nginx-1.18.0/api/app

count=`lsof -i:1010 | awk 'NR>=2' | awk '{print $2}' | wc -l`
if [ $count -gt 0 ]; then
 lsof -i:1010 | awk 'NR>=2' | awk '{print $2}' | xargs kill -9
else
 echo $count
fi

cd /opt/nginx-1.18.0/sbin/
nohup ./nginx &

Maven 主要配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot-dependencies.version}</version>
    <configuration>
        <layout>ZIP</layout>
        <!--不打包依赖到 jar 包中-->
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

https://blog.csdn.net/qq_37334435/article/details/100994238

https://www.cnblogs.com/zouhong/p/12189476.html

原文地址:https://www.cnblogs.com/jhxxb/p/13614282.html