栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

配置前后端分离项目共用一个端口-后端使用api访问-使用nginx当网关进行端口转发接口转发 -在线图

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

配置前后端分离项目共用一个端口-后端使用api访问-使用nginx当网关进行端口转发接口转发 -在线图

vue+springboot项目实现共用一个端口进行外网访问

前端项目进行端口转发到项目

后端项目进行/api接口进行转发到后端
1.打开端口映射工具,进行公网端口映射(如果开发项目有公网则忽略这一步)
端口映射工具有:
花生壳,
1.1进行端口映射配置
配置本机的地址:localhost或者127.0.0.1

要映射的端口号:就是你配置的nginx监听的端口号

应用类型选:http


1.2开启端口映射

1.3然后你的nginx对应的公网地址就是访问地址:域名了
访问地址:
http://1f872a2261.zicp.vip:28579

注意:
这个可以使用别的设备进行访问

2.配置nginx进行端口转发和接口映射 2.1nginx目录结构

2.2进入conf目录对nginx的配置文件nginx.conf进行配置

最主要的配置信息
#################################最主要的配置信息#########################################
    server {
        #listen       80;
		#监听的端口,也是nginx服务启动占用的端口
		listen       8001;
		#server name 为虚拟服务器的识别标志,匹配到特定的server块,转发到对应的应用服务器中去。
        server_name  localhost;
	
		#所有访问localhost:8081端口的请求,都发送到这里,模糊匹配
        location / {
			#反向代理  对请求localhost:8081/xxxx进行代理转发到http://127.0.0.1:8002/xxxx
			proxy_pass  http://127.0.0.1:8002;
			
        }
        
		#所有访问localhost:8081/api/接口的请求,都发送到这里,精确匹配
		location  /api/ {
			#反向代理   将localhost:8081/api/xxxxxxx的请求进行代理转发到http://127.0.0.1:8080/renren-fast/xxxx
			proxy_pass  http://127.0.0.1:8080/renren-fast/;
			
		}
    }
}

############################################################################

全部配置信息

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

	#定义的日志格式为main     后边的是日志参数
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

	#自定义日志格式mylog   后边是自定义的日志参数
	log_format  mylog '$remote_addr- "$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;
		#监听的端口,也是nginx服务启动占用的端口
		listen       8001;
		#server name 为虚拟服务器的识别标志,匹配到特定的server块,转发到对应的应用服务器中去。
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		#所有访问localhost:8081端口的请求,都发送到这里,模糊匹配
        location / {
            #root   html;
            #index  index.html index.htm;
			#反向代理  对请求localhost:8081/xxxx进行代理转发到http://127.0.0.1:8002/xxxx
			proxy_pass  http://127.0.0.1:8002;
			
			#配置前端项目的日志  日志存储的位置/日志名字   要记录的日志参数信息
			access_log  logs/qianduan.log  mylog;
        }

		#所有访问localhost:8081/api/接口的请求,都发送到这里,精确匹配
		location  /api/ {
			#反向代理   将localhost:8081/api/xxxxxxx的请求进行代理转发到http://127.0.0.1:8080/renren-fast/xxxx
			proxy_pass  http://127.0.0.1:8080/renren-fast/;
			
			#配置后端项目的日志   日志存储的位置/日志名字   要记录的日志参数信息
			access_log  logs/houduan.log  mylog;
		}
        #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;
    #    }
    #}

}

2.3.配置前端项目的端口地址

2.4配置前端调用后端的地址

2.5.测试访问前端项目 5.1测试访问本地ip的前端地址http://localhost:8082

5.2测试访问端口映射的ip地址:http://1f872a2261.zicp.vip:28579/

3.后端项目启动renrenfast后台

4.测试访问前端项目,并登陆 4.1查看验证码的访问路径

4.2进行登录
用户名:admin
密码:admin

1.可以看到用户点击登陆,会响应一个token信息

2.根据token和请求链接重新发送请求,进入首页

主要内容,重点就是vue配置后端/api接口 和nginx的配置

参考: 1.nginx配置文件nginx.conf之server及server_name的意义详解

https://blog.csdn.net/qq_40737025/article/details/85053164

2.配置nginx日志

https://developer.aliyun.com/article/792474

3.在父目录打包项目 项目打包命令 >mvn clean package -Dmaven.test.skip=true

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/887307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号