nginx
nginx # 启动
nginx -s stop # 快速停止
nginx -s quit # 优雅停止
nginx -s reload # 热重载配置(不中断业务)
nginx -s reopen # 重新打开日志文件
nginx -t # 检查配置语法
nginx -V # 查看编译参数、模块
systemctl
systemctl status nginx # 查看 Nginx 状态
systemctl start nginx # 启动
systemctl stop nginx # 停止
systemctl restart nginx # 重启(先停再起)
systemctl reload nginx # 重载配置(不中断业务,优先用这个)
systemctl enable nginx # 设置开机自启
systemctl enable nginx --now # 设置开机自启并且立即启动
systemctl disable nginx # 关闭开机自启
nginx -t # 检查配置语法
# 全局块
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# events 块
events {
worker_connections 1024; # 单个worker连接数
}
# http 块
http {
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
# server 块(一个站点)
server {
listen 80;
server_name www.xxx.com;
# location 块
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 反向代理
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
location / {
proxy_pass http://127.0.0.1:8080;
}
upstream backend {
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
server {
location / {
proxy_pass http://backend;
}
}
location / {
rewrite ^/article/(\d+)$ /info.php?id=$1 last;
}
allow 192.168.1.0/24;
deny all;
location ~* \.(jpg|jpeg|png|css|js)$ {
expires 7d;
}
ssl_certificate:公钥证书(.crt/.pem)
ssl_certificate_key:私钥(.key)
路径自己改,权限建议 600。 chmod 600 path
server {
listen 80;
server_name yourdomain.com;
# HTTP 强制跳转 HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com;
# 证书路径(crt/cer/pem 都可以)
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
# 基础 SSL 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}

ss -tulnp | grep :80
nginx -t