DTeam团队日志

Nginx部署与维护

2018-12-15  本文已影响6人  冯宇Ops

安装

直接通过官方仓库安装,细则参考官方文档

添加官方仓库

# 添加nginx官方仓库GPG密钥
curl -s https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
# 添加nginx官方仓库配置文件
echo "deb http://nginx.org/packages/ubuntu/ `lsb_release -sc` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
# 刷新本地软件缓存
sudo apt update

安装软件包

sudo apt install -y nginx
# 注意nginx官方仓库安装的nginx默认不会开机自启动,需要设置开机自启动,而Ubuntu官方仓库的nginx-full/nginx-light包无此问题
sudo systemctl enable nginx
sudo systemctl start nginx

配置

Nginx默认的配置文件存储于/etc/nginx/目录下,官方仓库安装的nginx会在主配置文件/etc/nginx/nginx.conf中使用include conf.d/*.conf;指令引入/etc/nginx/conf.d/*.conf配置文件,建议虚拟主机配置统一存放于/etc/nginx/conf.d/<name>.conf配置文件。

参考/etc/nginx/nginx.conf配置文件内容(做过部分优化):

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
  multi_accept        on;
}

http {
  include        /etc/nginx/mime.types;
  default_type   application/octet-stream;
  server_tokens  off;
  etag           off;
  
  access_log  /var/log/nginx/access.log combined;
  error_log   /var/log/nginx/error.log;
  
  sendfile        on;
  tcp_nopush      on;
  tcp_nodelay     on;
  
  keepalive_timeout    30;
  types_hash_max_size  2048;
  
  gzip             on;
  gzip_min_length  1000;
  gzip_comp_level  6;
  gzip_proxied     expired no-cache no-store private auth;
  gzip_types
    text/plain
    text/css
    text/javascript
    text/xml
    application/json
    application/x-javascript
    application/javascript
    application/xml
    application/xml+rss
    application/vnd.ms-fontobject
    application/x-font-ttf
    font/opentype
    font/truetype
    image/svg+xml
    image/x-icon;
  gzip_disable     "MSIE [1-6]\.";
  gzip_vary        on;
  
  include /etc/nginx/conf.d/*.conf;
}

此配置文件结合了Nginx官方版本和Ubuntu仓库版本的两个配置文件,进行了融合。

HTTPS配置(配合let's encrypt免费证书)

首先申请let's encrypt免费证书:

# 安装let's encrypt的certbot-auto脚本,仅第一次部署需要
wget https://dl.eff.org/certbot-auto
chmod +x certbot-auto
sudo mv certbot-auto /usr/local/bin/

# 申请免费证书
certbot-auto certonly --webroot -w /usr/share/nginx/html/ -d 你的域名 -m 你的邮件 --agree-tos

申请成功的证书文件存放在/etc/letsencrypt/live/你的域名/,在nginx中直接配置这个证书路径就可以了。参考cipherli的Strong SSL(原理描述参见这篇文档)的配置:

# 第一次需要在/etc/nginx/目录下生成一个dhparam.pem文件
cd /etc/nginx
sudo openssl dhparam -out dhparam.pem 4096

添加对应的/etc/nginx/conf.d/你的域名.conf配置文件:

server {
  listen 443 ssl http2;
  server_name  你的域名;

  ssl_certificate /etc/letsencrypt/live/你的域名/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/你的域名/privkey.pem;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_prefer_server_ciphers on; 
  ssl_dhparam dhparam.pem;
  ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
  ssl_ecdh_curve secp384r1;
  ssl_session_timeout  10m;
  ssl_session_cache shared:SSL:10m;
  ssl_session_tickets off;
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 223.5.5.5 114.114.114.114 valid=300s;
  resolver_timeout 5s; 
  # add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
  add_header X-Frame-Options DENY;
  add_header X-Content-Type-Options nosniff;
  add_header X-XSS-Protection "1; mode=block";

  root /usr/share/nginx/html;
  index index.html;

  ... SNIP ...

  location /.well-known {
    root /usr/share/nginx/html;
  }

  if ($scheme != "https") {
      return 301 https://$http_host$request_uri;
  }
}

以上配置需要注意:

let's encrypt证书自动续期:

# 交互式编辑crontab
sudo crontab -e
# 打开交互式编辑器之后加入以下内容
0 0 */15 * * /usr/local/bin/certbot-auto renew -q --post-hook '/usr/sbin/service nginx reload'

表示每15天的00:00自动调用certbot-auto renew命令定时续期。

日常维护

服务维护

直接使用service manager即可方便管理

sudo systemctl start/stop/restart/reload/status nginx
sudo service start/stop/restart/reload/status nginx

配置检查

sudo nginx -t

日志查看

默认的日志路径在/var/log/nginx/,其中access.log是用户请求访问日志,error.log是错误日志。默认情况下nginx安装包已经释放了logrotate脚本/etc/logrotate.d/nginx,会对/var/log/nginx/下的*.log文件自动滚动。因此建议自定义的日志文件也放在这个目录下。

如果访问遇到问题,需要开启debug模式查看请求。除了在error_log配置中开启debug级别之外,还必须使用nginx-debug命令运行nginx服务才可以。详细参考官方文档

sudo service nginx stop
sudo service nginx-debug start
上一篇 下一篇

猜你喜欢

热点阅读