-
-
Save anjia0532/da4a17f848468de5a374c860b17607e7 to your computer and use it in GitHub Desktop.
| worker_processes 1; | |
| 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; | |
| keepalive_timeout 65; | |
| server { | |
| listen 47777; | |
| server_name localhost; | |
| access_log logs/47777.access.log main; | |
| set $token ""; # declar token is ""(empty str) for original request without args,because $is_args concat any var will be `?` | |
| if ($is_args) { # if the request has args update token to "&" | |
| set $token "&"; | |
| } | |
| location /test { | |
| set $args "${args}${token}k1=v1&k2=v2"; # update original append custom params with $token | |
| # if no args $is_args is empty str,else it's "?" | |
| # http is scheme | |
| # service is upstream server | |
| proxy_pass http://127.0.0.1:46666$uri$is_args$args; # proxy pass | |
| } | |
| } | |
| server { | |
| listen 46666; | |
| server_name localhost; | |
| access_log logs/46666.access.log main; | |
| location / { | |
| root html; | |
| index index.html index.htm; | |
| } | |
| } | |
| } |
anjia0532
commented
Dec 14, 2017
the access_log
127.0.0.1 - - [14/Dec/2017:12:01:03 +0800] "GET /test?k1=v1&k2=v2 HTTP/1.0" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.15 Safari/537.36" "-"
127.0.0.1 - - [14/Dec/2017:12:01:21 +0800] "GET /test/?k1=v1&k2=v2 HTTP/1.0" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.15 Safari/537.36" "-"
127.0.0.1 - - [14/Dec/2017:12:01:31 +0800] "GET /test/?a=a&b=b&k1=v1&k2=v2 HTTP/1.0" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.15 Safari/537.36" "-"
127.0.0.1 - - [14/Dec/2017:12:01:35 +0800] "GET /test?a=a&b=b&k1=v1&k2=v2 HTTP/1.0" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.15 Safari/537.36" "-"
If you have no args then token is missing the ?
/testk1=v1
This only works if you already have args
If you are missing the ? with even when using $is_args in the URI then making the default token ? should fix this:
set $token "?"
if there are no args then it will be ? else it will be &
If you have no args then token is missing the ?
/testk1=v1
This only works if you already have args
/testk1=v1 it is wrong syntax
If you are missing the
?with even when using$is_argsin the URI then making the default token?should fix this:set $token "?"if there are no args then it will be?else it will be&
https://gist.github.com/anjia0532/da4a17f848468de5a374c860b17607e7#file-nginx-conf-L38
If has args, set $token "&"; and
$is_args=? ,$ {args}${token}k1=v1&k2=v2 eq ${params}&k1=v1&k2=v2
If not has args, set $token ""; and$is_args='' ,$ {args}${token}k1=v1&k2=v2 eq ?k1=v1&k2=v2