Skip to content

Instantly share code, notes, and snippets.

@anjia0532
Created December 14, 2017 04:08
Show Gist options
  • 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;
}
}
}
@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