Configuration Nginx Apache Reverse proxy

Hi,

I'm hosting a couple of sites (some wordpress, some drupal, some regular php) on a Linode using Apache. This sometimes leads to high memory usage.

Because of this I'm thinking about using nginx.

For the statis sites, I suppose I can serve them right from nginx, so no configuration on apache will be needed?

For the wordpress and drupal sites, I'd like to use nginx as reverse proxy.

I've been going through a lot of documentation and configuration examples but I found a lot of contradictions and multiple possibilities.

As I'm running already some production sites, I cannot do a lot of trial and error ;-)

Is there somebody who has this kind of setup who's willing to share his/her configuration with me?

Thanks!

5 Replies

I wrote a little about my Apache/nginx reverse-proxy setup here:

~~[https://forum.linode.com/viewtopic.php?f=12&t=10568#p61207" target="_blank">](https://forum.linode.com/viewtopic.php?f=12&t=10568#p61207](

Here's an example Apache virtual host file:

 <virtualhost *:81="">ServerAdmin webmaster@domain.com
    ServerName www.domain.com
    ServerAlias domain.com

    DocumentRoot /var/www/domain
     <directory var="" www="" domain="">Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all</directory> 

    ErrorLog ${APACHE_LOG_DIR}/errors/domain-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/domain-access.log combined</virtualhost> 

And an example nginx virtual host file:

server {
  listen 80;
  server_name domain.com www.domain.com;
  access_log off;
  error_log off;
  location / { proxy_pass http://127.0.0.1:81; }
  location ~* ^.+\.(htm|html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|bz2|pdf|odt|txt|tar|bmp|rtf|js|swf|avi|mp4|mp3|ogg|flv)(\?.*)?$ {
    expires 30d; #adjust to your static content's update frequency
    root /var/www/domain;
  }
  location ~ /\.ht { deny all; }
}

Thanks Nicholas.

How do you handle parked domains?

On apache i've added them all as serverAlias and use .htaccess to redirect to the 'main' domain.

Parked domain Apache virtual host file:

 <virtualhost *:81="">ServerAlias www.parkeddomain.net
ServerAlias parkeddomain.net
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule (.*) http://www.domain.com$1 [R=301,L]</virtualhost> 

Parked domain nginx virtual host file:

server {
  listen 80;
  server_name parkeddomain.net www.parkeddomain.net;
  access_log off;
  error_log off;
  location / { proxy_pass http://127.0.0.1:81; }
  location ~* ^.+\.(htm|html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|bz2|pdf|odt|txt|tar|bmp|rtf|js|swf|avi|mp4|mp3|ogg|flv)$ {
    expires 30d; #adjust to your static content's update frequency
  }
  location ~ /\.ht { deny all; }
}

Hi Nicholas,

I followed your guidelines but I think I'm still missing something.

Nginx is always displaying the same site. However when I bypass nginx by adding the port on which apache is running the correct site is shown.

I did a test-setup on a Ubuntu server running in Virtualbox.

These are the nginx virtualhost files:

server {
listen 80;
server_name site1;
root /var/www/site1;
location / { proxy_pass http://127.0.1.1:8080; }
 }
server {
listen 80;
server_name site2;
root /var/www/site2;
location / { proxy_pass http://127.0.1.1:8080; }
 }

and these from apache:

 <virtualhost *:8080="">ServerAdmin webmaster@example.com
            ServerName  site1
            DocumentRoot /var/www/site1

        <directory var="" www="" site1="">Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all</directory></virtualhost> 
 <virtualhost *:8080="">ServerAdmin webmaster@example.com
            ServerName  site2
            DocumentRoot /var/www/site1

        <directory var="" www="" site2="">Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all</directory></virtualhost> 

When removing the

location / { proxy_pass http://127.0.1.1:8080; }

from the nginx vhost files and stopping the apache server it's working fine.

What could be missing?

Update: Changed to proxy_pass http://127.0.0.1:8080; and it seems fine now.

I guess I was confused because i saw in some apache logfile the ServerName was 127.0.1.1

next: get this working on the Linode :-)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct