跳转至

💪🏻 Nginx

参考



安装及使用

  1. 安装

    Bash
    1
    sudo apt install nginx
    
  2. 卸载

    Bash
    1
    2
    sudo apt purge nginx nginx-common
    sudo apt autoremove
    
  3. 热重载

    Bash
    1
    sudo nginx -s reload
    
  4. 重启服务

    Bash
    1
    sudo systemctl restart nginx
    



/etc/nginx/nginx.conf

  • 主配置文件:用于定义全局配置和基本设置
  • 包含指令:包含服务器级别的设置、全局变量、工作进程数、日志文件位置、用户权限、加载模块等
  • 包含其他文件:通常会使用include指令将其他配置文件包含进来,如/etc/nginx/conf.d/*.conf,从而组织和管理配置文件



/etc/nginx/conf.d/*.conf

  • 虚拟主机配置文件:用于定义具体的虚拟主机(服务器)配置的文件,通常用于设置特定域名的服务器块
  • 作用范围:这个文件主要包含HTTP服务器块(server block),定义了具体的域名、监听端口、根目录、日志路径、反向代理设置等
  • 从属关系:这个文件通常被主配置文件 nginx.conf 通过 include 指令包含进来,使得Nginx能够加载这些虚拟主机配置
Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;                    # IPv4 80端口

    server_name example.com;      # 域名

    root /var/www/example.com;    # 网站根目录
    index index.html;             # 默认首页

    location / {
        try_files $uri $uri/ =404;# 文件查找规则
    }
}



SSL 证书

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
server {
    listen 80;                              # 将 http 重定向到 https
    server_name dxlcq.cn;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name dxlcq.cn;

    ssl_certificate     /etc/letsencrypt/live/dxlcq.cn/fullchain.pem;   # SSL 完整证书
    ssl_certificate_key /etc/letsencrypt/live/dxlcq.cn/privkey.pem;     # SSL 私钥
    ssl_session_cache   shared:SSL:1m;                                  # 缓存 SSL 会话
    ssl_ciphers         HIGH:!aNULL:!MD5;                               # 支持的密码套件
    ssl_protocols       TLSv1.2 TLSv1.3;                                # 支持的协议版本
    ssl_prefer_server_ciphers on;                                       # 优先使用服务器密码套件

    location /.well-known {                                             # 用于验证域名所有权
        root /;
    }
}
  • 安装 certbot

    Bash
    1
    2
    sudo snap install --classic certbot
    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    
  • 首次申请

    Bash
    1
    sudo certbot certonly --webroot -w / -d dxlcq.cn
    
  • 测试更新

    Bash
    1
    sudo certbot renew --dry-run
    
  • 每周更新 sudo crontab -e

    Bash
    1
    0 0 * * 1 certbot renew && nginx -s reload
    
  • 查看证书剩余时长

    Bash
    1
    sudo certbot certificates
    



下载站点与加密

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
server {
    location /private {         # 文档根目录
        alias   /usr/share/nginx/private;
        autoindex on;           # 自动索引

        # 鉴权
        auth_basic "password";
        auth_basic_user_file /usr/share/nginx/htpwd;
    }
}



使用 docker 快速部署 Nginx

Bash
1
2
sudo docker run --rm -d -p 80:80 \
    -v /www:/usr/share/nginx/html nginx



反向代理

Text Only
1
2
3
4
5
6
7
8
9
server {
    resolver 119.29.29.29 valid=6s;                                     # dns 解析 6 秒刷新一次

    location / {                                                        # 反向代理到后端服务器
        proxy_pass http://dxlcq.cn;                                     # 后端服务器地址和端口
        proxy_set_header Host $host;                                    # 保持主机头不变
        proxy_set_header X-Forwarded-Proto $scheme;                     # 转发协议
    }
}