Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a VPS server with installed DirectAdmin and httpd apache server. I heard that if you using nginx as a front-end and then apache as back-end it can speed up the loading of website. but in some tutorials that I have looked I need to configure virtual hosts in apache and nginx. in my server i have a lot of virtual hosts (DirectAdmin accounts) and it's a lot of work to do.

Is there any shortcut?

share|improve this question

migrated from webmasters.stackexchange.com Mar 9 '13 at 16:25

This question came from our site for pro webmasters.

    
Is copy and paste a shortcut? –  Stephen Ostermiller Mar 9 '13 at 10:54
    
I mean is there a script that does this job already? –  Daniel Chernenkov Mar 9 '13 at 11:23
    
What do you need to do? Import your vhosts from apache into nginx, or is adding the reverse proxy directives to each one that is time consuming? –  Stephen Ostermiller Mar 9 '13 at 11:24
    
Exactly, i need to add reverse proxy directives to each account.. –  Daniel Chernenkov Mar 9 '13 at 11:31
add comment

1 Answer 1

up vote 1 down vote accepted

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:

  1. Search for "location /"
  2. Position the cursor at the top of the document
  3. Start recording the macro
  4. Press "ctrl g" to search again, finding the first instance of "location /"
  5. Press the "home" key and then the "down" arrow to get to the beginning of the next line
  6. Press "ctrl right arrow" to move the cursor by words until I am at the start of "myapp"
  7. Press "ctrl shift right arrow" and then "shift left arrow" to highlight "myapp"
  8. Press "ctrl c" to copy "myapp"
  9. Type the proxy directives, using "ctrl p" to paste in "myapp" when needed
  10. Stop recording the macro
  11. Replay the macro as many times as needed.
share|improve this answer
    
Great! thanks for your help. I will use the macros.. –  Daniel Chernenkov Mar 9 '13 at 14:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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