第一種方式,分兩種情況:
第一種情況:修改Nginx安裝目錄/conf/nginx.conf文件
server {
listen 80;
server_name localhost; #將localhost修改為您證書綁定的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent; #將所有http請求通過rewrite重定向到https。
location / {
index index.html index.htm;
}
}
# 以下屬性中以ssl開頭的屬性代表與證書配置有關,其他屬性請根據自己的需要進行配置。
server {
listen 443 ssl; #SSL協議訪問端口號為443。此處如未添加ssl,可能會造成Nginx無法啟動。
server_name localhost; #將localhost修改為您證書綁定的域名,例如:www.example.com。
root html;
index index.html index.htm;
ssl_certificate cert/domain name.pem; #將domain name.pem替換成您證書的文件名。
ssl_certificate_key cert/domain name.key; #將domain name.key替換成您證書的密鑰文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用該協議進行配置。
ssl_prefer_server_ciphers on;
location / {
root html; #站點目錄。
index index.html index.htm;
}
}
第二種情況:虛擬主機配置SSL證書,虛擬主機配置文件vhost.conf或*.conf
server {
listen 80;
server_name localhost ;
location / {
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name localhost;
root html;
index index.html index.htm;
ssl_certificate cert/domain name.pem; #將domain name.pem替換成您證書的文件名。
ssl_certificate_key cert/domain name.key; #將domain name.key替換成您證書的密鑰文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
index index.html index.htm;
}
}
在Web目錄下打開.htaccess
文件(如沒有,需新建該文件),添加以下rewrite語句,實現HTTP訪問自動跳轉到HTTPS頁面。
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$ [NC] # 將yourdomain.com修改為您證書綁定的域名,例如:example.com。
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L] # 將yourdomain.com修改為您證書綁定的域名,例如:example.com。
第二種方式
server {
listen 80;
server_name demo.jumpserver.org; # 自行修改成你的域名
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name demo.jumpserver.org; # 自行修改成你的域名
ssl_certificate /etc/nginx/sslkey/1_jumpserver.org_bundle.crt; # 自行設置證書
ssl_certificate_key /etc/nginx/sslkey/2_jumpserver.org.key; # 自行設置證書
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # 自行替換成你證書支持的加密套件
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 支持的協議
ssl_prefer_server_ciphers on;
location / {
index index.html index.htm;
}
}