I highly doubt that somebody has written any scripts to add proxy code to each virtual host.
I usually approach problems like this in one of two ways:
- Regular expression search and replace
- Text editor macros
I use a text editor called nedit and I blogged about why it is awsome. It has excellent regex support and macros.
Presumably you have a bunch of nginx directives like this:
server {
listen myhost:80;
server_name myhost;
location / {
root /path/to/myapp/public;
}
}
which you need to make look like this
server {
listen myhost:80;
server_name myhost;
location / {
root /path/to/myapp/public;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myapp:8080;
}
}
Regex Search and Replace
I use regex search for root /path/to/(myapp)/public (note the parenthesis around the myapp, so that I can use it in the replacement) and I replace it with:
root /path/to/\1/public;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://\1:8080;
Macros
I could create a macro to do to same thing using the following procedure:
- Search for "location /"
- Position the cursor at the top of the document
- Start recording the macro
- Press "ctrl g" to search again, finding the first instance of "location /"
- Press the "home" key and then the "down" arrow to get to the beginning of the next line
- Press "ctrl right arrow" to move the cursor by words until I am at the start of "myapp"
- Press "ctrl shift right arrow" and then "shift left arrow" to highlight "myapp"
- Press "ctrl c" to copy "myapp"
- Type the proxy directives, using "ctrl p" to paste in "myapp" when needed
- Stop recording the macro
- Replay the macro as many times as needed.