Git Product home page Git Product logo

nginxconfig-1's Introduction

sslPatch.patch 是一个用于为OpenSSL 1.0.2l版本添加ChaCha20-Poly1305, Camellia-GCM, X25519曲线以及硬编码的等价加密组的Patch.


如何编译:
1.安装依赖
我的系统是 Ubuntu 16.04.1 LTS,如果你使用的是其它发行版,与包管理有关的命令请自行调整。
首先安装依赖库和编译要用到的工具:

sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev unzip git autoconf libtool automake

2.获取必要组件
  2.1nginx-ct  nginx-ct 模块用于启用 Certificate Transparency 功能。直接从 github 上获取源码:
  #bash
  wget -O nginx-ct.zip -c https://github.com/grahamedgecombe/nginx-ct/archive/v1.3.2.zip
  unzip nginx-ct.zip

 2.2ngx_brotli
  本站支持 Google 开发的 Brotli 压缩格式,它通过内置分析大量网页得出的字典,实现了更高的压缩比率,同时几乎不影响压缩 / 解压速度。
  以下是让 Nginx 支持 Brotli 所需准备工作,这些工作是一次性的。首先安装 libbrotli:

  git clone https://github.com/bagder/libbrotli
  cd libbrotli
  # 如果提示 error: C source seen but 'CC' is undefined,可以在 configure.ac 最后加上 AC_PROG_CC
  ./autogen.sh
  ./configure
  make
  sudo make install
  cd  ../
  默认 libbrotli 装在 /usr/local/lib/libbrotlienc.so.1,如果后续启动 Nginx 时提示找不到这个文件,那么可以把它软链到 /lib 或者 /usr/lib 目录。
  # 64 位系统
 ln -s /usr/local/lib/libbrotlienc.so.1 /lib64

 # 32 位系统
 ln -s /usr/local/lib/libbrotlienc.so.1 /lib
  
  接下来获取 ngx_brotli 源码:
  #Bash

  git clone https://github.com/google/ngx_brotli.git
  cd ngx_brotli
  git submodule update --init
  cd ../
3.补丁
本站使用了ChaCha20-Poly1305 for OpenSSL (ALLPATCH.patch),以及 Cloudflare的Dynamic TLS Records for Nginx, Nginx http2 spdy, Nginx http2 hpack补丁。
先来获取补丁文件:

git clone https://github.com/HamJin/nginxconfig.git
git clone https://github.com/cloudflare/sslconfig.git

OpenSSL
由于系统自带的 OpenSSL 库往往不够新,推荐在编译 Nginx 时指定 OpenSSL 源码目录,而不是使用系统自带的版本,这样更可控。
本站目前使用 OpenSSL 1.0.2l:
#bash

wget -O openssl.tar.gz -c https://github.com/openssl/openssl/archive/OpenSSL_1_0_2l.tar.gz
tar zxf openssl.tar.gz
mv openssl-OpenSSL_1_0_2l/ openssl

打上 ChaCha20/Poly1305 补丁:

#BASH
cd openssl
patch -p1 < ../nginxconfig/ALLPATCH.patch 
cd ../

编译并安装 Nginx
接着就可以获取 Nginx 源码,并打上 Dynamic TLS Records 补丁:
#BASH
wget -c https://nginx.org/download/nginx-1.13.1.tar.gz
tar zxf nginx-1.13.1.tar.gz

cd nginx-1.13.1/
patch -p1 < ../sslconfig/patches/nginx__1.11.5_dynamic_tls_records.patch
patch -p1 < ../sslconfig/patches/nginx_1.13.1_http2_hpack.patch
patch -p1 < ../sslconfig/patches/nginx__1.13.0_http2_spdy.patch
cd ../

编译和安装:
#BASH
cd nginx-1.13.1/
./configure --add-module=../ngx_brotli --add-module=../nginx-ct-1.3.2 --with-openssl=../openssl --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_spdy_module --with-http_v2_hpack_enc
make
sudo make install

除了 http_v2 http_spdy http_v2_hpack_enc和 http_ssl 这两个 HTTP/2 必备模块之外,我还额外启用了 http_gzip_static,需要启用哪些模块需要根据自己实际情况来决定(注:从 Nginx 1.11.5 开始,ipv6 模块已经内置,故 --with-ipv6 配置项已被移除)。
以上步骤会把 Nginx 装到 /usr/local/nginx/ 目录,如需更改路径可以在 configure 时指定。
管理脚本与自启动
为了方便管理 Nginx 服务,再创建一个管理脚本:

sudo nano /etc/init.d/nginx

输入以下内容:
--

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then

. /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
    echo -n "Reloading $DESC configuration: "
    start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  status)
    status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

快捷键Ctrl+X退出,提示保存时键入y。
增加执行权限:

sudo chmod a+x /etc/init.d/nginx

现在管理 Nginx 只需使用以下命令即可:

sudo service nginx start|stop|restart|reload

如果要开机自动启动 Nginx,请执行以下命令:

sudo update-rc.d -f nginx defaults

Nginx 全局配置
到此为止,Nginx 已经安装完毕。再来修改一下它的全局配置,打开 /usr/local/nginx/conf/nginx.conf,新增或修改https://github.com/HamJin/nginxconfig/nginx.conf中内容.

要让网站支持浏览器通过 HTTP/2 访问必须先部署 HTTPS,要部署 HTTPS 必须先有合法的证书。本博客目前在用 RapidSSL 单域名证书,在 NameCheap 购买。另外,我还申请了 Let's Encrypt 的免费证书备用。一般情况下,个人使用 Let's Encrypt 的免费证书就足够了,还可以节省一笔开销。
要申请 Let's Encrypt 证书,推荐使用 Neilpang/acme.sh 这个小巧无依赖的命令行工具,
注:Let's Encrypt 已于 2016 年 3 月 26 日修复 Windows XP 下的兼容问题,2018年一月计划支持通配符证书,2017年9月1日之前计划ECDSA根证书


WEB 站点配置
以下是本博客站点完整配置:

日志自动切分
上一节中,我在 Nginx 的站点配置中通过 access_log 指定了访问日志的存放位置。Nginx 启动后,会持续往这个文件写入访问日志。如果网站访问量很大,最好能按照指定大小或者时间间隔切分日志,便于后期管理和排查问题。
虽然本站访问量不大,但我也使用了 logrotate 工具对访问日志进行了按天切分。
大多数 Linux 发行版都内置了 logrotate,只需新建一个配置文件即可,例如:
BASH
sudo vim /etc/logrotate.d/nginx

/home/jerry/www/nginx_log/*.log {
    su root root
    daily
    rotate 5
    missingok
    notifempty
    sharedscripts
    dateext
    postrotate
        if [ -f /usr/local/nginx/logs/nginx.pid ]; then
            kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
        fi
    endscript
}
配置中具体指令的含义可以查看手册。配置好之后,可以手动执行一下,看是否正常:
sudo /usr/sbin/logrotate -f /etc/logrotate.d/nginx
如果一切无误,后续 Nginx 的访问日志就会自动按天切分,并以年月日做为文件后缀,一目了然。
在我的 Ubuntu 16.04.1 LTS 上,/etc/logrotate.d/ 目录中的日志切分任务会由 /etc/cron.daily/logrotate 来确保每天执行一次。查看 /etc/crontab 会发现 cron.daily 任务会在每天 6:25 执行,这就是 logrotate 每天切分日志的时机。
如果想要让日志正好在零点被切分,可以修改 cron.daily 的执行时机,也可以把自己的 logrotate 配置文件放在 /etc/logrotate.d/ 之外,再手动配置 crontab 规则。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.