Cấu hình Nginx làm Proxy cho một Web Server khác

0
726
(Last Updated On: Th12 11, 2018)

Ở các bài viết trước, mình đã có hướng dẫn ẩn IP máy chủ bằng cách sử dụng Nginx Proxy. Và hoạt động đối với cả Web Server có SSL: Ẩn IP máy chủ với Nginx. Hôm nay là bài viết dành cho máy chủ web không cài đặt SSL. Đơn giản nhưng hoạt động khá tốt.

Trước tiên là trên máy chủ Nginx Proxy:

Trên Ubuntu, đầu tiên cài đặt

apt-get install software-properties-common

Tiếp sau đó:

apt-get update
add-apt-repository ppa:nginx/stable

Và cài đặt nginx:

apt-get install nginx

Việc đầu tiên đó là chúng ta tiến hành sao lưu tập tin cấu hình Virtual host trên Nginx:

cp default default-backup

Và dưới đây là cấu hình Proxy:

server {
  listen 80;
  server_name vngeek.com;

  location / {
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $remote_addr;
    proxy_set_header  X-Forwarded-Host $remote_addr;
    proxy_pass        http://service.vngeek.net;
  }
}

Các bạn lưu ý, trong trường hợp này:

  • Domain vngeek.com được trỏ vào IP máy chủ được cài đặt Nginx với cấu hình Virtual host như ở trên.
  • service.vngeek.net: Đây là một host, trỏ vào IP của máy chủ chính chứa nội dung (máy chủ được Proxy của chúng ta thiết lập ở trên trỏ đến). Bạn có thể thay bằng một IP tĩnh nếu muốn.

Đối với máy chủ Web được Proxy trỏ tới (máy chủ web chứa nội dung chính):

Đối với Web Server Apache (Nginx cũng tương tự vậy thôi, bởi trỏ tới một Web server đang hoạt động mà):

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName vngeek.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Các bạn chú ý, phần server_name (trên Nginx làm Proxy) và ServerName (trên máy chủ chứa nội dung được Proxy trỏ đến) phải cùng một host giống nhau, như vậy thì Proxy mới hoạt động được. Trong trường hợp của chúng ta đều là vngeek.com.

Proxy có khá nhiều cái hay, giống như một chiếc cầu vừa hữu hình, vừa vô hình. Hữu hình với tất cả mọi người nhìn thấy nó nhưng ẩn chứa đằng sau là gì thì chỉ có máy chủ Proxy mới biết. Chỉ thấy được bên ngoài, không biết được bên trong có gì, chính vì thế bảo vệ an toàn cho máy chủ chính nằm đằng sau, cũng có thể đóng vai trò như một Load Balancing, hoặc là Failover,…

Cuối cùng xin mời các bạn tham khảo mô hình dưới đây:

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

P/S: Cấu hình cho tập tin /etc/nginx.conf của máy chủ Proxy:

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;
#	}
#}

Trên đây là cấu hình tối ưu hóa, cho phép Proxy chờ máy chủ “được Proxy” với thời gian lâu hơn, Upload tập tin lớn hơn, gia tăng kích thước temp (lưu trữ tạm thời), cho phép sử dụng nhiều kết nối đồng thời hơn,…

Bình luận