1. 问题
在某个测试环境中,get请求调用时出现下面的异常。
image.png而应用代码:
@Slf4j
@RestController
public class TestNginx {
@GetMapping("sendNginx")
public String sendEvent(@RequestParam("id") String id) {
log.info(id);
log.info("请求成功进入");
return "success"+id;
}
}
配置文件:
server:
port: 8089
servlet:
context-path: /study
当然,不通过nginx调用服务没有出现这个情况;
2. 解决方案
推测是nginx问题。
nginx.conf配置
user 用户名 用户组;
#worker_processes 1;
#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 /Users/yexuerui/Documents/nginxlog/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
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;
}
}
include servers/*;
}
去servers目录下读取conf配置:
问题配置.png- 注意$1和$2表示的含义;
- proxy_pass 和upstream的关系;
原因在于,当location存在正则匹配时,get请求会丢失参数
解决方案:
在正则的location的proxy_pass后加?$args。
server {
listen 8083;
# server_name test.one.cn;
access_log /Users/xxx/Documents/nginxlog/local.log main;
error_log /Users/xxx/Documents/nginxlog/local-error.log debug_http;
location ~*^/(273|251)study/(.*) {
# 不使用改配置upstream中有_tomcat会出现异常。
proxy_set_header Host $host;
proxy_pass http://webchat_study_$1/study/$2?$args;
}
location /271study {
proxy_redirect off;
keepalive_timeout 300s 300s;
keepalive_requests 100;
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 3000s;
proxy_read_timeout 3000s;
proxy_send_timeout 3000s;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://webchat_study_271/study/;
}
}
upstream webchat_study_271 {
server 127.0.0.1:8089;
keepalive 16;
}
upstream webchat_study_273{
server 127.0.0.1:8089;
}
网友评论