可搜索,可注册,可登录,致敬逗比大佬!尽在救援版逗比根据地 dbgjd.com
投稿文章 | 广告合作 | Telegram 群组 / 公告频道 / 使用教程

本博客 Nginx 配置(系列三) 完整篇

News mlone11978 473℃ 0评论

这是本博客 Nginx 配置系列的第三篇了,在前面的两篇中我分别介绍了安全和性能的相关内容,这篇文章会对之前我所作的那些文章(所作的那些铺垫)做一个集合,以本站的配置为示例进行说明。

本文最后更新: 2018-03-24

编译安装

# 下载所需 apt-get install -y git  # pcre 8.42 # 更新至最新版 pcre wget https://raw.githubusercontent.com/nanqinlang/Nginx/master/dependence/pcre-8.42.tar.gz && tar -zxf pcre-8.42.tar.gz cd pcre-8.42 && ./configure && cd ..  # zlib-cloudflare # 采用 cloudflare 维护的 zlib git clone --depth=1 https://github.com/nanqinlang-fork-blog-nginx/zlib.git zlib-cloudflare cd zlib-cloudflare && ./configure && cd ..  # 本站已启用 TLS 1.3,所以采用 openssl-draft-18 分支 # git clone -b tls1.3-draft-18 https://github.com/nanqinlang-fork/openssl.git openssl-draft-18 # 更新:本站不再使用 openssl 并不再启用 TLS1.3 # 本站已改用 LibreSSL 作为加密库 wget https://raw.githubusercontent.com/nanqinlang/Nginx/master/dependence/libressl-2.6.4.tar.gz && tar -zxf libressl-2.6.4.tar.gz  # 本站使用 Nginx mainline v1.13.9 wget https://nginx.org/download/nginx-1.13.9.tar.gz && tar -zxf nginx-1.13.9.tar.gz  # Nginx brotli module git clone https://github.com/nanqinlang-fork-blog-nginx/ngx_brotli.git && cd ngx_brotli && git submodule update --init && cd ..  # Nginx CT module # wget https://raw.githubusercontent.com/nanqinlang/Nginx/master/module/ct-module-1.3.2.tar.gz && tar -zxf ct-module-1.3.2.tar.gz # 改用 LibreSSL 后不能再编译进 CT 模块  # Nginx substitutions filter module git clone https://github.com/nanqinlang-fork/ngx_http_substitutions_filter_module.git  # 编译所需 apt-get install -y build-essential cmake clang  # 开始吧 ./configure / --prefix=/home/nginx / --builddir=/home/nginx-installation/build / --sbin-path=/home/nginx/sbin/nginx / --modules-path=/home/nginx/modules / --pid-path=/home/nginx/sbin/nginx.pid / --conf-path=/home/nginx/conf/nginx.conf / --error-log-path=/home/nginx/logs/error.log / --http-client-body-temp-path=/home/nginx/temp/client_body / --http-proxy-temp-path=/home/nginx/temp/proxy / --with-pcre=/home/nginx-installation/pcre-8.42 / --with-zlib=/home/nginx-installation/zlib-cloudflare / --with-openssl=/home/nginx-installation/libressl-2.6.4 / --with-http_ssl_module / --with-http_v2_module / --with-http_gzip_static_module / --with-http_gunzip_module / --add-module=/home/nginx-installation/ngx_brotli / --with-http_sub_module / --add-module=/home/nginx-installation/ngx_http_substitutions_filter_module / --with-http_stub_status_module / --with-http_degradation_module / --without-http_fastcgi_module / --without-http_scgi_module / --without-http_uwsgi_module / --without-http_autoindex_module / --without-http_charset_module / --without-http_empty_gif_module / --without-mail_pop3_module / --without-mail_imap_module / --without-mail_smtp_module  make && make install 

为了能够更方便地体会到新特性,在版本上我都选择了较新的版本,在 configure 中我详细的指定了各项目录、启用了较多模块。

--with-openssl-opt=enable-tls1_3 用于启用 TLS 1.3 本站已不再启用 TLS1.3。更多详细可以看我的 这篇文章

--add-module=.../ngx_brotli 用于启用 Brotli 压缩。更多详细可以看我的 这篇文章

--with-http_gzip_static_module 用于启用 Gzip 压缩,这个我在前面的 性能篇 中已有提过,且也有 一篇文章 专门讲述其配置。

--add-module=.../nginx-ct-1.3.2 用于 TLS 拓展方式启用 Certificate Transparency 特性,更多详细可以看我的 这篇文章

全局配置

Nginx 的全局配置位于 nginx.conf 中:

# 运行 Nginx 的用户组和用户 user                  naive naive; # 进程数,一般设置为 CPU 核心数的两倍,推荐使用 auto 即可 worker_processes      auto; # 错误日志路径 error_log             /home/nginx/logs/error.log crit; # pid 路径 pid                   /home/nginx/sbin/nginx.pid; # 最大访问文件数限制 worker_rlimit_nofile  4096;  events {     # nginx 处理请求的方法     use                 epoll;     # 单个 worker process 的最大链接数     worker_connections  4096;     # 让 nginx worker 进程尽可能多的接受请求     multi_accept        off; }  http {     include         mime.types;     charset         UTF-8;     default_type    application/octet-stream;      # 隐藏 Nginx 版本信息     server_tokens   off;     # 连接日志记录的格式     log_format logformat '[$time_local] [ $remote_addr $http_user_agent] [$status $request_time] [$http_host $request] [$http_referer]';      # 限制并发连接数,需要在站点配置中写入剩下的配置项     limit_conn_zone $binary_remote_addr zone=perip:10m;     limit_conn_zone $server_name        zone=perserver:100m;      # TCP 优化     sendfile            on;     tcp_nopush          on;     tcp_nodelay         on;     keepalive_timeout   60s;      # Gzip 压缩     gzip  on;     gzip_vary  on;     gzip_min_length  1000;     gzip_buffers  16 10k;     gzip_comp_level  3;     gzip_proxied  any;     gzip_types  text/plain text/html;     gzip_http_version  1.0;     gzip_disable  "msie6";     gzip_static always;     gunzip on;      # open_file_cache     open_log_file_cache         max=4096 inactive=30m min_uses=3 valid=30m;     open_file_cache             max=4096 inactive=12h;     open_file_cache_valid       12h;     open_file_cache_min_uses    3;     open_file_cache_errors      off;      # TLS 会话恢复     ssl_session_tickets     on;     # 如果只是单机站点,不需配置 ticket_key 这项     ssl_session_ticket_key  /home/site/crt/ssl_session_ticket.key;     ssl_session_cache       shared:SSL:10m;     ssl_session_timeout     30m;      # 引入我的站点配置     include sometimesnaive.conf; } 

站点配置

本博客站点配置位于 sometimesnaive.conf

server {     listen 443 ssl spdy http2 fastopen=3 reuseport;      root   /home/site/blog;     index  index.html;      server_name  sometimesnaive.org;      # 连接日志     access_log      /home/site/access-log/access.log logformat;     log_not_found   off;      # 限制并发连接数     limit_conn  perip 10;     limit_conn  perserver 100;      #ssl_protocols TLSv1.3 TLSv1.2;     #本博客已取消启用 TLS 1.3     ssl_protocols  TLSv1.2;      # 本站已启用 ECC & RSA 双证书     ## Let's Encrypt 的 ECC 证书     ssl_certificate      /home/site/crt/ecc/LetsEncryptecc-chained.cer;     ssl_certificate_key  /home/site/crt/ecc/LetsEncryptecc.key;     ## 本站已启用 Certificate Transparency     ssl_ct  on;     ssl_ct_static_scts   /home/site/crt/ecc;     # Let's Encrypt的 RSA 证书     ssl_certificate      /home/site/crt/rsa/LetsEncryptrsa-chained.cer;     ssl_certificate_key  /home/site/crt/rsa/LetsEncryptrsa.key;     ssl_ct_static_scts   /home/site/crt/rsa;      ssl_prefer_server_ciphers  on;     ssl_dhparam  /home/site/crt/dh-4096.pem;     ssl_ciphers  'ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-ECDSA-CHACHA20-POLY1305 ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-RSA-AES128-GCM-SHA256 ECDHE-RSA-CHACHA20-POLY1305 ECDHE-RSA-AES256-GCM-SHA384';      # OCSP Stapling     ssl_stapling             on;     ssl_stapling_verify      on;     ssl_trusted_certificate  /home/site/crt/LetsEncryptocsp.cer;     resolver  208.67.222.222 valid=300s;     resolver_timeout         5s;      # HTTP 响应头     add_header  Strict-Transport-Security  "max-age=31536000; preload; includeSubDomains" always;     add_header  X-Frame-Options            "deny" always;     add_header  X-Content-Type-Options     "nosniff" always;     add_header  X-Xss-Protection           "1; mode=block" always; } 

以上配置,分别解释在我的以下文章。以下列表,按照“站点配置中的配置项目”,从上至下依次排列:

转载请注明:逗比根据地 » 本博客 Nginx 配置(系列三) 完整篇

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址