Centos7.5 下Nginx配置SSL支援https訪問。
核心配置:
• 通過指定由受信任的證書頒發機構(CA)頒發的有效證書,將伺服器配置為偵聽埠上的HTTPS流量。
• 通過配置nginx.conf檔案來加強安全性。示例包括選擇更強大的密碼,並將所有流量通過HTTP重定向到HTTPS。
• 新增HTTP Strict-Transport-Security(HSTS)頭部確保客戶端所做的所有後續請求僅通過HTTPS。
#################################################################################
nginx配置ssl,需要確保nginx包含相應的模組--with-http_ssl_module
檢視nginx安裝引數是否已經包含此模組,如果未包含請先安裝此模組。
[[email protected] ~]# nginx -V
新增/etc/nginx/proxy.conf配置檔案:
[[email protected] ~]# vi etc/nginx/proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
編輯/etc/nginx/nginx.conf配置檔案。配置主要包含兩個部分http和server。
vi /etc/nginx/nginx.conf
http {
include /etc/nginx/proxy.conf;
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
server_tokens off;
sendfile on; keepalive_timeout 29; # Adjust to the lowest possible value that makes sense for your use case. client_body_timeout 10; client_header_timeout 10; send_timeout 10; upstream hellomvc{ server localhost:5000; } server { listen *:80; add_header Strict-Transport-Security max-age=15768000; return 301 https://$host$request_uri; } server { listen *:443 ssl; server_name example.com; ssl_certificate /etc/ssl/certs/testCert.crt; ssl_certificate_key /etc/ssl/certs/testCert.key; ssl_protocols TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; ssl_ecdh_curve secp384r1; ssl_session_cache shared:SSL:10m; ssl_session_tickets off; ssl_stapling on; #ensure your cert is capable ssl_stapling_verify on; #ensure your cert is capable add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; #Redirects all traffic location / { proxy_pass http://hellomvc; limit_req zone=one burst=10; } }
}
有幾處需要更改
• server_name改成你的域名=申請證書繫結的域名。
• ssl_certificate改成你的公鑰路徑。
• ssl_certificate_key改成你的私鑰路徑。
一些安全配置
防止Clickjacking(點選劫持),Clickjacking是一種惡意技術來收集受感染的使用者的點選。劫持受害者(訪問者)點選感染的網站,使用X-FRAME-OPTIONS。
編輯nginx.conf檔案:
[[email protected] ~]# vi /etc/nginx/nginx.conf
將配置中的
add_header X-Frame-Options DENY;
更改為
add_header X-Frame-Options SAMEORIGIN;
重新載入nginx
[[email protected] ~]# service nginx reload
MIME型別的嗅探,此標頭防止大多數瀏覽器從宣告的內容型別中嗅探響應,因為標頭指示瀏覽器不要覆蓋響應內容型別,使用nosniff選項
編輯nginx.conf檔案:
[[email protected] ~]# vi /etc/nginx/nginx.conf
新增行
add_header X-Content-Type-Options nosniff;
上文的nginx.conf已配置好此標頭,儲存檔案,重新啟動Nginx。