I have a nginx config file like this:
server {
listen 80;
server_name localhost;
location /api/test {
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
send_timeout 3000;
client_max_body_size 10M;
client_body_buffer_size 100M;
proxy_pass http://demo.com/;
}
location /api/demo {
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
send_timeout 3000;
client_max_body_size 10M;
client_body_buffer_size 100M;
proxy_pass http://demo2.com/;
}
}
how I want to using the url1 to replace the first proxy_pass and url2 to replace the second proxy_pass in shell, this is my shell script looks like:
#!/usr/bin/env bash
set -u
set -e
set -x
echo "please input url1:"
read URL1
echo "$URL1"
echo "plase input url2:"
read URL2
echo "$URL2"
sed -E "12s/.*proxy\_pass.*/proxy\_pass:$URL1/" nginx.conf
sed -E "22s/.*proxy\_pass.*/proxy\_pass:$URL2/" nginx.conf
now I am facing a problem that the sed did not modify the original file, and only output the url in terminal, what should I do to make it modify the original config file? what I want like this (input www.google.com;
and www.facebook.com;
):
server {
listen 80;
server_name localhost;
location /api/test {
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
send_timeout 3000;
client_max_body_size 10M;
client_body_buffer_size 100M;
proxy_pass www.google.com;
}
location /api/demo {
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
send_timeout 3000;
client_max_body_size 10M;
client_body_buffer_size 100M;
proxy_pass www.facebook.com;
}
}
-i
option likesed -i.bak 'script' file
. (2) Don't escape the underscore.\_
is undefined and could result in strange behaviour on some implementation. (3) Is it guaranteed, that the URLs will not contain any slashes, because this would break the script? (4) Are you sure the line numbers will always be 12 and 22? There are other possibilities.