参考资料

  1. 查看nginx 设置配置文件路径方法
  2. Nginx伪流媒体服务器搭建详细说明以及案例
  3. Nginxlimit_conn:并发连接数限制模块详细说明以及案例
  4. Nginx反向代理缓存服务器配置详细说明以及案例
  5. Nginx Stream(TCP/UDP)负载均衡详细说明以及案例
  6. Nginx + PHP-FPM 配置指南
  7. 实现完整离线浏览,需配合Service Worker
  8. Nginxindex:首页处理模块详细说明以及案例

安装

# Ubuntu/Debian
sudo apt install nginx

# CentOS/RHEL
sudo yum install nginx

# macOS
brew install nginx

启动/停止

# 启动
sudo systemctl start nginx

# 停止
sudo systemctl stop nginx

# 重启
sudo systemctl restart nginx

# 开机自启
sudo systemctl enable nginx

配置文件

  • 主配置:/etc/nginx/nginx.conf

  • 子配置:/etc/nginx/conf.d//etc/nginx/sites-available/

基础配置示例

server {
    listen 80;
    server_name example.com;
    
    location / {
        root /var/www/html;
        index index.html;
    }
}

反向代理配置

location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
}

检查配置

sudo nginx -t

重载配置

sudo nginx -s reload

静态文件服务

location /static {
    alias /path/to/static/files;
    expires 30d;
}

日志文件

  • 访问日志:/var/log/nginx/access.log

  • 错误日志:/var/log/nginx/error.log

SSL配置

# 生成证书(示例)
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
}