Blog Cấu hình website tự động chuyển từ HTTP sang HTTPS

1.Cấu hình trên Apache
Bạn có thể dùng file .htaccess vốn luôn được hỗ trợ bởi các máy chủ chạy Apache. Mở file .htaccess và tìm đoạn sau:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Hoặc
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

2.Cấu hình trên Nginx

Chúng ta mở file cấu hình nginx.conf (hoặc /etc/nginx/sites-enabled/domain.conf) và tìm đoạn cấu hình cho website trên port 80, sau đó điều chỉnh như sau:

server {
listen 80;
server_name yourdomain.com;
return 301 https://$server_name$request_uri;
}

Cuối cùng khởi động lại nginx: service nginx restart 

3.Cấu hình trên Tomcat/JBoss

Đầu tiên bạn mở file cấu hình server.xml của Tomcat (thường nằm trong thư mục tomcat/conf) và tìm các đoạn cấu hình cho các port 80 và 443.
Trong phần cấu hình port 80 chỉnh lại như bên dưới:

<Connector port="443" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" disableUploadTimeout="true" acceptCount="100" scheme="https" secure="true" SSLEnabled="true" clientAuth="false" sslProtocol="TLS" keyAlias="tomcat" keystoreFile="/usr/local/ssl/keystore/server.jks" keystorePass="your_keystore_password" />

<Connector port="80" enableLookups="false" redirectPort="443" />
Tiếp theo đến file cấu hình web.xml của website và thêm vào đoạn sau đây ở cuối file, nhưng trước thẻ </web-app>
<!-- SSL settings. Only allow HTTPS access -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Sau đó bạn cần khởi động lại Tomcat.

4.Cấu hình trên IIS

Đầu tiên chúng ta khởi động Internet Information Services (IIS) Manager, tìm đến website cần cấu hình. Trong khung HOME của website bạn >>> double click SSL Settings >>> bật Client Certificates = Accept >>> Apply.

Lưu ý: không chọn cấu hình Require SSL
Tiếp theo là cài đặt module URL Rewrite 2.0, bạn có thể download trực tiếp module này từ các link mà Microsoft cung cấp như sau :
- Phiên bản 64 bit cho IIS8
- Phiên bản 32 bit cho IIS8
- Phiên bản 64 bit cho IIS7
- Phiên bản 32 bit cho IIS7

* Cấu hình trên IIS8.x
Sau khi cài đặt thành công cũng trong khung HOME của website cần cấu hình bạn double click URL Rewrite >>> Add Rule >>> Inbound Rule >>> Blank Rule >>> cấu hình như sau:
NAME : http to https

MATCH URL :
– Requested URL: Matches the Pattern
– Using: Regular Expression
– Pattern: (.*)

CONDITIONS >>> Add
– Condition Input: {HTTPS}
– Check if input string: Matches the Pattern
– Pattern: ^OFF$
– Nhấn OK

ACTION
– Action type: Redirect
– Redirect URL: https://{HTTP_HOST}/{R:1}
– Redirect type: See Other (303)
– Nhấn Apply

 

Bạn đang quan tâm tới dịch vụ của chúng tôi? Hãy chia sẻ mong muốn của bạn để chúng tôi phục vụ bạn!