This is the first day of my participation in the August Text Challenge.More challenges in August

Background:

  1. Port 8080 of the server is startednodeService, provide someapiRequest (GET/POST/PATCH/PUT/DELETE)
  2. Port 80 of the server is enablednginxProxy, access/jsonWhen usingproxy_passReverse proxy to port 8080nodeservice

In case

When the GET/POST/PATCH request is sent to xxx.xx.com/json, the interface runs normally, but an error is reported when the DELETE /services/:servicename request is sent. After studying this request, the following cases appear:

  1. DELETE xxx.xx.com/json/services/redux ok
  2. DELETE xxx.xx.com/json/services/@orgName/microA 404 fail
  3. DELETE xxx.xx.com/json/services/%40orgName%2FmicroA 404 fail
  4. DELETE xxx.xx.com:8080/services/redux ok
  5. DELETE xxx.xx.com:8080/services/%40orgName%2FmicroA ok

conclusion

RESTFUL path parameter request. If the parameter contains escape characters, the request will fail when the nginx proxy_pass reverse proxy is used

The Nginx part is configured as follows

location /json/ {
    add_header Access-Control-Allow-Origin *;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass  http://xxx.xx.com:8080/;
    root /usr/share/nginx;
}
Copy the code

Thinking 🤔

If there are escape characters in the path, the request using the Nginx proxy will result in 404, and 404 is not matched to the request path, but the request is successful when accessing the Node service directly, so it can be found that when the request with escape characters is sent to Nginx, Nginx processes the escaped URL, resulting in an incorrect match.

Modify Nginx proxy_pass configuration

proxy_pass  http://xxx.xx.com:8080/;
Copy the code
if ($request_uri ~* ^/json/(.*)$) {
   proxy_pass  http://xxx.xx.com:8080/$1;
}
Copy the code

The last

When accessing the request with escape characters, nginx proxy_pass reverse proxy to Node service is successfully implemented without processing the subsequent parameters of URL.

Welcome ━ (` ∀ ´) all dwellings ノ! reading