nginx配置https协议

来源:互联网 发布:美发教程软件下载 编辑:程序博客网 时间:2024/05/25 05:35

配置方法如下,需要注意的地方是即使有一个listen 80了,还是需要加一个listen 443的配置项,在server块里面添加如下配置即可(我用的腾讯云的com.cn域名)

listen 443 default ssl;server_name  www.yourDomain.com.cn;ssl on;ssl_certificate /etc/nginx/conf.d/1_www.yourDomain.com.cn_bundle.crt;ssl_certificate_key /etc/nginx/conf.d/2_www.yourDomain.com.cn.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;

如果一个nginx服务器中有多个项目,比如我有一个thinkPHP项目和一个phpMyadmin,分别配置了两个server,那么这两个server都要进行https的配置,但是要注意的是只能有一个listen 443 default ssl的代码,具体配置如下:

1.default.conf

#配置共享会话缓存大小,视站点访问情况设定ssl_session_cache   shared:SSL:10m;#配置会话超时时间ssl_session_timeout 10m;server {    listen       80;    listen 443 default ssl;    server_name  www.xiaoxiaohei.com.cn;    error_page 497  https://$host$uri?$args; #http重定向到https    #设置长连接    keepalive_timeout   70;     #HSTS策略    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;    ssl on;    ssl_certificate /etc/nginx/conf.d/1_www.xiaoxiaohei.com.cn_bundle.crt;    ssl_certificate_key /etc/nginx/conf.d/2_www.xiaoxiaohei.com.cn.key;    ssl_session_timeout 5m;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;    ssl_prefer_server_ciphers on;    #使用DH文件    ssl_dhparam /etc/ssl/certs/dhparam.pem;    #减少点击劫持    add_header X-Frame-Options DENY;    #禁止服务器自动解析资源类型    add_header X-Content-Type-Options nosniff;    #防XSS攻击    add_header X-Xss-Protection 1;    #charset koi8-r;    #access_log  /var/log/nginx/log/host.access.log  main;    location / {        #root   /usr/share/nginx/html;        root   /data/web/webapp/public;        index  index.html index.htm index.php;        if (!-e $request_filename) {            rewrite  ^(.*)$  /index.php?s=/$1  last;            break;        }    }    #error_page  404              /404.html;    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        #root   /usr/share/nginx/html;        root   /data/web/webapp/public;    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80    #    #location ~ \.php$ {    #    proxy_pass   http://127.0.0.1;    #}    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    #    location ~ \.php$ {        #root           /usr/share/nginx/html;        root   /data/web/webapp/public;        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;        include        fastcgi_params;    }    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ /\.ht {    #    deny  all;    #}}

2.php-myadmin.conf

server {    listen       7170;    server_name  www.xiaoxiaohei.com.cn;    #设置长连接    keepalive_timeout   70;     #HSTS策略    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;    ssl on;    ssl_certificate /etc/nginx/conf.d/1_www.xiaoxiaohei.com.cn_bundle.crt;    ssl_certificate_key /etc/nginx/conf.d/2_www.xiaoxiaohei.com.cn.key;    ssl_session_timeout 5m;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;    ssl_prefer_server_ciphers on;    #使用DH文件    ssl_dhparam /etc/ssl/certs/dhparam.pem;    #减少点击劫持    add_header X-Frame-Options DENY;    #禁止服务器自动解析资源类型    add_header X-Content-Type-Options nosniff;    #防XSS攻击    add_header X-Xss-Protection 1;    #charset koi8-r;    #access_log  /var/log/nginx/log/host.access.log  main;    location / {        #root   /usr/share/nginx/html;    root   /data/web/phpMyAdmin;        index  index.html index.htm index.php;    }    #error_page  404              /404.html;    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        #root   /usr/share/nginx/html;    root   /data/web/phpMyAdmin;    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80    #    #location ~ \.php$ {    #    proxy_pass   http://127.0.0.1;    #}    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    #    location ~ \.php$ {        #root           /usr/share/nginx/html;    root   /data/web/phpMyAdmin;        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;        include        fastcgi_params;    }    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ /\.ht {    #    deny  all;    #}}

详细配置解析配置详情

基本配置如下:

server {    #ssl参数    listen              443 ssl;    server_name         example.com;    #证书文件    ssl_certificate     example.com.crt;    #私钥文件    ssl_certificate_key example.com.key;    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers         HIGH:!aNULL:!MD5;    #...}

证书文件会作为公用实体發送到每台连接到服务器的客戶端,私钥文件作为安全实体,应该被存放在具有一定权限限制的目录文件,并保证 Nginx 主进程有存取权限。

私钥文件也有可能会和证书文件同放在一個文件中,如下面情況:

ssl_certificate     www.example.com.cert;ssl_certificate_key www.example.com.cert;

这种情況下,证书文件的的读取权限也应该加以限制,仅管证书和私钥存放在同一个文件里,但是只有证书会被发送到客戶端

命令 ssl_protocols 和 ssl_ciphers 可以用来限制连接只包含 SSL/TLS 的加強版本和算法,默认值如下:

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers HIGH:!aNULL:!MD5;

由于这两个命令的默认值已经好几次发生了改变,因此不建议显性定义,除非有需要额外定义的值,如定义 D-H 算法:

#使用DH文件ssl_dhparam /etc/ssl/certs/dhparam.pem;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#定义算法ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";#...

HTTPS服务器优化

减少 CPU 运算量

SSL 的运行计算需要消耗额外的 CPU 资源,一般多核处理器系统会运行多个工作进程(worker processes ),进程的数量不会少于可用的 CPU 核数。SSL 通讯过程中『握手』阶段的运算最占用 CPU 资源,有两个方法可以减少每台客户端的运算量:

激活 keepalive 长连接,一个连接发送更多个请求
复用 SSL 会话参数,在并行并发的连接数中避免进行多次 SSL『握手』
这些会话会存储在一个 SSL 会话缓存里面,通过命令 ssl_session_cache 配置,可以使缓存在机器间共享,然后利用客戶端在『握手』阶段使用的 seesion id 去查询服务端的 session cathe(如果服务端设置有的话),简化『握手』阶段。

1M 的会话缓存大概包含 4000 個会话,默认的缓存超时时间为 5 分钟,可以通过使用 ssl_session_timeout 命令设置缓存超时时间。下面是一個拥有 10M 共享会话缓存的多核系统优化配置例子:

worker_processes auto;http {    #配置共享会话缓存大小    ssl_session_cache   shared:SSL:10m;    #配置会话超时时间    ssl_session_timeout 10m;    server {        listen              443 ssl;        server_name         www.example.com;        #设置长连接        keepalive_timeout   70;        ssl_certificate     www.example.com.crt;        ssl_certificate_key www.example.com.key;        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;        ssl_ciphers         HIGH:!aNULL:!MD5;        #...

使用 HSTS 策略强制浏览器使用 HTTPS 连接

HSTS – HTTP Strict Transport Security,HTTP严格传输安全。它允许一个 HTTPS 网站要求浏览器总是通过 HTTPS 来访问,这使得攻击者在用戶与服务器通讯过程中拦截、篡改信息以及冒充身份变得更为困难。

只要在 Nginx 配置文件加上以下头信息就可以了:

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;preload" always;

max-age:设置单位时间内強制使用 HTTPS 连接
includeSubDomains:可选,所有子域同时生效
preload:可选,非规范值,用于定义使用『HSTS 预加载列表』
always:可选,保证所有响应都发送此响应头,包括各种內置错误响应

加强 HTTPS 安全性

HTTPS 基础配置采取的默认加密算法是 SHA-1,这个算法非常脆弱,安全性在逐年降低,在 2014 年的时候, Google 官方博客就宣布在 Chrome 浏览器中逐渐降低 SHA-1 证书的安全指示,会从 2015 年起使用 SHA-2 签名的证书,可参阅 Rabbit_Run 在 2014 年发表的文章:《为什么Google急着杀死加密算法SHA-1》

为此,主流的 HTTPS 配置方案应该避免 SHA-1,可以使用 迪菲-赫尔曼密钥交换(D-H,Diffie–Hellman key exchange)方案。

首先在目录 /etc/ssl/certs 运行以下代码生成 dhparam.pem 文件:

openssl dhparam -out dhparam.pem 2048

然后加入 Nginx 配置:

#优先采取服务器算法ssl_prefer_server_ciphers on;#使用DH文件ssl_dhparam /etc/ssl/certs/dhparam.pem;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#定义算法ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";

如果服务器夠強大,可以使用更为复杂的 4096 位进行加密。

一般情況下还应该加上以下几个增强安全性的命令:

#减少点击劫持add_header X-Frame-Options DENY;#禁止服务器自动解析资源类型add_header X-Content-Type-Options nosniff;#防XSS攻击add_header X-Xss-Protection 1;

优化后的综合配置

worker_processes auto;http {    #配置共享会话缓存大小,视站点访问情况设定    ssl_session_cache   shared:SSL:10m;    #配置会话超时时间    ssl_session_timeout 10m;    server {        listen              443 ssl;        server_name         www.example.com;        #设置长连接        keepalive_timeout   70;        #HSTS策略        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;        #证书文件        ssl_certificate     www.example.com.crt;        #私钥文件        ssl_certificate_key www.example.com.key;         #优先采取服务器算法        ssl_prefer_server_ciphers on;        #使用DH文件        ssl_dhparam /etc/ssl/certs/dhparam.pem;        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;        #定义算法        ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";        #减少点击劫持        add_header X-Frame-Options DENY;        #禁止服务器自动解析资源类型        add_header X-Content-Type-Options nosniff;        #防XSS攻擊        add_header X-Xss-Protection 1;        #...
0 0
原创粉丝点击