#SSL配置
server {
listen 80;
listen 443 ssl http2;
#ssl on; 80和443共存的时候不要开启
ssl_certificate /data/cert/xxx.com.pem; #证书
ssl_certificate_key /data/cert/xxx.com.key; #公钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000; #HSTS 强制https访问,子域名加includeSubDomains
ssl_stapling on;
ssl_stapling_verify on;
server_name www.xxx.com;
access_log /data/wwwlogs/www.xxx.com_nginx.log combined;
index index.html index.htm index.php;
root /data/wwwroot/xxx.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
#下面配置略
nginx如何配置HSTS
add_header Strict-Transport-Security "max-age=15768000";
#这样当第一次以https方式访问我的网站,nginx则会告知客户端的浏览器,以后即便地址栏输入http,也要浏览器改成https来访问我的nginx服务器
添加配置避免被嵌套
add_header X-Frame-Options "DENY";
#避免点击劫持,还要添加 X-Frame-Options 头部,确保不会嵌入到frame 或 iframe,使得网站的内容不会嵌入到其他网站
获取不到$_SERVER[‘HTTPS’]值的问题
proxy_set_header X-Forwarded-Proto $scheme;
获取不到真实IP
- set_real_ip_from 是指接受从哪个信任前置代理处获得真实用户ip
- real_ip_header 是指从接收到报文的哪个http首部去获取前代理传送的用户ip
- real_ip_recursive 是否递归地排除直至得到用户ip(默认为 off)
示例: 从 proxy_protocol 获取
set_real_ip_from 192.168.1.0/24;
real_ip_header proxy_protocol;