Hướng dẫn cài đặt NGINX REVERSE PROXY và CACHING với MULTIPLE DOMAIN VHOST

INSTALL​​ NGINX REVERSE PROXY AND CACHING​​ MULTIPLE DOMAIN VHOST

 

 

Mình có lab thử bài sử dụng nginx làm reverse proxy và cache cho các backend để tăng tốc cho website lơn. chi tiết như sau:

ảnh sưu tầm​​ https://autrunk.com/

#NGINX RESERVE PROXY CACHING multiple domain

#If you install Nginx from source. You must create the folder sites-available and sites-enabled

vi /etc/nginx/sites-available/cloudx.com.vn.conf

#Paste this content

proxy_cache_path /var/cache/nginx/cloudx.com.vn​​ levels=1:2 keys_zone=my_cache1:10m max_size=10g​​ inactive=60m use_temp_path=off;

server {

 ​​​​ listen 80;

 ​​​​ listen [::]:80;

 

 ​​​​ server_name​​ cloudx.com.vn;

 ​​​​ access_log  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ /var/log/nginx/cloudx.com.vn/access.log;

 ​​​​ error_log  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ /var/log/nginx/cloudx.com.vn/error.log;

 

 ​​​​ location / {

 ​​ ​​ ​​ ​​ ​​​​ 

  ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ Host $host;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Real-IP $remote_addr;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Forwarded-For $proxy_add_x_forwarded_for;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Forwarded-Proto $scheme;

  ​​​​ proxy_pass http://10.0.1.26:81/;

  ​​​​ proxy_read_timeout  ​​ ​​ ​​ ​​​​ 90;

 ​​​​ }

}

#link symbolink​​ (soft link, mình sẽ giới thiệu 1 bài về softlink và hard link trong 1 hướng dẫn khác.)

ln -s /etc/nginx/sites-available/cloudx.com.vn.conf /etc/nginx/sites-enabled/cloudx.com.vn.conf

vi /etc/nginx/sites-available/fixloinhanh02.com.conf

#Paste this content

 

 

 

proxy_cache_path /var/cache/nginx/fixloinhanh02.com​​ levels=1:2 keys_zone=my_cache2:10m max_size=10g

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ inactive=60m use_temp_path=off;

server {

 ​​​​ listen 80;

 ​​​​ listen [::]:80;

 

 ​​​​ server_name​​ fixloinhanh02.com;

 ​​​​ access_log  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ /var/log/nginx/fixloinhanh02.com/access.log;

 ​​​​ error_log  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ /var/log/nginx/fixloinhanh02.com/error.log;

 

 ​​​​ location / {

 ​​ ​​ ​​ ​​ ​​​​ 

  ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ Host $host;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Real-IP $remote_addr;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Forwarded-For $proxy_add_x_forwarded_for;

 ​​ ​​ ​​ ​​ ​​​​ proxy_set_header  ​​ ​​ ​​ ​​ ​​ ​​​​ X-Forwarded-Proto $scheme;

  ​​​​ proxy_pass http://10.0.1.26:81/;

  ​​​​ proxy_read_timeout  ​​ ​​ ​​ ​​​​ 90;

 ​​​​ }

}

Chi tiết cấu hình của file​​ /etc/nginx/nginx.conf

###Nginx server block caching

 

 

#user ​​ nobody;

worker_processes auto;

 

error_log ​​ /var/log/nginx/error.log;

#error_log ​​ logs/error.log ​​ notice;

#error_log ​​ logs/error.log ​​ info;

 

#pid  ​​ ​​ ​​ ​​ ​​ ​​​​ /var/run/nginx.pid;

 

 

events {

 ​​ ​​ ​​​​ worker_connections ​​ 1024;

}

 

 

http {

 ​​ ​​ ​​​​ include  ​​ ​​ ​​ ​​ ​​​​ 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 ​​ logs/access.log ​​ main;

 

 ​​ ​​ ​​​​ sendfile  ​​ ​​ ​​ ​​ ​​ ​​​​ on;

 ​​ ​​ ​​​​ #tcp_nopush  ​​ ​​ ​​​​ on;

 

 ​​ ​​ ​​​​ #keepalive_timeout ​​ 0;

 ​​ ​​ ​​​​ keepalive_timeout ​​ 65;

 

 ​​ ​​ ​​​​ gzip ​​ on;

gzip_disable "msie6";

 

gzip_vary on;

gzip_proxied any;

gzip_comp_level 6;

gzip_buffers 16 8k;

gzip_http_version 1.1;

gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/  ​​ ​​​​ javascript;

 

 

 ​​ ​​ ​​​​ proxy_temp_path  ​​ ​​​​ /var/cache/nginx/tmp;

 ​​ ​​ ​​​​ include /etc/nginx/sites-enabled/*;

 

 ​​ ​​ ​​​​ server {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ listen  ​​ ​​ ​​ ​​ ​​​​ 80;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ server_name ​​ localhost;

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #charset koi8-r;

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #access_log ​​ logs/host.access.log ​​ main;

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ location / {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ root  ​​​​ html;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ index index.php ​​ index.html index.htm;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #error_page ​​ 404  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ /404.html;

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ # redirect server error pages to the static page /50x.html

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ error_page  ​​​​ 500 502 503 504 ​​ /50x.html;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ location = /50x.html {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ root  ​​​​ html;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ # proxy the PHP scripts to Apache listening on 127.0.0.1:80

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #location ~ \.php$ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ proxy_pass  ​​​​ http://127.0.0.1;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #}

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #location ~ \.php$ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ root  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ html;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ fastcgi_pass  ​​​​ 127.0.0.1:9000;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ fastcgi_index ​​ index.php;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ fastcgi_param ​​ SCRIPT_FILENAME ​​ /scripts$fastcgi_script_name;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ include  ​​ ​​ ​​ ​​ ​​ ​​​​ fastcgi_params;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #}

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ # deny access to .htaccess files, if Apache's document root

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ # concurs with nginx's one

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #location ~ /\.ht {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #  ​​ ​​​​ deny ​​ all;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ #}

 

 ​​ ​​ ​​​​ }

 

 

 ​​ ​​ ​​​​ # another virtual host using mix of IP-, name-, and port-based configuration

 ​​ ​​ ​​​​ #

 ​​ ​​ ​​​​ #server {

 ​​ ​​ ​​​​ #  ​​ ​​​​ listen  ​​ ​​ ​​ ​​ ​​​​ 8000;

 ​​ ​​ ​​​​ #  ​​ ​​​​ listen  ​​ ​​ ​​ ​​ ​​​​ somename:8080;

 ​​ ​​ ​​​​ #  ​​ ​​​​ server_name ​​ somename ​​ alias ​​ another.alias;

 

 ​​ ​​ ​​​​ #  ​​ ​​​​ location / {

 ​​ ​​ ​​​​ #  ​​ ​​ ​​ ​​ ​​ ​​​​ root  ​​​​ html;

 ​​ ​​ ​​​​ #  ​​ ​​ ​​ ​​ ​​ ​​​​ index ​​ index.html index.htm;

 ​​ ​​ ​​​​ #  ​​ ​​​​ }

 ​​ ​​ ​​​​ #}

 

 

 ​​ ​​ ​​​​ # HTTPS server

 ​​ ​​ ​​​​ #

 ​​ ​​ ​​​​ #server {

 ​​ ​​ ​​​​ #  ​​ ​​​​ listen  ​​ ​​ ​​ ​​ ​​​​ 443 ssl;

 ​​ ​​ ​​​​ #  ​​ ​​​​ server_name ​​ localhost;

 

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_certificate  ​​ ​​ ​​ ​​​​ cert.pem;

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_certificate_key ​​ cert.key;

 

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_session_cache  ​​ ​​​​ shared:SSL:1m;

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_session_timeout ​​ 5m;

 

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_ciphers ​​ HIGH:!aNULL:!MD5;

 ​​ ​​ ​​​​ #  ​​ ​​​​ ssl_prefer_server_ciphers ​​ on;

 

 ​​ ​​ ​​​​ #  ​​ ​​​​ location / {

 ​​ ​​ ​​​​ #  ​​ ​​ ​​ ​​ ​​ ​​​​ root  ​​​​ html;

 ​​ ​​ ​​​​ #  ​​ ​​ ​​ ​​ ​​ ​​​​ index ​​ index.html index.htm;

 ​​ ​​ ​​​​ #  ​​ ​​​​ }

 ​​ ​​ ​​​​ #}

 

}

 

Kết luận:

Như vậy mình​​ đã cấu hình nginx proxy caching cho 2 domain cloudx.com.vn và fixloinhanh02.com thành công.​​ các bước chi tiết cài nginx mình không trình bày ở đây.​​ 

Lưu ý: mình định nghĩa từ vhost để chạy multiple domain, thực tế nginx sẽ là server block

 

xin cảm ơn các bạn đã theo dõi!

BÀI VIẾT CÙNG CHUYÊN MỤC