之前使用apache,懶得去看文檔了咧叭,重新弄個(gè)nginx來(lái)搞蚀乔。nginx的server配置很是簡(jiǎn)單,因?yàn)閏onf里面自帶了https server的配置模板菲茬。吉挣。。
免費(fèi)CA選擇的是https://letsencrypt.org/婉弹。
獲取兩個(gè)必要的東西: 證書(shū) 和 私鑰 也十分簡(jiǎn)單睬魂,網(wǎng)站有操作就不細(xì)說(shuō)了。
獲取到這兩樣?xùn)|西镀赌,在nginx的conf里面配置二者的路徑即可氯哮。一般在/etc/letsencrypt的live中。
主要是網(wǎng)站訪(fǎng)問(wèn)時(shí)從HTTP轉(zhuǎn)HTTPS的技巧:nginx監(jiān)聽(tīng)80端口肯定有個(gè)默認(rèn)主頁(yè)佩脊,只要在默認(rèn)主頁(yè)里面重定向即可:
index.html
<html>
<meta http-equiv="refresh" content="0;url=https://test.com/">
</html>
這里有個(gè)疑問(wèn)蛙粘,假設(shè)直接訪(fǎng)問(wèn)某個(gè)頁(yè)面,這樣的機(jī)制就不會(huì)有效果了威彰,如何做呢?
rewrite是個(gè)選擇:
即在conf中http的server配置中添加
rewrite ^(.*)$ https://$host$1 permanent;
將所有80端口的請(qǐng)求都rewrite到https上面穴肘。
還遇到一個(gè)問(wèn)題:
nginx的server root目錄下面的子目錄無(wú)法訪(fǎng)問(wèn)的問(wèn)題歇盼。
權(quán)限配置正確,死活找不到原因评抚,后來(lái)看到一個(gè)nginx的配置:
在conf中寫(xiě):
autoindex on;
即可豹缀。。慨代。
現(xiàn)在全站即開(kāi)啟HTTPS了邢笙。
另外就是當(dāng)前的nginx配置,作為以后參考:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
autoindex on;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
rewrite ^(.*)$ https://$host$1 permanent;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 https://kyrray.tech;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name kyrray.tech;
root /usr/share/nginx/html;
ssl_certificate "/etc/letsencrypt/live/kyrray.tech/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/kyrray.tech/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}