Konfigurasi Nginx sebagai Reverse Proxy Apache

Tags: April 28, 2012 2:49 PM

There is no home like Apache mod_php, itu mungkin yang dikatakan PHP. Performa PHP yang berjalan sebagai Fast-CGI process terkadang masih kalah dengan performa mod_php. Keunggulan utama Nginx adalah memory kecil dan sangat cepat dalam memproses static content. Untuk itu kenapa tidak memakai keunggulan dari keduanya sekaligus, mod_php sebagai PHP handler dan Nginx sebagai static content handler. It makes everyone happy :).

>
Web Server Interface Alamat IP Keterangan
Nginx eth0 192.168.1.10 Static content URL » /static
Apache lo 127.0.0.1 Dinamic content handler (PHP)

Nginx

Konfigurasi file /etc/nginx/nginx.conf.

user  www-data;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    server_names_hash_bucket_size 64;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  10;

    gzip   on;
    gzip_comp_level  2;
    gzip_vary  on;
    gzip_disable "MSIE [1-6]\.";
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript 
                    image/jpeg image/png image/gif;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/site-enabled/*;
}

Konfigurasi /etc/nginx/conf.d/proxy.conf

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
client_header_buffer_size 64k;
proxy_connect_timeout   30;
proxy_send_timeout      60;
proxy_read_timeout      30;
proxy_buffer_size   32k;
proxy_buffers       64   32k;
proxy_busy_buffers_size 128k;

Konfigurasi website dengan host example.com /etc/nginx/site-available/example.com.

server {
    listen       192.168.1.10:80;
    server_name  example.com;

    # lempar semua request ke Apache kecuali ada konfigurasi directive location lain
    location / {
       proxy_pass http://127.0.0.1;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # lokasi static content
    # URL yang diawali http://example.com/static akan langsung dihandle nginx dan tidak dilempar ke apache
    #
    # Contoh:
    # http://example.com/static/js/jquery.js
    # http://example.com/static/css/style.css
    location /static {
        expires max;  # masa expires maximum agar browser menyimpan cache lebih lama

        # dokumen webroot dari URL /static
        root /var/www/vhosts/example.com
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

Apache

Konfigurasi file /etc/apache2/ports.conf.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1

#<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
#    Listen 443
#</IfModule>

#<IfModule mod_gnutls.c>
#    Listen 443
#</IfModule>

Konfigurasi virtual host file /etc/apache2/site-available/example.com.

<VirtualHost 127.0.0.1:80>
 ServerAdmin you@example.com
 ServerName example.com

 # Flag berikut ditujukan untuk PHP debugging dalam proses development
 # Hapus/beri tanda komentar jika masuk tahap production
 <IfModule mod_php5.c>
  php_admin_flag engine on
  php_flag display_errors on
  # E_ALL | E_STRICT = 32767
  php_value error_reporting 32767
 </IfModule>

 DocumentRoot /var/www/vhosts/rio.lokasiponsel.com/php
 <Directory />
  Options FollowSymLinks
  # Ganti None dengan All jika ingin support .htaccess
  AllowOverride None
 </Directory>

 ErrorLog /var/log/apache2/error.log

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

 CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Install mod_rpaf agar IP address yang dilog apache bukan 127.0.0.1 tetapi IP dari user asli yang mengakses via reverse proxy.

# apt-get install libapache2-mod-rpaf

Aktifkan vhost

# cd /etc/nginx/site-enabled
# ln -s ../site-available/example.com
# cd /etc/apache2/site-enabled
# ln -s ../site-available/example.com

Restart masing-masing service kemudian cek apakah keduanya sudah bind di port 80.

# service apache2 restart
# service nginx restart
# netstat -ntap | grep :80
tcp        0      0 127.0.0.1:80            0.0.0.0:*               LISTEN      4316/apache2    
tcp        0      0 192.168.1.10:80         0.0.0.0:*               LISTEN      4108/nginx.conf

Share on Facebook Twitter

0 comments:

Post a Comment