Nginx部署与维护

作者: 冯宇Ops | 来源:发表于2018-12-15 13:39 被阅读6次

安装

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

添加官方仓库

# 添加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;
  }
}

以上配置需要注意:

  • TLSv1.3需要openssl 1.1.1以上版本和Nginx 1.13以上版本支持,而当前ubuntu的LTS版本(当前18.04)不支持,所以只启用TLSv1.3会报错。同时写上只是为了以后如果真的需要do-release-upgrade这里不用改动
  • add_header Strict-Transport-Security这一行注释掉是关闭了HSTS,如果需要HSTS的话可以开启,但是注意是否需要影响到其他子域名,有关HSTS的详细描述参考: https://developer.mozilla.org/zh-CN/docs/Security/HTTP_Strict_Transport_Security
  • location /.well-known这个 配置端是为了兼容let's encrypt的证书续期。let's encrypt的HTTP检测会检查/.well-known这个路径
  • 最后一个配置段是强制跳转https,开启http->https的跳转。用户如果访问的是http协议,会强制重定向到https访问。不需要这个功能可以删掉

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

相关文章

  • Nginx部署与维护

    安装 直接通过官方仓库安装,细则参考官方文档 添加官方仓库 安装软件包 配置 Nginx默认的配置文件存储于/et...

  • nginx学习目录

    nginx安装部署和配置管理 nginx日志配置 nginx平滑升级与回滚 nginx反向代理 nginx负载均衡...

  • 解决Nginx启动出现端口被占用提示问题

    今日伏笔主机测试部署LEMP的时候需要部署与启动Nginx的时候有出现提示"Starting nginx: ngi...

  • 微服务注册中心Consul部署

    目录 部署节点 部署设计 依赖 安装与运行 维护 1. 部署节点 2. 部署设计 2.1 高可用 Consul集群...

  • Docker应用部署教程

    MySQL部署 Tomcat部署 Nginx部署 提前在该文件写入如下内容$PWD/conf/nginx.conf...

  • 杂谈部署

    Flask + Gunicorn + Nginx 部署 nginx 位置vim /etc/nginx/nginx....

  • Windows Server 2008 R2 Enterpris

    部署nginx nginx主要做反向代理用,可以单独部署到其它机器上,这里nginx和tomcat部署在同一台机器...

  • PostgreSQL部署与维护

    安装 详情参考官方安装文档 安装指定版本: 配置 postgresql的配置在/etc/postgresql/VE...

  • GitLab部署与维护

    安装 推荐使用omnibus打包版本进行安装和部署,官方提供了软件仓库部署。官方仓库托管在S3上,国内比较慢,因此...

  • Nginx安装与服务部署

    Nginx安装与服务部署 nginx安装 a.安装编译工具及库文件 b.下载PCRE c.安装Nginx 配置服务...

网友评论

    本文标题:Nginx部署与维护

    本文链接:https://www.haomeiwen.com/subject/uqcahqtx.html