Nginx 使用指南与服务器搭建记录
Nginx 是一款高性能的 HTTP 和反向代理服务器,以其高并发、低内存占用著称。
# CentOS/RHEL/Alibaba Cloud Linux
yum install -y epel-release
yum install -y nginx
# 启动服务
systemctl start nginx
systemctl enable nginx
nginx -t # 测试配置文件
nginx -s reload # 重载配置
systemctl start nginx # 启动
systemctl stop nginx # 停止
/etc/nginx/nginx.conf/etc/nginx/conf.d/*.conf/usr/share/nginx/html/server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
使用正则表达式匹配所有子域名:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.example\.com$;
location / {
root /var/www/$subdomain;
}
}
在 HTML 的 <head> 中添加:
<meta charset="UTF-8">
或在 Nginx 配置中添加:
charset utf-8;
问题:访问子域名返回默认页面
解决:删除或注释 nginx.conf 中的默认 server 块