Showing posts with label nginx. Show all posts
Showing posts with label nginx. Show all posts

Sunday, March 18, 2018

Nginx Reverse Proxy

Halo Sobat Nostra,
Kali ini saya akan berbagi tutorial bagaimana membuat nginx sebagai reverse proxy untuk apache web server dibelakangnya. K
ita dapat menggabungkan 2 webserver pada satu vps yang mana nantinya Nginx sebagai front end (sebagai web server statis) sedangkan Apache sebagai back end (pemroses).

Nginx adalah server HTTP dan reverse proxy gratis berbasis open-source berkemampuan tinggi. Perangkat lunak ini diciptakan oleh Igor Sysoev pada tahun 2002, dan dirilis untuk pertama kalinya secara umum pada tahun 2004. Nginx terkenal karena performanya yang tinggi, stabil, memiliki banyak fitur, mudah dikonfigurasi, dan menggunakan sedikit sumberdaya pada server.

Sebenarnya jenis proxy tersebut adalah Forward Proxy. Sebaliknya, ada jenis Proxy yang berjalan dari sisi lainnya yaitu sisi server, Web Server lebih tepatnya, dan inilah yang dimaksud dengan Reverse Proxy. 

Pada tutorial ini kita akan menggunakan os Centos 7.x. yang di dalamnya sudah terpasang apache/httpd.

Karena default repo pada mesin tidak ada paket nginx, maka perlu menambahkan EPEL.
yum update && yum install epel-release

Ubah konfigurasi apache/http sebelum menginstall nginx.
vim /etc/httpd/conf/httpd.conf

Ubah port 80  menjadi listen port 8080 dan 8081 untuk digunakan web 1 dan web 2 nantinya.
Listen 8080
Listen 8081

Lalu tambahkan rule dibawah ini pada file httpd.conf, letakkan paling bawah. Kita membuat virtual host untuk web 1 dengan port 8080 dan virtual host web2 dengan port 8081.
<VirtualHost *:8080>
    ServerName horas.tk
    ServerAlias web1.horas.tk
    DocumentRoot /var/www/html/web1/
    ErrorLog /var/www/html/web1/error.log
    CustomLog /var/www/html/web1/requests.log combined
</VirtualHost>

<VirtualHost *:8081>
    ServerName horas.tk
    ServerAlias web2.horas.tk
    DocumentRoot /var/www/html/web2/
    ErrorLog /var/www/html/web2/error.log
    CustomLog /var/www/html/web2/requests.log combined
</VirtualHost>

Jika sudah selesai save konfigurasi tersebut. Selanjutnya kita akan menginstall nginx.
yum install nginx

Kemudian edit file nginx.conf
vim /etc/nginx/nginx.conf

Beri tanda command seperti dibawah ini untuk menonaktifkan rule default konfigurasi nginx.

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
        #root         /usr/share/nginx/web;

#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;

#        location / {
#        }
#        error_page 404 /404.html;
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

Buat konfigurasi baru nginx

vim /etc/nginx/nginx.conf

Isi file tersebut dengan rule dibawah ini.
server {
    client_max_body_size 40M;
    listen 80;
    server_name web1.horas.tk;

        location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://web1.horas.tk:8080;
        proxy_redirect off;
        }
}

server {
    client_max_body_size 40M;
    listen 80;
    server_name web2.horas.tk;
location / {
 proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://web2.horas.tk:8081;
        proxy_redirect off;
        }
}

Restart kembali service httpd dan nginx


service httpd restart && service nginx restart

Untuk memastikannya port sudah listen dapat menggunakan perintah dibawah ini.
[root@node1 land]# netstat -ntlupa|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30275/nginx: master
tcp        0      0 10.148.0.4:47668        169.254.169.254:80      ESTABLISHED 888/python
tcp        0      0 10.148.0.4:47654        169.254.169.254:80      ESTABLISHED 885/python
tcp        0      0 10.148.0.4:22           103.47.132.16:54780     ESTABLISHED 30690/sshd: land [p
tcp        0      0 10.148.0.4:47664        169.254.169.254:80      ESTABLISHED 889/python
tcp        0      0 10.148.0.4:47644        169.254.169.254:80      CLOSE_WAIT  888/python
tcp6      0      0 :::8080                 :::*                    LISTEN      30038/httpd
tcp6      0      0 :::8081                 :::*                    LISTEN      30038/httpd


Selanjutnya kita bisa mengecek via browser web1.horas.tk



Tampilan web2.horas.tk


Sekian tutorial dari saya, semoga bermanfaat :)

Friday, December 15, 2017

BROTLI compression with NGINX

     Hello guys, this time i wanna share about BROTLI. What is brotli?, Why Brotli?, Comparison and how to build the packages. Let's begin with what brotli is.      

BROTLI is a new compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm + Huffman algorithms.
LZ77 overview :
abcdeabcdf can be compressed with LZ77 to abcde<4,5>f and aaaaaaaaaa can be compressed to simply a<9, 1>.
Huffman : Bit Conversion. I will not discuss this in depth but if you want to, take a look on this brotli-ietf.

Developed by Google.
Why Brotli?

The smaller compressed size allows for better space utilization and faster page loads.
As the smaller compressed size would give additional benefits to mobile users, such as lower data transfer fees and reduced battery use. Compressed content takes less time to transfer and reduces load times.

Brotli comes  with 0 to 9 levels of compression.


Comparison with others?



Browser Support?

Brotli compression not yet supported by all varian of web browser, so make sure you have a "PLAN B" compression such as gzip. 


Build the package?

Default nginx package not include brotli module, so we must add brotli module manually and rebuild the nginx package. Even there is bortli-included nginx package on the web (not official), it's recommended to build it yourself in concern of security. So, follow this step

I'm using Centos 7 when i build the package.

- Install the prerequisite package

- Download nginx package using wget. Pull ngx_brotli module using git and update the module.


- Before we configure nginx, add nginx user and temporary folder

- Decompress nginx package and configure nginx to include the ngx_brotli module. After that make the installment.

 
- Check nginx version

- Next, we need to make nginx systemd service

- Run nginx service to make sure it's running and enable it

- Configure nginx.conf file to enable brotli module, in my case i enabled the gzip algorithm too, because in case the browser not support with brotli it will redirect the algorithm to gzip.

- And then restart nginx

Result ?

This is the result sample when brotli compression implemented with nginx server.



Happy Brotling !!!


Referensi :
- https://www.enovate.co.uk/blog/2017/02/28/how-to-brotli-compression-with-nginx
- https://certsimple.com/blog/nginx-brotli
- https://sekolahlinux.com/implementasi-nginx-libressl-http2-brotli-pada-centos-7