前後端分離部署,ngnix反向代理

開發環境

前端:

  1. vue,axios,proxytable
  2. build打包成dist
  3. 8084端口

後臺:

  1. springboot
  2. maven打包成jar包(使用springboot內置的Tomcat)
  3. 已部署在8082端口

部署情況

前端:

  1. ngnix(nginx安裝、部署測試教程
  2. 已測試連接成功

後臺:

1.安裝好java(1.8), nohup java -jar XXX.jar &啓動
2. 已測試連接成功

生產環境(問題:前端請求後臺接口時404)

前端config-index.js配置代理

proxyTable: {
      '/api': {
        // target: 'http://127.0.0.1:18891',
        target: 'http://X.X.235.234:8082',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },
axios.post(/api/index/getlist).then((res)=>{})

前臺請求:/api/index/getlist
相當於
後臺請求:http://X.X.235.234:8082/index/getlist

linux服務器nginx.conf配置反向代理


user  root;#因爲是在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       8084;#端口號
        server_name  webapp;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /opt/education-platform/webapp;#打包後文件位置
            index  index.html index.htm; 
        }
        location /api {
            rewrite ^.+api/?(.*)$ /$1 break;
            #include uwsgi_params;#可選項
            proxy_pass http://X.X.235.234:8082; #必選項
        }

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

}

瀏覽器訪問

http://X.X.235.234:8084

發佈了71 篇原創文章 · 獲贊 12 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章