Quick-Ref
Nginx

Nginx

Nginx 是一个高性能的 HTTP 和反向代理服务器,常用于 Web 服务器、负载均衡、缓存等。

  • 安装 Nginx:

    sudo apt install nginx
  • 启动 Nginx:

    sudo systemctl start nginx
  • 停止 Nginx:

    sudo systemctl stop nginx
  • 重启 Nginx:

    sudo systemctl restart nginx
  • 查看 Nginx 状态:

    sudo systemctl status nginx
  • 设置 Nginx 开机自启:

    sudo systemctl enable nginx
  • 取消 Nginx 开机自启:

    sudo systemctl disable nginx
  • 查看 Nginx 配置文件:

    cat /etc/nginx/nginx.conf
  • 测试 Nginx 配置文件:

    sudo nginx -t
  • 重新加载 Nginx 配置文件:

    sudo nginx -s reload
  • 查看 Nginx 运行日志:

    cat /var/log/nginx/access.log
    cat /var/log/nginx/error.log
  • 查看 Nginx 进程:

    ps aux | grep nginx
  • 停止 Nginx 进程:

    sudo pkill nginx
  • 示例 Nginx 配置文件:

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
  • HTTPS 配置示例:

    server {
        listen 443 ssl;
        server_name example.com;
        ssl_certificate /etc/nginx/ssl/example.com.pem;
        ssl_certificate_key /etc/nginx/ssl/example.com.key;
        location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }