Ẩn IP máy chủ với Nginx

2
527
(Last Updated On: Th11 15, 2018)

Chào tất cả bác bạn. Có một số lý do nào đó mà chúng ta nên ẩn đi IP máy chủ. Có thể vì lý do bảo mật, tránh khỏi các cuộc tấn công DDOS,… hoặc là một số lý do nào đó khác.

Ẩn IP ở đây các bạn cần phân biệt đối với IP của trình khách và IP của máy chủ. IP trình khách (client) tức là bạn đang truy cập từ máy tính, hoặc thiết bị di động như điện thoại hoặc máy tính bảng kết nối vào Internet, và bạn muốn ẩn đi IP để người khác không biết IP thật của bạn là gì (vì sao thì chắc các bạn cũng đã hiểu) và bạn cũng có thể làm điều đó bằng cách kết nối thông qua VPN hay dùng Proxy hoặc dùng phần mềm thứ 3 nào đó để kết nối “bắc cầu” mà thôi. “Bắc cầu” ở đây cũng tương tự như Proxy thôi. Tuy nhiên ở Proxy thì bạn chỉ cần thiết lập với các thông số có sẵn, còn phần mềm bên thứ 3 thì bạn phải cài đặt chương trình của họ và việc còn lại thì phần mềm tự lo, bạn chỉ việc kết nối vào Internet mà thôi.

Tuy nhiên đối với máy chủ thì không phải như vậy. Bởi vì xin mời các bạn xem sơ đồ sau đây:

 

Trình khách (client)

Gửi yêu cầu (request)

DNS (Domain Name Server)

 Máy chủ phân giải tên miền

         (Trỏ đến IP nào đó)

Máy chủ A

IP: 123.123.123.123

Máy chủ B

IP: 102.102.102.102

Chỉ biết được tên miền trỏ về IP này, không biết được IP máy chủ chính là IP của máy chủ B.

Chính vì điều đó nên cách thức hoạt động khác so với Client cho nên sẽ khó khăn hơn một chút để giấu đi IP thật của máy chủ so với việc bạn kết nối tới một Proxy hay VPN để ẩn IP của mình trên máy tính cá nhân hay điên thoại.

Có một cách khá phổ biến mà hiện nay nhiều Webmaster hay sử dụng đó là sử dụng dịch vụ DNS trung gian của CloudFlare hay Incapsula.

Tuy nhiên ở đây không phải là IP thật của bạn đã bị ẩn hoàn toàn. Bởi vì khách hàng hay nhiều tổ chức đúng là không biết nhưng chắc chắn các công ty cung cấp dịch cho bạn chắc chắn sẽ biết (ở đây như CloudFlare hay Incapsula). Mà 1 người biết thì chắc chắn sẽ có thêm nhiều người biết dù có thế nào chăng nữa. Cũng đôi khi một tổ chức có thẩm quyền cao hơn đề nghị thì họ vẫn phải cung cấp cho nên cách này vẫn chưa gọi là ẩn danh IP máy chủ của bạn hoàn toàn.

Và chỉ để cho một mình bạn biết – và cũng chỉ riêng mình bạn biết, kiểu như là “sống để bụng, chết mang theo” như ông cha ta xưa nay hay nói vậy. Thì chúng ta sẽ dùng một cách khác đó là dùng Proxy. Và trong bài viết này, mình sử dụng Nginx – một máy chủ Web cực kỳ tiên tiến và mạnh mẽ được nhiều tổ chức sử dụng (như WordPress.com, một lượng lớn các trang Web có lượng truy cập với thứ hạng dưới 10.000 trên toàn thế giới cũng sử dụng Nginx làm máy chủ Web).

 

Client (trình khách)

Nginx (Proxy)

Nginx, Apache,…

(Web Server)

Ở trên đây là sơ đồ mô tả sơ lược. Nếu không yêu cầu quá cao, bạn chỉ cần thiết lập Proxy cho ổn là được, và phần máy chủ cần che giấu cũng không cần chỉnh sửa gì.

Dưới đây là ví dụ cấu hình một Proxy: (Virtual Host Nginx)

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
upstream backend  {
  server server1.vngeek.com;
}
upstream mediaserver  {
        server server2.vngeek.com;
}
server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  proxy_max_temp_file_size 10000m;	
  root /usr/share/nginx/html;
  index index.html index.htm;

  # Make site accessible from http://localhost/
  server_name vngeek.com www.vngeek.com;
  location / {
    proxy_pass  http://backend;
#		proxy_max_temp_file_size 10000m;
    }
  }
  
server {
        listen 80;
#        listen [::]:80 ipv6only=on;

       root /usr/share/nginx/html;
       index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name media.vngeek.com;
        location / {
                proxy_pass  http://mediaserver;
                }
        }

Ở trên đây có nghĩa là sẽ có một máy chủ cài đặt Nginx làm Proxy.

  • Các truy cập đến tên miền vngeek.comwww.vngeek.com sẽ được đưa đến một tên miền cụ thể là server1.vngeek.com (ở đây không phải là một chuyển hướng như trên trình duyệt nhé và bạn cũng chỉ thấy trên trình duyệt của bạn các địa chỉ vngeek.comwww.vngeek.com được giữ nguyên – Proxy đã che đi tất cả những gì đằng sau nó diễn ra).
  • Tương tự như vậy các truy vấn đến media.vngeek.com được chuyển đến server2.vngeek.com.

Bonus: nginx.conf (/etc/nginx.conf):

user www-data;
worker_processes 4;
pid /run/nginx.pid;
worker_rlimit_nofile  8192;
events {
  worker_connections 768;
  # multi_accept on;
}

http {

  ##
  # Basic Settings
  ##
  proxy_max_temp_file_size 10000m;
  proxy_connect_timeout       6000;
  proxy_send_timeout          6000;
  proxy_read_timeout          6000;
  send_timeout                6000;
  client_header_timeout 2400;
  client_body_timeout 2400;
  fastcgi_read_timeout 2400;
  client_max_body_size 10000m;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 7200;
  types_hash_max_size 2048;
  # server_tokens off;

  server_names_hash_bucket_size 512;
  # server_name_in_redirect off;

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

  ##
  # Logging Settings
  ##

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  ##
  # Gzip Settings
  ##

  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/x-javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # nginx-naxsi config
  ##
  # Uncomment it if you installed nginx-naxsi
  ##

  #include /etc/nginx/naxsi_core.rules;

  ##
  # nginx-passenger config
  ##
  # Uncomment it if you installed nginx-passenger
  ##
  
  #passenger_root /usr;
  #passenger_ruby /usr/bin/ruby;

  ##
  # Virtual Host Configs
  ##

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


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

 

Nói nôm ra rằng tới đây coi như là bạn đã che dấu đi IP máy chủ thật sự của vngeek.com hay www.vngeek.com (như những gì mình đang ví dụ nãy giờ).

Tuy nhiên bạn nên chú ý một điều vì mình ví dụ cho các bạn dễ hiểu cho nên ở trên mình có đề cập đến hai subdomain là server1.vngeek.com và server2.vngeek.com. Thực tế bạn cần trỏ đến 2 IP tĩnh nào đó chứ không phải là hai Subdomain, nếu muốn dùng Subdomain, hãy chỉ định một tên miền khác để đánh lạc hướng (kiểu như server1.vnge.com chẳng hạn), bởi các Record A bất kể lúc nào cũng sẽ để lại dấu vết (ở đây có vẻ nghiêm trọng quá phải không các bạn, nhưng mình quan trọng hóa vấn đề lên cho có vẻ hấp dẫn á mà).

Tuy nhiên để cấu hình một cách đúng như chúng ta sẽ có thiết lập như thế này:

# redirect vngeek.com to https
server {
  listen      80 default_server;
  server_name vngeek.com;
  return 301 https://vngeek.com;
}

# redirect www prefix to https
server {
  listen      80;
  server_name www.vngeek.com;
  return 301 https://www.vngeek.com;
}

server {
  #listen 80;
  listen 443 ssl default_server;
  server_name vngeek.com;
  ssl_certificate /etc/nginx/ssl/vngeek.com.crt;
  ssl_certificate_key /etc/nginx/ssl/vngeek.com.key;

  location / {
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  X-Forwarded-For $remote_addr;
    proxy_set_header  X-Forwarded-Host $remote_addr;
    proxy_pass        http://server1.vngeek.com;
  }
}

Và trên máy chủ được trỏ đến thông qua Proxy cấu hình thế này:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  root /www;
  index index.php index.html index.htm;

  # Make site accessible from http://localhost/
  server_name vngeek.com www.vngeek.com;
  rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
  rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  set_real_ip_from 123.123.123.123;
  set_real_ip_from 192.168.2.86;
  real_ip_header X-Forwarded-For;
  real_ip_recursive on;
# những thiết lập khác...

Đầy đủ chúng ta sẽ có như thế này:

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  root /www;
  index index.php index.html index.htm;

  # Make site accessible from http://localhost/
  server_name vngeek.com www.vngeek.com;
  rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
  rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  set_real_ip_from 123.123.123.123;
  set_real_ip_from 192.168.2.86;
  real_ip_header X-Forwarded-For;
  real_ip_recursive on;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    proxy_max_temp_file_size 10000m;
    try_files $uri $uri/ /index.php?$args;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
  }

  # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
  #location /RequestDenied {
  #	proxy_pass http://127.0.0.1:8080;    
  #}

  #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 /usr/share/nginx/html;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
  #	fastcgi_split_path_info ^(.+\.php)(/.+)$;
  #	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  #
  #	# With php5-cgi alone:
  #	fastcgi_pass 127.0.0.1:9000;
  #	# With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    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;
#	root html;
#	index index.html index.htm;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}


# HTTPS server
#
#server {
#	listen 443;
#	server_name localhost;
#
#	root html;
#	index index.html index.htm;
#
#	ssl on;
#	ssl_certificate cert.pem;
#	ssl_certificate_key cert.key;
#
#	ssl_session_timeout 5m;
#
#	ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#	ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#	ssl_prefer_server_ciphers on;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

Chúc các bạn thành công!

2 BÌNH LUẬN

Bình luận