Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

anjia0532 commented Dec 14, 2017

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

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

@arc41t3ct
Copy link

arc41t3ct commented Sep 18, 2018

If you have no args then token is missing the ?

/testk1=v1

This only works if you already have args

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