Skip to content

Instantly share code, notes, and snippets.

@anjia0532
Created December 14, 2017 04:08
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save anjia0532/da4a17f848468de5a374c860b17607e7 to your computer and use it in GitHub Desktop.
Save anjia0532/da4a17f848468de5a374c860b17607e7 to your computer and use it in GitHub Desktop.
nginx proxy_pass add a static parameter
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
Copy link
Author

http://localhost:47777/test ==> http://127.0.0.1:46666/test?k1=v1&k2=v2
http://localhost:47777/test/ ==> http://127.0.0.1:46666/test/?k1=v1&k2=v2
http://localhost:47777/test/?a=a&b=b ==> http://127.0.0.1:46666/test/?a=a&b=b&k1=v1&k2=v2
http://localhost:47777/test?a=a&b=b ==> http://127.0.0.1:46666/test?a=a&b=b&k1=v1&k2=v2

@anjia0532
Copy link
Author

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" "-"

@arc41t3ct
Copy link

If you have no args then token is missing the ?

/testk1=v1

This only works if you already have args

@dasper
Copy link

dasper commented May 6, 2024

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 &

@anjia0532
Copy link
Author

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

@anjia0532
Copy link
Author

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 &

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment