worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#前端页面服务器
server {
#监听端口和域名
listen 7000;
server_name localhost;
#添加头部信息
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#添加拦截路径和代理地址
location /api/ {
proxy_pass http://localhost:8080/; #注意:使用代理地址时末尾记得加上斜杠"/"。
}
#添加拦截路径和根目录
location / {
root html/hehe; #注意:使用"/"拦截全路径的时候记得放在最后。
index index.html index.htm; #index表示首页
}
}
}
Page Index
前台系统7000
package com.hehe;
@SpringBootApplication
@RestController
@RequestMapping("/user/login/*")
public class SpringBootNginxApplication {
//在拦截器打印访问URL
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
if(response.getStatus()/100>=4){
System.err.println("访问URL:"+request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE));
}else {
System.out.println("访问URL:"+request.getRequestURI());
}
}
});
}
};
}
打开浏览器,访问 http://localhost:7000/api/user/login/verifyCode ,可以看到后台获取的Session和第一次生成的验证码。如图:
server {
listen 7000;
server_name localhost;
#正确示范: 末尾加斜杠"/"
location /api/ {
proxy_pass http://localhost:8080/hehe/;
}
}
server {
listen 7000;
server_name localhost;
#错误示范: 末尾无斜杠
location /api/ {
proxy_pass http://localhost:8080/hehe;
}
}
测试完成了