nginx代理转发url接口请求路径到spring boot后端实现真正响应

x33g5p2x  于2022-02-07 转载在 Spring  
字(2.6k)|赞(0)|评价(0)|浏览(557)

常规的服务端架构设计中,一般出于负载均衡或者请求转发目的把Nginx作为“前端”,接受用户的url请求,然后根据不同的url路径,再次把请求转发到后端真正的响应服务器上。以流行的restfull架构设计为例,一个服务器需要满足图片、返回json的api接口转发到不同的服务器上,比如,有两个请求,

(a)http://xxx.com:80/restfull/image

(b)http://xxx.com:80/restfull/api

现在需要把请求a转发到图片(image)服务器http://yyy.com:8080上,且有http://yyy.com:8080/image实现真正的服务响应。另外,需要把请求b转发到http://zzz.com:8080上,并且最终由http://zzz.com:8080/api实现真正的服务响应。

即最终形成一个http://xxx.com/restfull/api这样的url请求路径,实际的响应是由http://*.com:**/api完成。

实现上述功能。

(1)先实现真正的后端spring boot响应程序。先写一个演示的spring boot测试程序,完成一个简单的spring controller,响应/和/api的请求:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class SimpleController {
    @GetMapping("/")
    public HashMap home() {
        HashMap<String, String> map = new HashMap<>();
        map.put("home", System.currentTimeMillis() + "");
        return map;
    }

    @GetMapping("/api")
    public HashMap api_test() {
        HashMap<String, String> map = new HashMap<>();
        map.put("api", System.currentTimeMillis() + "");
        return map;
    }
}

启动spring boot application。此时在浏览器直接访问localhost:8080,返回:

直接访问localhost:8080/api,返回:

(2)在spring boot前面再加一层Nginx,实现Nginx的请求代理响应和转发。引入Nginx实现请求转发,把localhost/restfull/的请求转发到真正的spring boot后端响应接口。请求转发,主要得依靠Nginx的配置工作。

Nginx启动后,在80端口监听网络请求。需要做的是把localhost/restfull/或者localhost/restfull/转发到localhost:8080上去。实现该需求,修改nginx.conf,在nginx.conf只需加入一条最核心的代理转发规则即可完成:

location /restfull/ {
            proxy_pass http://localhost:8080/;
        }

注意添加的位置,是在http -> server里面。最终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;

    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 /index/ {
			proxy_pass  http://127.0.0.1:8001;
        }
		
		location /restfull/ {
            proxy_pass http://localhost:8080/;
        }

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

}

配置完成后,启动Nginx:

start nginx

如果此前Nginx已经启动过,因为Nginx配置文件已经修改,需要加载新修改后的配置文件,那么需要重新使用Nginx命令加载配置:

nginx -s reload

nginx -s reopen

(3)完成以上配置后,此时浏览器访问http://localhost/restfull

返回:

访问 http://localhost/restfull/api 

服务器返回:

说明Nginx代理请求转发的功能成功实现。

相关文章

微信公众号

最新文章

更多