nginx的用法
2025-03-05
7
安装
# 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; }
本篇文章内容来源于:nginx的用法
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。