Nginx SSL配置/优化方案(转)

Nginx SSL配置/优化方案(转)

Vanish
2016-08-14 / 0 评论 / 621 阅读 / 正在检测是否收录...

Nginx SSL配置/优化方案 ,晚上一个群里的朋友分享的很详细,我就转到博客留备用。
该配置方案是本站自用的配置方案,在SSL Labs测试中可以获得A+的最佳评级。
Nginx站点配置文件一般位置:/usr/local/nginx/conf/vhost

server {
listen 443 ssl http2;

#开启HTTP严格安全传输(HSTS),非已经决定长期使用SSL的站长慎用!
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

#SSL证书文件位置
ssl_certificate /root/SSL/ECC/Seryo.net.crt;
ssl_certificate_key /root/SSL/ECC/Seryo.net.key;

#SSL优化配置
ssl_session_timeout 10m;    #SSL session过期时间
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #只允许TLS协议
ssl_prefer_server_ciphers on;    #由服务器协商最佳的加密算法
ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:AES128+EECDH:AES128+EDH:EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;    #OCPS开启
ssl_stapling_verify on;    #OCPS验证开启
resolver 8.8.8.8 8.8.4.4 valid=300s;    #用于查询OCPS服务器的DNS
resolver_timeout 5s;

#一般站点配置
server_name seryo.net www.seryo.net;
access_log /data/wwwlogs/seryo.net_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/typecho.conf;
root /data/wwwroot/seryo.net;

#重定向www到根域名
if ($host != seryo.net) {
    rewrite ^/(.*)$ $scheme://seryo.net/$1 permanent;
    }

location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}
server {
listen 80;
server_name seryo.net;

#将HTTP请求重写到HTTPS
rewrite ^/(.*) https://$server_name/$1 permanent;
}

适用于一般RSA证书的Nginx SSL优化配置
之前的文章中已经提供了一份拥有较强安全性的SSL配置,但是如果你不想更换证书也不希望太过复杂的配置,
则只需要更新一下两项,就可以获得足够高的安全性

一般网站评级较低的原因都是这两个:

1、使用的证书密钥交换密钥过弱

2、使用过时且不安全的加密算法:RC4

那么我们就将解决这两个问题

证书密钥交换密钥过弱
一般网站使用的SSL证书都是RSA证书,这种证书基本都是2048位的密钥,但是证书密钥交换密钥必须要比证书密钥更长才能安全,
而默认的只有1024位,所以我们需要手动生成一个更强的密钥。

安装screen

(防止密钥生成过程中SSH连接中断)
CentOS/RHEL

yum -y install screen

Debian/Ubuntu

apt-get -y install screen

生成4096位的DH-Key(证书密钥交换密钥)

最好和SSL证书放在一起,方便管理

screen -S DH

openssl dhparam -out dhparam.pem 4096

然后就是漫长的等待。。。

在Nginx中配置DH-Key

在你的站点配置文件中(一般在/usr/local/nginx/conf/vhost),SSL证书位置的下方加入一行:

ssl_dhparam /文件位置/dhparam.pem;

将文件位置替换为你的DH-Key所在目录的【绝对路径】即可。

解决密钥套件过弱

在你的站点配置文件中(一般在/usr/local/nginx/conf/vhost)中SSL配置信息中加入(如果有则直接替换):

ssl_prefer_server_ciphers on;    #由服务器协商最佳的加密算法
ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:AES128+EECDH:AES128+EDH:EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_session_cache builtin:1000 shared:SSL:10m;    #使用了CHACHA20算法,能够提升网站在移动设备上的打开速度,需要在Nginx中编译LibreSSL库,你也可以使用Seryo Network提供的Nginx一键编译脚本。

注意:如果你的Nginx不支持CHACHA20算法,则删除ECDHE-ECDSA-CHACHA20-POLY1305:即可。 让你的Nginx支持最潮流最快捷的CHACHA20算法:

Nginx一键更新脚本 这个也是本站自用的Nginx编译配置,采用最新的Nginx1.9.9版本,增加了LibreSSL的支持(可以使用CHACHA20算法),并且添加了一些有用的模块,具体可以在下方找到说明。 采用了的编译参数:

./configure \
 --prefix=/usr/local/nginx \
 --user=www \
 --group=www \
 --http-client-body-temp-path=/var/tmp/nginx/client \
 --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
 --http-proxy-temp-path=/var/tmp/nginx/proxy \
 --http-scgi-temp-path=/var/tmp/nginx/scgi \
 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
 --with-http_ssl_module \
 --with-http_gzip_static_module \
 --with-ipv6 \
 --with-http_sub_module \
 --with-http_stub_status_module \
 --with-http_v2_module \
 --with-http_flv_module \
 --with-http_realip_module \
 --with-ld-opt="-ljemalloc -lrt" \
 --add-module=ngx_http_google_filter_module \
 --add-module=ngx_http_substitutions_filter_module \
 --with-openssl=libressl-${LIBRESSL_VERSION} \
 --with-http_gunzip_module
使用方法:

1、下载脚本

wget -P /root https://www.seryo.moe/nginx.sh

2、赋予执行权限

chmod +x nginx.sh

3、运行,然后等待安装

yum -y install screen && screen -S nginx && ./nginx.sh

双核CPU的服务器请下载以下版本:

wget -P /root https://www.seryo.moe/nginx_core2.sh
以上内容来源: https://seryo.net/7.Seryo https://seryo.net/17.Seryo https://seryo.net/8.Seryo https://wiki.phoenixlzx.com/page/NGINX/ https://doc.ssl.do/page/install-nginx/ https://www.xiaofengsky.com/82658.html
0

评论

博主关闭了所有页面的评论