Nginx+Node.js+HTTPS+HTTP2布署

发布:elantion 日期:2018-07-11 阅读:2248 评论:0

之前每配置一次服务器都要问谷歌,好烦。好记性不如烂笔头,还是把流程记下来吧,免得下次又到处找。如果有服务器有什么功能更新,我也会在这里更新。

安装nginx

由于过程比较杂,按主线流程来操作就行,参考以下脚本:

#!/bin/bash
# config
nginx_url="https://nginx.org/download/nginx-1.13.1.tar.gz"
openssl_url="https://www.openssl.org/source/openssl-1.1.0f.tar.gz"
nginx_config="--with-openssl=../openssl --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module"
cache_path="/tmp/install_nginx_cache"

# real job
echo "install nginx script start..."
echo "download ssh..." 
mkdir -p ${cache_path}
cd ${cache_path}
wget -c ${openssl_url} -O openssl.tar.gz
tar -zxf openssl.tar.gz
echo "download nginx..."
wget -c ${nginx_url} -O nginx.tar.gz
tar -zxf nginx.tar.gz
cd nginx
echo "compile nginx..."
./configure ${nginx_config}
make
make install

Nginx网站配置

这是我当前网站的配置参数,/usr/local/nginx/conf/conf.d/lazycoffee.conf

server {
    listen 443 ssl http2;
    server_name www.lazycoffee.com;
    ssl_certificate /path/to/your/xxx.pem;
    ssl_certificate_key /path/to/your/xxx.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    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;
    # 请根据实际情况配置log信息
    # access_log logs/www.lazycoffee.com.log main;
    # error_log logs/www.lazycoffee.com.err.log info;
    root /home/elantion/mentry2/public;
    location / {
        try_files uri $uri @node_server;
    }
    location @node_server {
        # nginx使用http1.1协议跟node.js传输
        proxy_http_version 1.1;
        # 禁止你的网站被别的网站利用iframe嵌套
        add_header    X-Frame-Options deny;
        # 禁止浏览器检查内容的类型,坚持使用头部的content-type来执行处理内容
        add_header    X-Content-Type-Options nosniff;
        # 缓存控制
        add_header    Cache-Control no-cache;
        # 隐藏框架显示的头部信息
        proxy_hide_header        Vary;
        proxy_hide_header        X-Powered-By;
        # nginx转发客户端真实的host给node.js
        proxy_set_header          Host      $host;
        # nginx转发客户端真实的ip给node.js
        proxy_set_header          X-Real-IP $remote_addr;
        # nginx转发真实的ip给node.js
        proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
        # x-forwarded-proto用于转发真实的协议
        # 如果cookie设置了secure值为true,那么就需要添加下面头部信息。
        proxy_set_header          X-Forwarded-Proto $scheme;
        # nginx转发客户端请求给node.js
        proxy_pass http://localhost:3000;
    }
}

Node.js配置

由于所有的请求都自nginx的转发,所以node.js要设置“相信proxy”参数。

app.enable('trust proxy'); // 相信nginx转发的cookie
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
    secret: 'Super Secret Password',
    proxy: true, // 重要
    key: 'session.sid',
    cookie: {secure: true}, //https要设置secure为true
    //请不要使用下面这种内存式session,这里只是举例。
   store: new sessionStore() 
}));

gzip配置

http传输压缩,网站必备,/usr/local/nginx/conf/conf.d/gzip.conf

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

Nginx全局配置

有些地方需要手动修改,/usr/local/nginx/conf/nginx.conf

# nginx运行的用户
user lazycoffee lazycoffee;
# ...

http {
    # 引进gzip配置
    include conf.d/gzip.conf;
    # 引进lazycoffee网站配置
    include conf.d/lazycoffee.conf;
    # 所有lazycoffee.com跳转到www.lazycoffee.com
    # 不显示nginx的版本
    server_tokens off;
    # 更快的发送静态文件
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    tcp_nodelay on;
    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name lazycoffee.com;
        return 301 https://www.lazycoffee.com$request_uri;
    }
    # 所有80端口的访问都跳到https://www.lazycoffee.com
    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        return 301 https://www.lazycoffee.com$request_uri;
    }
}