sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
C:\nginx
)nginx.exe
启动服务brew install nginx
brew services start nginx
/etc/nginx/nginx.conf
nginx安装目录/conf/nginx.conf
# 工作进程数,通常设置为CPU核心数
worker_processes auto;
# 事件模块配置
events {
worker_connections 1024; # 每个工作进程的最大连接数
}
http {
# MIME类型设置
include mime.types;
default_type application/octet-stream;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志
access_log /var/log/nginx/access.log main;
# 开启高效文件传输模式
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 连接超时时间
keepalive_timeout 65;
# 开启gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 包含其他配置文件
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
通常放在/etc/nginx/conf.d/
或/etc/nginx/sites-available/
目录下
server {
listen 80;
server_name example.com www.example.com;
# 网站根目录
root /var/www/example.com;
index index.html index.htm;
# 访问日志
access_log /var/log/nginx/example.com.access.log main;
# 错误页面配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri $uri/ =404;
}
# 静态文件缓存设置
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t # 测试配置
sudo systemctl reload nginx
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# SSL优化配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
# 其他配置...
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost: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;
}
}
nginx -t
nginx -s reload
或 systemctl reload nginx
nginx -s stop
或 systemctl stop nginx
systemctl start nginx
systemctl status nginx
/var/log/nginx/access.log
/var/log/nginx/error.log
worker_processes
worker_connections
以适应高并发sendfile
和tcp_nopush
提高静态文件传输效率keepalive_timeout
希望这份指南能帮助您顺利安装和配置Nginx服务器!