2

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;
    }
}
1
  • 1
    (1) If you want to modify the file, you need to use the -i option like sed -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.
    – Philippos
    Sep 14, 2022 at 9:48

1 Answer 1

1

To make sed modify the original file, you need -i. Also, please don't force your users to manually enter things. That just makes your script harder to use: you cannot re-run easily, you cannot automate, and it is very easy to make a mistake. Instead, make the script read the URLs from the command line:

#!/usr/bin/env bash
set -u
set -e
set -x


sed -i -e "12s/.*proxy_pass.*/proxy_pass:$1;/" \
       -e "22s/.*proxy_pass.*/proxy_pass:$2;/" nginx.conf

You should probably make a backup though, and you want to keep the alignment of the data, and you don't have proxy_pass: in the file, you have proxy_pass , which looks like the right format, so try something like this:

#!/usr/bin/env bash
set -u
set -e
set -x

timestamp=$(date +%s.%N)

sed -Ei."$timestamp".bak -e "12s/(.*proxy_pass ).*/\1$1;/" \
        -e "22s/(proxy_pass ).*/\1$2;/" nginx.conf

You then run this with the IPs as arguments:

$ foo.sh 1.2.3.4 5.6.7.8
++ date +%s.%N
+ timestamp=1663148866.892161961
+ sed -Ei.1663148866.892161961.bak -e '12s/(.*proxy_pass ).*/\11.2.3.4/' -e '22s/(proxy_pass ).*/\15.6.7.8;/' nginx.conf

That will create a file with a name like nginx.conf.1663148416.987587536.bak with the original nginx.conf file, and make the changes in nginx.conf.

2
  • Wouldn't this force the user to give www.foo.com; as an argument, needing to escape the semicolon? Maybe proxy_pass[^;]* would be a better choice.
    – Philippos
    Sep 14, 2022 at 9:55
  • @Philippos yes indeed, good catch thanks! I just added the ; to the end of the substitution. I suspect it doesn't really mater here since the proxy_pass is the last entry of each section, but it might in other cases.
    – terdon
    Sep 14, 2022 at 10:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .