1

I am trying to edit my 'nginx.conf' file using a bash script intended to help users change environments quickly. The user enters their selected environment as an argument when running the script, which gets stored as a variable on the bash script.

worker_processes  1;
error_log  /usr/local/var/log/nginx/error.log;
events {
   worker_connections  1024;
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
   #gzip  on;
  server {
     listen       80;
     server_name  localhost;
     merge_slashes off;
     location ~\.(jsp)$ {
        proxy_pass http://(environment_IP)
     }
     location ^~/r/ {
        proxy_pass http://127.0.0.1:7300;
     }
     location ~*/wishlist{      
        proxy_pass http://(environment_IP)
     }
     location ~*/media/{        
       proxy_pass http://(environment_IP)
     }
     location ^~/data/{
        proxy_pass http://(environment_IP)/data/
     }
     location ~*/static/{
        proxy_pass http://127.0.0.1:7777;      
     }
     location /{
        proxy_pass http://127.0.0.1:7300/;
     }
  }
   # HTTPS server
   #
   server {
       listen       443 ssl;
       server_name  localhost;
       ssl_certificate      /etc/ssl/certs/myssl.crt;
       ssl_certificate_key  /etc/ssl/private/myssl.key;
       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;
       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers  on;
       location ~\.(jsp)$ {           
           proxy_pass https://(environment_IP)
       }
       location ^~/r/ {
          proxy_pass http://127.0.0.1:7300;
       }
       location ~*/wishlist{        
          proxy_pass https://(environment_IP)
       }
       location ~*/media/{          
          proxy_pass https://(environment_IP)
       }
       location ^~/data/{
          proxy_pass https://(environment_IP)/data/
       }
       location ~*/static/{
          proxy_pass http://127.0.0.1:7777;
       }
       location /{
          proxy_pass http://127.0.0.1:7300/;
       }
   }
   include servers/*;
}

The issue I am running into is finding an optimal method to edit specific lines that are nested within my 'nginx.conf' file. Only certain instances of the 'proxy_pass' line is required to change such as the lines between 'location ~.(jsp)$' and 'location ~/wishlist*'.

Furthermore, the IP at the end of 'location ^~/data/' must have the '/data/' string appended to the end. Lastly, I have to factor in the different instances of http and https that are within the configuration file.

I have tried to edit the values after 'proxy_pass' using sed, however I do not know how to only change specific values that are nested within text. The code I wrote so far finds proxy_pass but changes all instances following the string.

sed "s/.*proxy_pass.*/proxy_pass testpost/"
  • regexps are not a good tool for this job (same as they're not a good tool for parsing json, xml, html, etc). You should use something that can understand (parse) the nested config entries. e.g. see stackoverflow.com/questions/15277453/… and stackoverflow.com/questions/11240422/… – cas Jul 31 '17 at 14:36
  • Are you just trying to change all occurrences of (environment_IP) to a given IP? Do you know you can set and use your own nginx variable inside the server{} section? – meuh Jul 31 '17 at 16:22
0

If your config is regular enough, and assuming you're already splitting it up into small separate files, it will probably be easier to regenerate the whole file from scratch each time, in which case it could be as easy as

#!/bin/sh
ENV_IP=$1
cat > nginx.conf <<EOF
...
...
location ^~/data/{
      proxy_pass https://$ENV_IP/data/
}
...
...
EOF
  • Did not consider this approach - works great and was able to create a backup copy of the user's existing .conf at the same time. – Interplay Aug 7 '17 at 16:59

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.