Springboot+vue项目使用Apache进行路由端口转发

1. 打包后端项目(jar包)

IDEA里执行package将后台工程打成jar包,上传至服务器 

我将jar包上传至了target目录,cd至target目录,执行以下命令启动后台(&表示后台运行,退出不会结束进程):

[root@zy-host target]# java -jar exam-admin.jar &

 2. 打包前端项目(dist包)

在前端工程下执行以下命令对前台项目打包。

# 构建生产环境
npm run build:prod

构建完之后,前端目录会出现一个dist文件夹,将该文件夹的内容上传至服务器指定目录,这里的目录可以自定义,和apache中指定的目录一致即可。我上传至了/usr/local/apache2.4.54/htdocs/html目录中。

3. Apache 配置

(1)在apache的配置目录中/usr/local/apache2.4.54/conf/httpd.conf,指定站点目录

DocumentRoot "/usr/local/apache2.4.54/htdocs"
<Directory "/usr/local/apache2.4.54/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride none
    Require all granted
</Directory>
#进一步放宽站点目录权限
<Directory "/usr/local/apache2.4.54/htdocs/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

(2)打开代理服务模块

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

mod_proxy提供代理服务器功能, mod_proxy_http让代理服务器能支持HTTP协议。

(3)配置ProxyPass

在httpd.conf文件末尾追加:

请求到 /exam-api/v4/  上的请求,会转发到后端项目的发布地址 http://39.107.232.89:8080/

ProxyPass /exam-api/v4/ http://39.107.232.89:8080/
ProxyPassReverse /exam-api/v4/ http://39.107.232.89:8080/

退出保存,然后重启httpd服务,访问 http://39.107.232.89 ,可以成功访问:

(4)进去刷新会出现404错误,如下:

 

 只需在httpd.conf文件中加以下配置即可,/ 后面根据项目具体情况来定。

FallbackResource /exam

2022-12-26补充:

或者将需要配置的内容写入一个配置文件中:

ProxyPass /geomis-api/v1 http://127.0.0.1:8090
ProxyPassReverse /geomis-api/v1 http://127.0.0.1:8090

Alias /geomis "/home/kylin/geomis/dist"
<Directory "/home/kylin/geomis/dist">

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order Deny,Allow
    Allow from all
    Satisfy Any

  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /geomis/
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /geomis/index.html [L]
  </IfModule>

  FallbackResource /geomis

</Directory>

这段代码是Apache服务器配置文件中的一部分。它定义了三个不同的功能:

  1. 将所有访问/geomis-api/v1的请求转发到本地127.0.0.1:8090端口,以便处理后端API的请求和响应。这是通过ProxyPassProxyPassReverse指令完成的。

  2. 将所有访问/geomis的请求指向本地文件系统中的/home/kylin/geomis/dist目录。这是通过Alias<Directory>指令完成的。

  3. 如果访问的文件或目录不存在,则将请求重定向到/geomis/index.html文件。这是通过FallbackResource<IfModule mod_rewrite.c>指令完成的。如果Apache服务器上没有安装mod_rewrite模块,则FallbackResource指令将会被使用,它会将请求直接传递给/geomis/index.html文件。如果安装了mod_rewrite模块,则<IfModule mod_rewrite.c>指令将被使用,并使用RewriteRule指令将请求重定向到/geomis/index.html文件。

对应的主配置文件内容:

httpd.conf

ServerRoot "/etc/httpd"

#监听端口
Listen 8088
#采用配置文件加载模块
Include conf.modules.d/*.conf
#用户组设置
User apache
Group apache

ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

#指定网站文件存放的目录路径,通常配置在主配置文件中,或虚拟主机文件中
DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

<Directory "/var/www/html">

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

#IfModule模块用于 如果存在该模块,标签中的内容就生效
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>
#错误日志存放目录
ErrorLog "logs/error_log"
#设置错误日志等级
LogLevel warn

#Apache日志格式配置
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>

    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>

    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

#EnableMMAP off
EnableSendfile on

# 在 "/etc/httpd/conf.d" 目录中加载配置文件
IncludeOptional conf.d/*.conf

#ServerTokens 参数设置 http 头部返回的 apache 版本信息, prod设置为仅返回软件名称,如apache
ServerTokens Prod

#在服务器生成的文档上配置页脚
ServerSignature off
TraceEnable off

碰到的坑:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小印z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值