0

I want to make a backup of the file "/etc/nginx/nginx.conf"

And then edit it so the following line in the file:

include /etc/nginx/sites-enabled/*;

Will instead appear as...

include /etc/nginx/sites-enabled/*;

server {
    listen 8081;

    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_redirect default;
        auth_basic "Server Administration";
        auth_basic_user_file /etc/nginx/passwords;
    }
}

My problem is that I'm still very new to Linux, and I thought I could use sed but my new understanding of it is that it reads line by line, and thus isn't good for multiline content. I'm not sure how to make perl work with it since some of the characters would interfere with perl. My brain is kind of fried just learning how NGINX works as it is.... any help is greatly appreciated.

My current SED Command....

Should work just fine. Yay! :D

sudo sed -i.bak 's/^    include \/etc\/nginx\/sites-enabled\/\*\;$/&\n  \n  server {\n      listen 8081\;\n     \n      location \/ {\n         proxy_pass http:\/\/127.0.0.1:8080\/\;\n            proxy_redirect default\;\n          auth_basic "Server Administration"\;\n          auth_basic_user_file \/etc\/nginx\/passwords\;\n    }\n}\n/' /home/pi/Public/NGINX-test/nginx.conf
  • You want to replace single line by multi-line. Even sed can do it. – AnFi Sep 28 '17 at 6:21
  • Heh, everything I tried just broke it. All I managed to do with sed was replace the entire file with that second block about 10 times. I'm still looking, I just someone might have had experience doing something similar that could answer it quicker than I could figure out digging for the next couple of hours. It's what I've been doing for some time already setting up a program called drawpile to act as a server on the Raspberry Pi 3. – Wade S Sep 28 '17 at 6:24
0

sed can convert single line into multiple lines.
Take a look at the sample "proof of concept" script below. It outputs converted "here doc" document.

#!/bin/sh
sed 's/^include \/etc\/nginx\/sites-enabled\/\*;$/&\nYES!/' <<END
input-line:1
include /etc/nginx/sites-enabled/*;
input-line:3
include /etc/nginx/sites-enabled/*;
END
  • I've managed to get rid of the error after researching it a bit more and no error code is present. But it doesn't do anything. sudo sed -i.bak -e 's/^\/etc\/nginx\/sites-enabled\/*;$/\/etc\/nginx\/sites-enabled\/*;\n \n server {\n listen 8081;\n \n location \/ {\n proxy_pass http:\/\/127.0.0.1:8080\/;\n proxy_redirect default;\n auth_basic "Server Administration";\n auth_basic_user_file \/etc\/nginx\/passwords;\n }\n}\n/' /home/pi/Public/NGINX-test/nginx.conf – Wade S Sep 28 '17 at 8:15
  • I have changed my sample script to look for input line you want. – AnFi Sep 28 '17 at 9:18
  • Thanks, but there's still a problem with it. I have multiple lines that end with the semicolon and it still doesn't seem to run because of it. I need a way of escaping the semicolon as well. – Wade S Sep 28 '17 at 9:40
  • Actually I think I figured it out finally. Thanks for pointing me in the right direction. – Wade S Sep 28 '17 at 10:16
0

In addition to the information given by Andrzej A. Flip, you need to escape the semicolon (;), asterisk (*), and forward slash (/) characters. The \n makes it a new line, and use -i.bak to make a backup of the file before making the change with an .bak extension.

sudo sed -i.bak 's/^    include \/etc\/nginx\/sites-enabled\/\*\;$/&\n  \n  server {\n      listen 8081\;\n     \n      location \/ {\n         proxy_pass http:\/\/127.0.0.1:8080\/\;\n            proxy_redirect default\;\n          auth_basic "Server Administration"\;\n          auth_basic_user_file \/etc\/nginx\/passwords\;\n    }\n}\n/' /home/pi/Public/NGINX-test/nginx.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.