# Nginx ## 常用命令 ```sh # 启动 Nginx 服务 sudo systemctl start nginx # 关闭 sudo systemctl stop nginx # 重启 sudo systemctl restart nginx # 重新加载 Nginx 以应用更改 sudo systemctl reload nginx # 默认目录 cd /var/www/ # 配置文件 cd /etc/nginx/ /etc/nginx/nginx.conf #测试 Nginx 配置是否正确 sudo nginx -t ``` ## 在 Ubuntu 22.04 上安装和配置 Nginx ### 安装 ```sh #首先,确保您的软件包索引是最新的: sudo apt update #步骤 2:安装 Nginx sudo apt install nginx #步骤 3:启动 Nginx 服务 sudo systemctl start nginx #步骤 4:使 Nginx 服务开机自启 sudo systemctl enable nginx #步骤 5:检查 Nginx 状态 sudo systemctl status nginx #步骤 6:配置防火墙 sudo ufw allow 'Nginx Full' ``` ### 配置ssh 常见的存放路径是 /etc/nginx/ssl/ ```sh sudo mkdir -p /etc/nginx/ssl sudo cp /path/to/your/example.com.crt /etc/nginx/ssl/ sudo cp /path/to/your/example.com.key /etc/nginx/ssl/ ``` ```sh server { listen 443 ssl; server_name api.zpc-xy.com; ssl_certificate /etc/nginx/ssl/api.zpc-xy.com_nginx/api.zpc-xy.com_bundle.crt; ssl_certificate_key ssl/api.zpc-xy.com_nginx/api.zpc-xy.com.key; # ssl_protocols TLSv1.2 TLSv1.3; # 仅使用 TLS 1.2 和 1.3 # ssl_ciphers HIGH:!aNULL:!MD5; # 配置强密码套件 # location / { # proxy_pass http://localhost:3000; # 例如,如果你有一个后端应用运行在端口3000 # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-Proto $scheme; # } } ``` ### 配置文件 ```bash # 基础配置 server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ =404; } } server { # 添加虚拟目录映射 location /output/ { # alias /disk/ai_sports/DataGateway/output/; # 或者使用 root 指令(视具体情况而定) root /disk/ai_sports/DataGateway/output/; # 可选配置:根据需要添加其他指令,如访问控制、缓存策略等 autoindex on; # 如果希望列出目录内容 # expires 30d; # 设置静态资源缓存过期时间 # try_files $uri $uri/ =404; # 用于处理目录索引和文件不存在的情况 } } ```