NGINX部署vue+springboot项目

22 篇文章 2 订阅
8 篇文章 0 订阅

NGINX部署vue+springboot项目

嫌麻烦不想自己创建项目的,可以自取

链接: https://pan.baidu.com/s/1z-BT1ZR4dN34BY5Oz1QkEg 提取码: 0ge3
在这里插入图片描述

在这里插入图片描述

部署Vue

首先将vue打包出来的dist文件放到nginx/html下,不是将整个文件夹放进去,而是将文件内的文件放进去

[root@hecs-82454 html]# ls
50x.html  css  favicon.ico  fonts  index.html  js

然后修改配置文件nginx/conf/nginx.conf,其实,什么也没修改,包括user root被开放也是我自己想测试一下,根本就什么都没改

user  root;
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   html;
            index  index.html index.htm;
        }

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

}

再次进入nginx的地址,也就是
http:xxx.xxx.xxx:80,就是我们的vue项目了
在这里插入图片描述
点击huaweiyuntest这个链接,正常进入了我们的主页面
在这里插入图片描述

然后再将我们的springboot项目启动起来,可以像我一样

[root@hecs-82454 Idea]# java -jar huaweiyun-0.0.1-SNAPSHOT.jar

或者

nohup java -jar xxx.jar &

这样的话后台启动项目,执行ctrl + c也不会中断线程的执行。

启动起来之后我们调用一下自己的接口

http://xxx.xxx.xxx.xxx:9877/huaweiyun/hello

在这里插入图片描述
但是出现了一个问题,请求的时候404了!明明同样的配置在本地调试的是没问题呀,而且直接请求后端接口也是没问题,那只能说明是我们nginx的配置文件出问题了
在这里插入图片描述
重新检查配置文件,发现少了一些配置,上图我们也看到了,我们的请求都带了一个/api,这个是我们vue项目里为了方便请求后端来做的一个配置,那现在我们就应该为这个/api做一个特殊的location
在这里插入图片描述
完整配置如下

user  root;
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   html;
            index  index.html index.htm;
        }
        location /api {
            proxy_pass http://localhost:9877/;
        }
        #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;
    #    }
    #}

}

重新启动nginx后再次请求,成功
在这里插入图片描述

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Nginx可以用来部署VueSpring Boot应用程序。 对于Vue应用程序,可以将Vue打包成静态文件,然后将这些文件放在Nginx的静态文件目录中。然后,可以使用Nginx配置文件中的location指令将请求重定向到这些文件。 对于Spring Boot应用程序,可以使用Nginx作为反向代理服务器。在Nginx配置文件中,可以使用proxy_pass指令将请求转发到Spring Boot应用程序的端口。此外,还可以使用Nginx的负载均衡功能来分配请求到多个Spring Boot实例。 总之,Nginx是一个非常强大的Web服务器和反向代理服务器,可以用来部署VueSpring Boot应用程序。 ### 回答2: Nginx是一个非常流行的Web服务器,也是一款反向代理服务器。在前后端分离的开发模式中,Vue作为前端框架和Spring Boot作为后端框架,Nginx也被广泛应用于这种应用程序的部署和运维中。 Nginx的优点 Nginx具有高性能,高并发,高可靠性和低资源消耗的优点,因此成为了前后端分离应用的首选。在前后端分离的开发模式中,响应速度非常重要。Nginx可以充分利用多核CPU和操作系统的缓存机制来提高性能,同时还可以支持多种负载均衡算法,提供高可用性和可扩展性,简化了应用程序中的代理和重定向操作。 Vue + Nginx部署 Vue是一款非常流行的前端JavaScript框架,具有简单易用,可扩展性强,易于部署的特点。而部署Vue前端应用的首选是基于Nginx的HTTP服务器。可以将Vue项目构建后,将生成的静态文件放置在Nginx服务器的标准目录下即可,这样浏览器即可通过Nginx访问到Vue应用。具体步骤如下: 1. 安装Nginx服务器 2. 将Vue项目打包成一个静态文件 3. 将打包好的静态文件放置到Nginx服务器的标准目录下 4. 配置Nginx反向代理 Spring Boot + Nginx部署 Spring Boot是一种非常流行的后端框架,它可以快速构建Java应用程序。使用Nginx来代理后端应用程序可以有效减少连接数,降低负载,并提高应用程序的响应速度。具体步骤如下: 1. 安装Nginx服务器 2. 配置Nginx反向代理 3. 部署Spring Boot应用程序 4. 配置Spring Boot应用程序端口 总结 综上所述,Nginx可以作为前后端分离应用的反向代理服务器,可以充分利用多核CPU和操作系统的缓存机制来提高性能,同时提供高可用性和可扩展性。在部署VueSpring Boot应用程序时,可以根据不同的需求来选择Nginx的不同部署方式,以达到最佳的应用程序性能和可用性。 ### 回答3: nginx是一款高性能的web服务器和反向代理服务器,其与vuespringboot的结合使用能够实现更加出色的web应用性能和效果,下面将具体介绍nginx部署vuespringboot的过程。 1. 部署vue (1)在本地开发好vue应用后,直接运行"npm run build"命令进行打包,将"dist"文件夹中的内容放到服务器上(如/var/www/目录下)。 (2)修改nginx配置文件,新增一个server配置段,并将server_name设置为自己的域名或IP地址: server { listen 80; server_name yourdomain.com; location / { root /var/www/dist; index index.html; } } (3)重启nginx服务,访问自己的域名或IP地址即可查看vue应用。 2. 部署springboot (1)将springboot项目打包成jar包,将其放到服务器上某个目录下(如/opt/springboot/目录下)。 (2)修改nginx配置文件,同样新增一个server配置段,将server_name设置为自己的域名或IP地址,并且将location设置为/springboot/,并将其反向代理到springboot的端口上: server { listen 80; server_name yourdomain.com; location /springboot/ { proxy_pass http://127.0.0.1:8080/; } } (3)重启nginx服务并启动springboot项目即可。 总结: 通过nginx反向代理服务器的优势,可以大幅提升web应用的运行效率和访问速度,在部署vuespringboot时,只需要简单地配置nginx就可以实现高效的web应用部署和运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值