Cấu hình cho máy chủ Web Nginx hoạt động với IPv6

0
345
(Last Updated On: Th4 2, 2019)

Chào tất cả các bạn. Nginx là một phần mềm máy chủ Web mạnh mẽ và hiệu năng cao, có lẽ mình không cần phải khoe khoang hay nói tốt cho em nó bởi vì những ai đã và đang sử dụng Nginx đều biết rõ…

Theo xu hướng của thời đại, IPv6 sẽ được sử dụng nhiều hơn và dần dần sẽ thay thế tiền bối IPv4 trong tương lai. Chính vì thế, những người quản trị Web như chúng ta cần phải cập nhật thông tin cũng như cấu hình máy chủ hoạt động tương thích với IPv6 mới.

Trong bài viết này mình xin chia sẻ các bạn cấu hình kích hoạt IPv6 trên máy chủ Web sử dụng Nginx.

IPv6 được sử dụng ngày càng nhiều, song song với IPv4 và sẽ dần thay thế IPv4 trong tương lai không xa.

Nếu trước đây đối với những phiên bản Nginx cũ bạn cần phải thêm Module –with-ipv6 thì hiện nay không cần nữa với các phiên bản mới gần đây.

Nếu bạn lo lắng thiếu đi những Module cần thiết, bạn có thể cài đặt gói mở rộng Nginx với dòng lệnh đơn giản:

apt-get install nginx-extras

Bạn sẽ có hàng loạt Module mở rộng và chắc chắn sẽ không thiếu Module hoạt động với IPv6 nên đừng lo lắng gì cả.

Bây giờ chúng ta sẽ lướt qua tập tin Virtual host trên Nginx một chút:

server {
        listen   80; ## lắng nghe ipv4;
        #listen   [::]:80 default ipv6only=on; ## lắng nghe IPv6

        root /var/www/vngeek.com/public_html;
        index index.php index.html;

        server_name vngeek.com;
}

Tất nhiên các bạn cần chú ý một điều, máy chủ có IPv6 thì Client kết nối cũng phải có IPv6. Đây là điều kiện cần để Client có thể kết nối với máy chủ được cấu hình chỉ hoạt động trên IPv6.

Và để kích hoạt IPv6 thì bạn chỉ cần thêm dòng:

listen [::]:80;

Tất nhiên phải nằm trong mục server {… }.

Khi đó tập tin cấu hình trông sẽ như thế này:

server {
        listen   80; ## lắng nghe ipv4;
        listen   [::]:80; ## lắng nghe IPv6

        root /var/www/vngeek.com/public_html;
        index index.php index.html;

        server_name vngeek.com;
}

Bạn có thể cấu hình nâng cao thêm, chẳng hạn như chỉ lắng nghe trên IPv6 (cho phép IPv6 kết nối, còn IPv4 thì không kết nối được):

listen [::]:443 default ipv6only=on;
listen [::]:80 default ipv6only=on;

Có nghĩa là Lắng nghe trên cổng 80 HTTP và 443 HTTPS và chỉ cho phép IPv6 kết nối, từ chối IPv4.

Chẳng hạn như cấu hình của VnGeek.com mời các bạn tham khảo:

##
# 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.
##

# Default server configuration
#
server {
	listen 80;
	listen [::]:80;
	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;
	client_max_body_size 500m;
	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name vngeek.com;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		#try_files $uri $uri/ =404;
		try_files $uri $uri/ /index.php$is_args$args;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	#
	#	# With php7.0-cgi alone:
		fastcgi_pass 127.0.0.1:9000;
	#	# With php7.0-fpm:
	#	fastcgi_pass unix:/run/php/php7.0-fpm.sock;
		fastcgi_read_timeout 300;
		fastcgi_buffers 8 16k;
		fastcgi_buffer_size 32k;
		fastcgi_connect_timeout 300;
		fastcgi_send_timeout 300;
	}

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

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/vngeek.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/vngeek.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

server {
    if ($host = vngeek.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	listen 80 default_server;
	listen [::]:80 default_server;

	server_name vngeek.com;
    return 404; # managed by Certbot


}

Cấu hình ở trên sẽ chuyển hướng các Request đến địa chỉ http://vngeek.com sang https://vngeek.com.

Lắng nghe trên cả IPv4 và IPv6.

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

Bình luận