ruoyi-vue前后端分类nginx配置

前置条件

  1. 本地安装node.js并配置环境变量(打包vue)
  2. 服务器安装nginx
  3. 服务器安装jdk并配置环境变量
  4. 安装mysql
  5. 安装redis
  6. 参考项目文档本地先运行一遍再部署到服务器

拉取代码

若依gitee项目地址

git clone https://gitee.com/y_project/RuoYi-Vue.git

修改项目配置并打包

ruoyi-admin修改application.yml中的redis地址,application-druid.yml中的mysql地址

后端打包

mvn clean package 

打包后上传到服务器并启动

nohup java -jar ruoyi.jar(根据实际名称修改) &

前端修改配置打包

修改vue.config.js中target为http://127.0.0.1,如果不部署在同一台服务器或者使用域名自行修改

打包

npm run build:prod

上传

将打包好的文件(dist目录)上传到服务器,我的上传到了/srv/ruoyi目录,解压并修改名称

unzip dist.zip
mv dist web

nginx配置文件修改

使用默认的安装方式,nginx.conf位于/usr/local/nginx/conf目录

修改配置文件


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

    # ruoyi backend
    server {
	listen 80;
	server_name 127.0.0.1;

	# 前端服务地址
	location /  {
	   root /srv/ruoyi/web;
	   index index.html;
	}

	# 后台服务 
	#location /api/ { 访问192.168.2.13/api/captchaImage 相当于访问127.0.0.1:8080/captchaImage
	location /prod-api/ {
		proxy_pass http://127.0.0.1:8080/;
	}
    }

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

}

注意:后台配置location中使用的prod-api是由于前端有个base_url是/prod-api,vue nginx都不是特别熟,暂时不知道什么原因,location配置要注意/,加/不加/请求路径不一样

重启nginx

/usr/local/nginx/sbin/nginx -s reload

验证

登录

postman请求

nginx请求路径
192.168.2.13/prod-api/captchaImage等同于192.168.2.13:8080/captchaImage

posted @ 2021-09-05 10:34  学无终  阅读(4273)  评论(0编辑  收藏  举报