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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM